Value passing in Java and "reference passing"

Source: Internet
Author: User


First, Java does not have a reference to pass such a saying.

Java has only a value pass . The value is passed, and the base data type is passed the value. The reference type passes the address value.

I'll take a look at it. This is like the rice noodles and rice together, and then mixed with fans of the relationship.

OK, let's take a look at the first example:

public static void Main (string[] args) {int a = 1;int B = 4; System.out.println ("before:a=" + A + "," + "b=" + b); swap (A, b); System.out.println ("After  : a=" + A + "," + "b=" + b);} private static void swap (int a, int b) {int temp = A;a = B;b = temp;}

Results:

Before:a=1,b=4after  : a=1,b=4

There's nothing to say , the A and B in the main function have nothing to do with a A or a-half wool in the swap () function, only the local variables in the swap function, and do not affect A and B in the main function.

2, the second one:

public static void Main (string[] args) {integer a = new integer (3); integer b = new integer (4); System.out.println ("before:a =" + A + "   B =" + b); swap (A, b); System.out.println ("after  : a =" + A + "   B =" + b);} public static void Swap (integer a1, Integer b1) {Integer temp = A1;A1 = B1;B1 = temp;}



Results:

Before:a = 3   b = 4After  : a = 3   b = 4

Very many people may be surprised. Why is it like that? Why is it that the wood is exchanged?

1. A pass the reference value to A1,b to pass the reference value to B1, at which point both A1 and a are pointing to 3 of the space. B1 and B both point to 4 of the space

2. In the swap function. The reference value of A1 and B1 has been exchanged, note is the reference value !

At this point A1 point to 4,b1 Point 3

3, then look at the print statement:

System.out.println ("after  : a =" + A + "   B =" + B);
what does it have to do with a1,b1 that the values of A and B are printed? The only exchange is the reference value of A1 and B1, and the A1,B1 is a A/b reference copy. There are four references pointing to 2 address spaces. exchanged two of them. Does not affect the value of the other two references.

All right, that's all. Let's look at another example:

First define a class yourself:

public class Person {private int age;public int getage () {return age;} public void Setage (int.) {this.age = age;}}


public static void Main (string[] args) {person p = new person (); System.out.println ("Before:" +p.getage ()); Changepersonage (P); System.out.println ("After:" + p.getage ());} public static void Changepersonage (person p) {P.setage (10);}
Results:

Before:0after:10

is not very wonderful, here can actually change the value.

Note: Here p passes the reference copy to the Changepersonage () function, the same as there are two references to the person object in the heap space at this point. Then the Setage () function is called in the Changepersonage () method, which in essence changes the value of age in the person object, where the object is changed rather than simply the passing of the reference value.



Let's look at another example:

public static void Main (string[] args) {String str = "Test"; System.out.println ("before =" +str); changestr (str); System.out.println ("After  =  " + str);} public static void Changestr (String str) {str + = "haha";}

Results:

before = Testafter =  Test

Oh I go, why can't I change?

In the main method, str first points to "test" in the constant pool, and then passes the reference copy to Str in the Changestr method. The same here there are two references, but in the Changestr method made, str+= "haha", the operation. Attention. At this point, str in CHANGESTR no longer points to "test", but point to "Testhaha", two references to different spaces, print out of course no change in value.



At last. Let's look at a sample and stabilize it.

public static void Main (string[] args) {StringBuffer sb = new StringBuffer ("Test2"); System.out.println ("Before   =" + SB); Changestr (SB); System.out.println ("After    =" + SB);} private static void Changestr (StringBuffer sb) {sb.append ("haha");}

Results:

Before   = Test2after    = Test2haha

The change in value here has caused no fuss. This is essentially similar to the example of the person object above.

SB passes the reference copy to the Changestr method. The Append method is then called in the Changestr method. is essentially the value of changing the space that the two references collectively point to. Not a reference is passed or the reference points to another space.



Finally spit the trough, very hot ah.



Value passing in Java and "reference passing"

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.