您的位置:首页 > 产品设计 > UI/UE

Is Java pass by VALUE or pass by REFERENCE?

2015-01-03 20:33 549 查看

Today, I wrote a java class like below:

public class RefTest {

public void changeString(String s){
System.out.println("inner changeString() s="+s);
s = "abc";

}

public static void main(String args[]){
RefTest rf = new RefTest();
String a = new String("a");
System.out.println("before changeString() s="+a);
rf.changeString(a);
System.out.println("after changeString() s="+a);
}
}
output:
before changeString() s=a

inner changeString() s=a

after changeString() s=a

The output seems that the method changeString() didn't change the String a, which was passed into the method. I searched the Internet and look for answers. My conclusion is as below:

Java has two kinds of variables, one is simple variables(e.g. int boolean etc.), another is references(e.g. String Character etc.).Let's talk about a question on how the two kinds of parameters passed into a method.

Simple variables:

let's look at a class:
public class RefTest {

public void changeInt(int i){
i = 5;
}

public static void main(String args[]){
RefTest rf = new RefTest();

int i = 2;
rf.changeInt(i);
System.out.println(i);
}
}
Apparently, the output is 2.

after creating a simple variable like this:
int i = 2;
the value 5 is stored in stack,
 when you pass i into a method like this:
changeInt(i);
Java create another variable in stack whose value is 2,too. The newly created int will be collected after the method return.
So, even if the newly created int is changed into 5 inner method, the older int i, whose value is 2 is not changed.

 
Reference variables:

when creating a reference, two different memory is used: reference(similar to record memory of the Object) is stored in stack, Object is stored in heap.

the reference in Java is similar to the pointer in C, both of them are memory address. In this point,it is the same as simple variables.
Let's see the class at the beginning of the blog,

when the reference a is passed into method like this:
public void changeString(String s)
Java created another reference s, which point to the same memory address as a, both of them point to a String Object in heap.

when it comes to this line:
s = "abc";
the reference s is pointed to another String Object "abc".

when the method returns, s as the local variable will be free. the reference a certainly didn't change its value(the address of String"a").

Another example:
.
public class RefTest {

public void changeString(StringBuffer s){
System.out.println("inner changeString() s="+s);
<pre name="code" class="java"><span style="white-space:pre">		</span>s.append("def");


}public static void main(String args[]){RefTest rf = new RefTest();StringBuffer a = new StringBuffer("abc");System.out.println("before changeString() s="+a);rf.changeString(a);System.out.println("after changeString() s="+a);}}


output:
before changeString() s=abc

inner changeString() s=abc

after changeString() s=abcdef

we know the local reference s and the reference a point to the same StringBuffer Object in heap.

when the line:
s.append("def");
is executed, the Object pointed by s is changed, which is just the same Object as a point.

As was discussed above, Java passes parameters by value.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java reference