How to pass the value of "Java"
When you ask most programmers whether Java is a value or a reference, you may get one of two answers:
(1) Java passes the original type data using the method of passing the value, when passing the object is used to refer to the way, string type of data using the method of value, because the string is immutable. (2) Java passes all parameters using the pass -through method.
Only the second answer is correct. The key to understanding the difference between a pass and a reference is to remember that when you pass an object to a method, Java does not put the object on the stack, it simply copies the reference to the object and puts the referenced copy on the stack. In other words, by definition, Java uses the method of passing values.
Prove
If Java passes the object by reference, executing the following code swaps the references stored in two variables A and B, and the corresponding output should be as follows:
A:4
b:100
swapped!
a:100
B:4
In fact, the real action is a reference in the parameters O1 and O2 of the method, and it does not affect the original variables A and B. Here is the actual output:
A:4
b:100
swapped!
A:4
b:100
Public classSwaptip { Public Static voidMain (String []args) {Integer a=NewInteger (4); Integer b=NewInteger (100); System.out.println ("A:" +a); System.out.println ("B:" +b); Swap (A, b); System.out.println ("Swapped!"); System.out.println ("A:" +a); System.out.println ("B:" +b); } Public Static voidSwap (Object O1, Object O2) {object T=O1; O1=O2; O2=T; }}
Why is the answer to that argument meaningful?
Many programmers who have been involved in arguments about how to pass parameters end up saying, "It's just a different semantics" or "It doesn't matter because you understand how it really works." ”
This can be a semantic problem for experienced programmers, but it's different for programmers with little experience. For programmers, the fewer black picks a language has, the better they can do when writing programs in that language.
An analogy
The relationship between an object reference and an instance is like a remote control's relationship to a TV, and a reference control object is like a remote controlled TV. If a copy of a remote control is given to another person, that person can also control the TV. The movement of the copied remote control to the TV (such as adjusting the volume, changing the channel, or adjusting the clock) is visible to both itself and the original remote. If the person modified the remote control of the copy to the other TV, then the original remote controller will not be affected.
Admittedly, it is only an academic distinction for Java to pass parameters by value or by reference, as long as we know what kind of behavior is expected. But sometimes it's important to know what's behind the table.
Java has only one way of passing parameters, that is, passing values. It's simple, documented, and it's true.
Transfer from http://www.blogjava.net/zsh118/articles/24564.html
Public classTest { Public Static voidMain (string[] args) {StringBuffer a=NewStringBuffer ("A"); StringBuffer b=NewStringBuffer ("B"); inti = 5;
System.out.println (A + "," + B + "," + i); Operate (A, B, i); System.out.println (A+ "," + B + "," +i); } Public Static voidOperate (StringBuffer x, StringBuffer y,intj) {x.append (y); Y=x; Y.append (C); J= 1; }}
Operation Result:
a,b,5
abc,b,5
Rule summary:Java parameters, whether primitive or reference type, are passed as replicas If the parameter type is the original type , then a copy of the parameter is passed in, that is, the value of the original parameter , which is the same as the value that was previously discussed. If you change the value of a copy in a function, the original value is not changed. If the parameter type is a reference type , then a copy of the reference parameter is passed in, and the copy holds the address of the parameter. If you do not change the address of the copy in the function, but change the value of the address by changing the address, then the change within the function will affect the parameters passed in. If you change the address of a copy in a function, such as when you perform an assignment such as a= other objects, a=new, and so on, you actually point A to the new position, the original value outside the function does not change. transferred from: http://www.cnblogs.com/wangfeixiong/p/7158442.html
How to pass the value of "Java"