关于java中是引用传递仍是值传递的问题

关于JAVA中参数传递问题有两种,一种是按值传递(若是是基本类型),另外一种是按引用传递(若是是對象).
首先以两个例子开始:
1)
public class Test2 {

public static void main (String [] args) {
StringBuffer a = new StringBuffer ("A");
StringBuffer b = new StringBuffer ("B");
operate (a,b);
System.out.println(a+","+b);
}

static void operate(StringBuffer x, StringBuffer y){
x.append(y);
y = x;
}
}
输出:AB,B

2)
public class Test2 {
public static void add3 (Integer i){
int val=i.intValue();
val += 3;
i = new Integer (val);
}

public static void main (String args [ ] ) {
Integer i = new Integer (0);
add3 (i);
System.out.println (i.intValue ( ));
}
}
输出:0
首先咱们应该明白JAVA中的参数传递全是以值传递的。是基本类型,就拷贝一个基本类型传进方法;是引用,就拷贝一个引用变量传进去方法,理解了这两点就能理解方法操做对象的相关问题了。最好能画出引用指向对象的图出来,就能彻底理解了。第1题,调用operate方法时,传入了两个引用a,b的拷贝x,y,这两个x,y都指向原a,b引用所指向的对象。x.append(y)对它指向的对象(即a指向的对象)进行了操做。而x=y,只是两个拷贝变量在赋值,并无影响到原b所指向的对象。因此b所指向的对象仍然为B。第2题,i=new Integer(val)只是一个引用的拷贝指向了另一个对象,而原来的i仍然是指向对象new Integer(0)的。把握住了JAVA都是传值而且传的都是拷贝的话,相似的题你们都能迎刃而解了。 Java中的参数传递只有一种方式: by value. 理论说教太麻烦了,直接看些例子吧:1). 基本类型 public class A{ public static void main(String[] args){ int x = 1; System.out.println(x); //1 test(x); System.out.println(x); //仍是1==>By value } static void test(int a){ a = 2; }}2). 引用类型 public class B{ public static void main(String[] args){ Integer x = new Integer(1); System.out.println(x); test(x); System.out.println(x); } static void test(Integer a){ a = new Integer(2); }} 理解这里的关键是区分对象和引用。 这里声明的x是一个引用,而不是一个对象(只是Java把它设计为看上去好像是对象同样)。这个引用它指向了一个对象,这个对象就是后面用new关键字生成的对象。所以,能够说x指向了一个Integer对象。 在调用test方法的时候,程序将x做为参数传递给test方法了。这里仍然是值传递,在test调用过程当中,会产生一份新的引用(不妨叫作y)。此时,x和y指向了同一个对象。 x和y指向的是同一个对象, 因为Java的设计,咱们能够经过操做引用来达到操做对象的目的。所以,若是咱们此时使用y来修改对象的属性 (例如,y.someField++); 你能够看到x指向的对象同时也被修改到了。 另外一方面,若是咱们让y指向另一个对象, y=new Integer(2); 此时x和y就指向了不一样的对象。y修改了它指向的对象的属性,很显然不会影响到x指向的对象。 有人说了数组。数组也是一个引用类型,它的参数传递方式按照引用类型的参数传递同样能够解释得通:import java.util.Arrays;public class A{ public static void main(String[] args){ int[] aa = {3, 2, 1}; System.out.println(Arrays.toString(aa)); //[3, 2, 1] test(aa); System.out.println(Arrays.toString(aa)); //[3, 2, 1] test2(aa); System.out.println(Arrays.toString(aa)); //[4, 2, 1] } static void test(int[] a){ a = new int[]{1, 2, 3}; //指向了新对象 } static void test2(int[] a){ if(a != null && a.length > 0) a[0]++; //修改原来的那个对象 }} 对象是传引用,简单类型是传值,不要被网上的一些概念所迷惑!!!你能够本身作个试验。至于String等类型传的仍是引用。若是你用concat方法,String对象的原值就会被改变。但你若是按以下方法:public class Test { public static void test(String str) { str = "World"; } public static void main(String[] args) { String string = "Hello"; test(string); System.out.println(string); }}  运行结果:Hello这里str = "World" 就等同于 String str=new String("World")。因此结果没有改变!!! 下列程序在1处是否会有异常,若是没有,输出是什么?是否会运行到2处,若是会,输出是什么?为何会有这样的结果?   import java.util.arraylist;   import java.util.list;      public class testclass {   public static void main(string args[]) {     list list = new arraylist();     test2(list);     system.out.println(list.size()); // 1处     test3(list);     system.out.println(list.size()); // 2处   }      public static void test2(list list) {     list = null;   }      public static void test3(list list) {      list.add(“aaaa“);   }   }plumechen:不会出错的。结果是0,1。由于test2(list)传得是list的引用,我理解成指针置的副本,list=null;只是把那个传入的值设置为null,不改变原来list的指针和内容。test3(list)传入的同样,可是执行了list.add()因为传入指针值的副本也指向原来的那个list的地址,因此原来的那个list的内容就改变了,size变成了1了本文复制于:http://wenwen.sogou.com/z/q101054529.htm
相关文章
相关标签/搜索