First, let's look at the following code:
public class Test1 { String a = "123"; public static void change(Test1 test) { test.a="abc"; } public static void main(String[] args) { Test1 test1=new Test1(); System.out.println(test1.a); change(test1); System.out.println(test1.a); } }
Results output 123 ABC believes that everyone can do the right thing for the problem. Java is passed by reference, and the value of the object can be modified inside the function. Let's look at the following code:
public class Test2{ public static void main(String[] args) { String str = "123"; System.out.println(str); change(str); System.out.println(str); } public static void change(String str){ str = "abc"; }}
How much do you think it will output? Anyway, several people around me said they would output 123 ABC. Because string is not a basic data type in Java, a reference is passed, so you can modify the value of str in the change method.
But the actual output is really 123 123, is not a bit ruined three views?
Myth 1:
Some say there may be a problem with the way the string is defined here, because the way it is defined here is string str = "123", there may be a problem, and Java will store it in a string constant pool (when declaring a string of the same content again, the original memory in the string constant pool will be used. and not reassigned)
Written
Java string str = new String ("123");
STR is the memory that points to the heap area that passes the reference. But the results are still printed 123 123
The misunderstanding 2:string is immutable
Someone else looks at the string document discovery
Strings is constant; Their values cannot is changed after they is created. String buffers support mutable strings. Because String objects is immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {‘a‘, ‘b‘, ‘c‘}; String str = new String(data);
You can see the official documentation explaining that the string cannot be changed. So some people think this is the reason why a string cannot be modified inside a function.
Take a look at the following code
public class Test3{ public static void main(String[] args) { String str = new String("123"); str = new String("abc"); System.out.println(str); }}
According to this understanding, the string here should also print 123 instead of ABC. Obviously it's not nonsense.
The document says StringBuffer is mutable. Let's try StringBuffer.
public class Test4{ public static void main(String[] args) { StringBuffer str = new StringBuffer("123"); System.out.println(str); change(str); System.out.println(str); } public static void change(StringBuffer str){ str = new StringBuffer("abc"); // str.append(abc); }}
Still printing 123 123, visible is not at all the reason, and the variable immutable understanding of the wrong.
Pass a reference?
So the question arises, what does it mean by reference?
It may be easier to understand that a person who has learned C + + will be able to change the value of a variable in a function to pass the address of that variable. That is, it is modified by a pointer (changing the value of a pointer variable to pass a level two pointer).
What does it mean to pass a reference in Java? Essentially, the address of the variable is passed over, which is actually passed by value.
The above memory graph represents the new variable when the real object is actually in the heap area, and the stack holds an address to the heap that holds the object's memory. Then passing the reference at the time of the function call is to pass the address worth a copy. This address value allows you to modify the contents of the heap area where the address resides. Remember that the address passed is passed by value. Modifying this in a function is worth the result of losing the pointer to the heap area object. The object has no effect after the function call is complete. As in code 1, the address of the Test1 object is passed, so the value of the Test1 object can be modified in the heap area, and Str is his attribute of course can be modified.
Look at the code below.
public class Test5{ public static void main(String[] args) { Object o = new Object(); System.out.println(o); change(o); System.out.println(o); } public static void change(Object o){ o = null; }}
Call the change function to null O, no effect on the O object, the value of printing o again has a value, because only a copy of the O object address is passed, and modifying the value of the variable inside the function has no effect on the outside.
Therefore, note that in Java, the by-reference delivery is essentially the address of the object, which is actually passed by value, through which you can modify the value of the object that it points to in memory. Changing the value of the address is meaningless and will only lose control of the real object.
Let's explain the reasons why the above Myth 2 is generated. The document says that string immutable StringBuffer variable means that the memory of the heap area is volatile.
For the string class, the memory that was previously requested in the heap cannot be modified by reference, and the size is fixed, that is, the value cannot be modified because his bottom is a char array. When you give the variable new a value again, he points to another heap memory, and on the surface it changes the value.
For StringBuffer, programmers can continue to allocate more memory based on actual use, such as calling the Append method, which is mutable. For example, the comment in the function in code 4 above can modify the value of string.
Added at November 2, 2015 10:44:27
Attached to the StackOverflow on the value of Java or a reference to the answer: is Java "pass-by-reference" or "Pass-by-value"?
Java is always pass-by-value. Unfortunately, they decided to call pointers references, thus confusing newbies. Because Those references is passed by value.
Java has always been a value pass. Unfortunately, they decided to call the pointer a reference, so the newcomer was always dizzy. Because these references are also passed by value
Translated from http://blog.csdn.net/cauchyweierstrass/article/details/49047217
The original author is weiers. Please respect the copyright of the original author
Do you really understand that Java is passed by reference?