The string is passed by value, and the Java parameter is a pass value

Source: Internet
Author: User

is the object in Java passed as a parameter to a method, whether it is a value pass or a reference pass?

are string and int parameter passes passed by value or by reference?

An interview topic, string delivery:

public string Change (string s) {     s = "222";     return s;  } public static void Main (stirng[] args) {    String s = "111";      Change (s);    Sout (s);}

I see the topic Leng a bit, originally not false thinking of the result is 111, but think carefully, string is the object type, the object is passed the address, then the address is passed into the method inside, will point to change to 222, then the result should be 222. The opposite is true.

The Java programming language has only value-passing parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a copy of the object's reference. Point to the same object, the contents of the object can be changed in the called method, but the object's reference (not the referenced copy) is never changed.

The Java pass parameter is both a value and, if it is an object, a copy of the referenced value to the method as a parameter. If the object in the heap is modified by reference, the object is actually modified, but it is not modified by the reference that created the assignment, it is a copy of the copied reference in the method. In other words, the spouses of Schwarz was kissed by a clone of Schwarz.

Use examples to understand, in fact, this understanding is based on the results of the JDK told me to remember the rules are like this, remember later.

  public string Change (string s, Int. I, StringBuffer SB, person p) {        s= "123";        i=3;        Sb.append ("Woshi");        P.setage (+);        SB = new StringBuffer ("SBSB");        p = new Person ("BB", "$");        return s;    }    @Test public    void Testchange () {        StringBuffer sb = new StringBuffer ("Buff");        String s = "AAA";        int i = 1;        Person p = new person ("AA", n);        i=2;        Change (s,i,sb,p);//        s= "222";        System.out.println (s);        System.out.println (i);        System.out.println (Sb.tostring ());        SYSTEM.OUT.PRINTLN (P);    }

A total of String,int was tested here, an object StringBuffer, an object people. Let's take a closer look at what happens to these passes. I think a large part of the people can not guess the print results.

Aaa2buffwoshiperson{id=0, name= ' AA ', age=100, Country=null, hashcode=638783031}

Let's analyze each of them.

the first is string.
String s = "AAA";

Here, the JVM creates a variable reference s, creates an object AAA in the heap, and puts the AAA into the constant pool. S points to AAA.

And then it's in the change method. This is understood here: Pass a copy of S reference to the method change. So the change has a variable s, and this s also points to AAA. So let's go through debug and see what happens later.

1.s points to AAA:

2.S when running into the change method

Then look at the time when S is re-assigned:

Then we run the end of the change method into the Main method:

The end of the S is here. So if we follow the reference to pass the variable s, that is, string s= "AAA" in this s itself, then, s itself is a variable, s point to AAA, in the method change in the S and point to 123, back to the main method of the S variable point is changed? Wrong! Obviously S is still AAA, then only this understanding: s passed to the method, copied the address of S pointed to Change,change S is another s,s point aaa (@718), and then in the change S point to 123 (@731), Since string is an immutable class (final and immutable), here only the point of the copy S is changed to 731, the object in the original address 718 does not change because the string is immutable. Then, back to the main method, the S variable itself does not change, s still point to address 718,718 is the content of AAA. So the final printing AAA.

And then the StringBuffer.

int is the basic type, so int just copies a copy of the value to another method, which we all know, will not be tested. Now look at the changes that have taken place in StringBuffer.

1. Initialize:

2. In the Change method:

3. Occurrence of Append

4. Point to the new object

Here's the point. The copy points to the new object. Like, Schwarz the clone of the person to find another woman as a wife, and the real Schwarz wife did not change.

5. Return to the Main method:

Here, the StringBuffer is over. We must know that, although we did not study how the source code is implemented, the change method gets a copy of SB, except that the copy points to 708, and in the change the 708 object is appended, and 708 of the objects are really changed. Then the SB copy points to the new address 737. This only modifies the address that the copy points to, and if you print sb.tostring () Here, the content is printed in 737. When you jump out of the change, back to the main method, the original SB still points to 708, and ultimately the result of printing 708. Unlike string, StringBuffer results in a variable because StringBuffer is variable and can be append. And string is immutable, s=123 in change is two behavior, one is to find whether there is a constant pool of 123, if not in the heap to create 123, One is to point S to 123. That is, it is time to create a new string object instead of modifying the original string object s content. So, when we go back to the main method, S is still AAA.

In the same vein, look at the object you created people

1. Initialize:

When the 2.P passed into the change,

3.P Copy Settings Age

4.P Copy re-assignment

Here we still have to say that the P copy modifies its point, and does not affect the direction of p in the Main method. The direction of P in the main method has not changed, and it should still be 720.

5. Back to the Main method

Summarize:

Through the above study of String,stringbuffer,people, should understand a truth, important words three times, important rules I have demonstrated three times. If you follow the step by step, it must be remembered:

All Java parameter passing is a copy of the passed copy of the value represented by the variable! All Java parameter passing is a copy of the passed copy of the value represented by the variable! All Java parameter passing is a copy of the passed copy of the value represented by the variable!

What must be remembered here is the concept of replicas. In the method, the thread that runs to this place takes a copy of the passed parameters to the workspace and changes the value of the copy in the workspace. The final change is the copy, and if the point is changed by pointing to the copy, then the content in that point does change. If you modify the point of the copy, that is, the copy is re-assigned value, then the original variable what? The meta variable still points to the original address.

Then, string passes past the copy, modifies the point of the copy, and the print element string does not change because the copy does not have the ability to modify the final string class.

The string is passed by value, and the Java parameter is a pass value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.