Passing values and passing references
When you ' re passing primitives to a method, you get a distinct copy of the primitive. When you ' re passing a reference to a method, you get a copy of the reference.
The above is quoted as "Thinging in Java", a summary is that regardless of the type of Java parameters, all the parameters are passed a copy.
In Java, variables fall into the following two categories:
For primitive type variables (int, long, double, float, Byte, Boolean, char), Java is a copy of the value passed.
For all object-type variables, Java is a copy of the reference, in fact, the essence of wearing a reference copy is to copy the pointer to the address.
One, the value Example 1: The value of the INT type program
@Test publicvoidpassInt(){ int0; System.out.println("Before operation, testInt is : " + testInt); intOperation(testInt); System.out.println("After operation, testInt is : " + testInt); } publicvoidintOperation(int i){ 5; System.out.println("In operation, testInt is : " + i); }
Results
is0Inis5Afteris0
Summarize
The result is not ugly, although the Intoperation () method changes the value of the parameter passed in, but it has no effect on the parameter source itself, the parameter type is a simple type when it is passed by value. When passing a variable of a simple type as a parameter type, it is actually passing the value of the parameter as a copy into the method function, so the result of changing the value in the method function is to change only the value of the copy, not the source value .
Second, the reference Example 2: The object type variable code
@Test Public void Passclass() {Person person =NewPerson (175, $); System. out. println ("Before classoperation, the person height is"+ Person.height +" and the weight is"+ person.weight); Classoperation (person); System. out. println ("After classoperation, the person height is"+ Person.height +" and the weight is"+ person.weight); } Public voidClassoperation ( Personperson) {Person.height = the; Person.weight = the; System. out. println ("In classoperation, the person height is"+ Person.height +" and the weight is"+ person.weight); } Public classperson{intHeightintWeight Public Person(intHeiintWei) { This. height = hei; This. Weight = Wei; } }
Results
theis175andtheis140theis190andtheis160theis190andtheis160
Summarize
From the results it is not difficult to see that after classoperation () processing, the value of person has changed.
There are some differences between the reference and the previous pass value, which can be understood as follows:
Value: The current value is like an apple A, the value in Java is the equivalent of the apple copied an Apple B, actually passed the Apple B, that is, no matter what changes to Apple B, Apple A is unchanged.
Reference: As you can understand, the current person is a warehouse, because this warehouse (object type variable) is not as small as the basic type variable, just as a warehouse is much larger than an apple. Smart Java does not pass the warehouse every time to re-copy a warehouse to pass the past, it is the practice is to copy key A a key B (the key is equivalent to reference, reference to the warehouse), the transfer of the key B pass the past, Similar to the value of the transfer of the key B is not affected by the key a value of the reference, but the key A and key B all point to the same warehouse, that is, through the key B to change the value of the warehouse (such as stealing 10,000 tons of food), then the value of this warehouse is indeed changed, If the warehouse is accessed through key A, the result is the same as the B access (10,000 tons less food).
Example 3: Passing String Type code
@Test publicvoidpassString(){ "Hello"; System.out.println("Before operation, testString is : " + testString); stringOperation(testString); System.out.println("After operation, testString is : " + testString); } publicvoidstringOperation(String s){ "World"; System.out.println("In operation, testString is : " + s); }
Results
is : HelloInis : WorldAfteris : Hello
Summarize
The string type is also an object type, so it is a reference copy.
But the problem is that the string type is also an object type, so why did the string type object, after Stringoperation (), not change the value?
First, the string type is an object-type variable, so it is a copy of the reference. Do not assume that string is a basic variable type because it is very easy to use in Java and does not require new. Just string is a non-mutable class, making it no different to pass a value or to wear a reference.
Then to solve the problem mentioned above, in fact, the problem is the creation of a string, in Stringoperation (), s = "World";
this statement equivalent to execute String s = new String("World");
, yes, that is, in this method s
is equivalent to a new string object, When this function ends, the function s
disappears and the value of the original memory address does not change.
Here are two examples to illustrate this problem:
Example 4: String Type--Create new object code using new
@Test public void Span class= "Hljs-title" >passstring () {String teststring = new String ( "Hello" ); System. out . println ( "before operation, TestString is:" + teststring); Stringoperation (teststring); System. out . println ( "after operation, TestString is:" + teststring); } public void Stringoperation (String s) {s = new String ( "world "); System. out . println ( + s); }
Output
is : HelloInis : WorldAfteris : Hello
Summarize
The difference between example 4 and example 3 is that creating a string uses new, and the final output is not the same.
Example 5: Object-type variable--create code using New in Classoperation ()
Public classperson{intHeightintWeight Public Person(intHeiintWei) { This. height = hei; This. Weight = Wei; }} @Test Public void PassClass2() {Person person =NewPerson (175, $); System. out. println ("Before classoperation, the person height is"+ Person.height +" and the weight is"+ person.weight); ClassOperation2 (person); System. out. println ("After classoperation, the person height is"+ Person.height +" and the weight is"+ person.weight); } Public voidClassOperation2 ( Personperson) {person =NewPerson ( the, the); System. out. println ("In classoperation, the person height is"+ Person.height +" and the weight is"+ person.weight); }
Results
theis175andtheis140theis190andtheis160theis175andtheis140
Summarize
The last example is to illustrate a case where the stringoperation () in example 3 saw that the assignment of a string is actually equivalent to the new object in Example 5, ClassOperation2 ().
Iii. Summary
The above analysis is mainly based on the results to judge, may not be particularly accurate, hope to criticize.
Article Source: Http://blog.csdn.net/zhaodedong
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Learning notes: detailed values and citations