04-java小案例-调用函数-内存

1,语句

——————————————————————————————————————       

代码实战
建议先动手实践后看答案


效果
*****
****
***
**
*
代码
class Demo1 
{
    public static void main(String[] args) 

    {

    for(int x=0;x<5;x++)
        {    
        for(int a=0;a<5-x;a++)            //  for(int a=x;a<5;a++)
            {
            System.out.print("*");
            }                                  
            System.out.println();  
        }                                      
    }                                          
}                                              

效果
54321
5432
543
54
5

代码
class Demo2
{
    public static void main(String[] args) 

    {

    for(int x=0;x<5;x++)
        {    
        for(int a=5;a<x;a--)            //  for(int a=x;a<5;a++)
            {
            System.out.print(a);
            }                                  
            System.out.println();  
        }                                      
    }                                          
}                                             

效果
1*1=1
2*1=2   2*2=4
3*1=3   3*2=6   3*3=9
4*1=4   4*2=8   4*3=12  4*4=16
5*1=5   5*2=10  5*3=15  5*4=20  5*5=25
6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36
7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49
8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64

代码
class Demo3
{
    public static void main(String[] args) 

    {

    for(int x=1;x<9;x++)
        {    
        for(int a=1;a<=x;a++)           
            {
            System.out.print(x+"*"+a+"="+a*x+"\t");
            }                                  
            System.out.println();  
        }                                      
    }                                          
}    

                                         


class Demo1 
{
    public static void main(String[] args) 

    {

    for(int x=1;x<9;x++)
        {    
        for(int a=1;a<=x;a++)           
            {
            System.out.print(x+"*"+a+"="+a*x+"\t");
            }                                  
            System.out.println();  
        }                                      
    }                                          
}                                             


效果
 * * * * *
  * * * *
   * * *
    * *
     *

代码
class Demo4
{
    public static void main(String[] args) 

    {
            for(int x=1;x<=5;x++)
        {
                for(int y=1;y<=x;y++)
            {System.out.print(" ");}


                for(int z=x;z<=5;z++)
        {System.out.print("* ");}
                System.out.println();
        }
    }                                          
}                                             

——————————————————————————————————————            


break:跳出
break的作用范围,要么switch语句,要么循环语句
break单独存在时,下面部分执行不到。

continue :继续
作用循环结构,结束本次循环,继续下次循环。

class Demo5
{
    public static void main(String[] args )

diyige.for(int  x=0;x<4;x++)
{
            for(int  y=0;y<4;y++)
     {

        System.out.print("*")
        break diyge;
      }

}

}


注:
\n:回车换行
\t:制表符
\b:退格符
\r:按下回车键
\n:回车符Linux
\r\n:回车符windows


2,函数

函数,定义在类中的特定功能的一段程序或方法


调用函数

格式:

修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2)

{

        执行语句;
        return 返回值;

}


//没有具体返回值,在return ;(分号)并  void 替代int
//public static void hello()
{
    System.out.print ("hello  world ");
    return ;//可以省略
}


例子:

class Demo1
{
    public static void main(String[] args) 
    {
        int c = add(5,3);//调用函数
        System.out.print("c="+c);
    }


static int add(int a,int b)//定义函数
{
return a+b;
}
}


注意 :函数中可以调用函数,但是不能定义函数。

函数的重载overload:
容许使用同名函数,只要参数类型或者参数个数不同就行。

 
3,数组


数组:同一类型数据的集合


格式:

元素类型 []  数组名 = new 元素类型{元素个数和元素长度}

int [] arr = new int[5];

arr[4]=22;

int [] arr = new int []{1,2,3,4,};

int [] arr ={1,2,3,4};




System.out.print (arr);

[  数组
I  int
c17166   哈希





内存的划分

1,寄存器      cpu
2,本地方法区    所在系统相关Linux windows mac
3,方法区
4,桟内存:       存储的都是局部变量。而且变量所属的作用域一旦结束,该变量就自动释放
5,堆内存:    存储的是数组和对象(数组即使对象)  凡事new建立在堆中
                      特点:1,每一个实体都有首地址值   
                                2,堆内存中,每一个变量都有默认初始化值,根据类型的不同步二不同,整数是0,小数是0.0或者0.0f boolean false  char '\u0000'
                                3,垃圾回收机制
























posted @ 2016-07-24 23:01  孙中明  阅读(96)  评论(0编辑  收藏  举报