好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

浅谈JAVA内存分配与参数传递

java中方法的参数传递方式只有一种:值传递。

java内存分配:

1.栈:存放 基本类型的数据、对象的引用(类似于c语言中的指针)

2.堆:存放用new产生的数据

3.静态域:存放在对象中用static定义的静态成员

4.常量池:存放常量

5.寄存器

6.非ram存储

?

1

2

3

4

5

6

7

8

9

10

class birthdate{

  private int day;

  private int month;

  private int year;

  public birthdate( int d, int m, int y){

  day=d;

  month=m;

  year=y;

  }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

public class test{

  public static void main(string[] args){

  int date= 9 ;

  test test= new test();

  test.change(date);

  birthdate d1= new birthdate( 7 , 7 , 1970 );

  }

 

  public void change( int i){

  i= 1234 ;

  }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public class testtransfer{

  public static void main(string[] args){

  int a= 6 ;

  int b= 9 ;

  swap(a,b);

  system.out.println( "交换结束后,a的值是" +a+ ";b的值是" +b); //a=9,b=6

  }

 

  public static void swap( int a, int b){

  int tmp=a;

  a=b;

  b=tmp;

  system.out.println( "swap方法里,a的值是" +a+ ";b的值是" +b); //a=6,b=9

  }

}

 

 

 

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public class testtransfer{

  public static void main(string[] args){

  dataswap ds= new dataswap();

  ds.a= 6 ;

  ds.b= 9 ;

  swap(ds);

  system.out.println( "交换结束后,ds.a的值是" +ds.a+ ";ds.b的值是" +ds.b); //a=9,b=6

  }

 

  public static void swap(dataswap ds){

  int tmp=ds.a;

  ds.a=ds.b;

  ds.b=tmp;

  system.out.println( "swap方法里,ds.a的值是" +ds.a+ ";ds.b的值是" +ds.b); //a=9,b=6

  }

}

 

class dataswap{

  public int a;

  public int b;

}

以上所述是小编给大家介绍的java内存分配与参数传递详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://blog.csdn.net/qq_41990380/article/details/88669624

查看更多关于浅谈JAVA内存分配与参数传递的详细内容...

  阅读:11次