Value Transfer and reference transfer in Java"

Source: Internet
Author: User


First, Java does not reference and pass this.

Java only supports value passing, All passed values,The value is transmitted for the basic data type, and the address value is transmitted for the reference type.

Let me take a look at the relationship between rice noodles and fans.

Okay. Let's 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;}

Result:

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

There is nothing to say about this. The a and B in the main function have no relationship with the and B semi-wool in the swap () function. Instead, they only exchange partial variables in the swap function, it does not affect a and B in the main function.

2. Second:

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;}



Result:

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

Many may be surprised. Why does it look like it? Why is there an exchange between trees.

1. a passes the reference value to a1 and B passes the reference value to b1. At this time, both a1 and a point to the space of 3, and b1 and B point to the space of 4.

2. In the swap function, the reference values of a1 and b1 must be referenced! A1 points to 4, b1 points to 3

3. Let's look at the print statement:

System.out.println("After  : a = " + a + "   b = " + b);
What is the relationship between printed values a and B and a1 and b1? The exchange is only the reference values of a1 and b1. To put it bluntly, a1 and b1 are the reference copies of a and B. Here, four references point to two address spaces. If two of them are exchanged, the values of the other two references are not affected.

Okay, that's all. Let's look at an example:

First, define a class:

public class Person {private int age;public int getAge() {return age;}public void setAge(int age) {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);}
Result:

Before : 0After : 10

Isn't it amazing? Here we can change the value again.

Note: Here, p will pass the reference copy to the changePersonAge () function. There are also two references pointing to the Person object in the heap space. In this case, the setAge () function is called in the changePersonAge () method, which actually changes the age value in the Person object. Here, the object is changed, rather than passing the reference value.



Let's look at an 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";}

Result:

Before = testAfter =  test

Why can't I change it?

In the main method, str first points to the "test" in the constant pool, and then passes the reference copy to the str in the changeStr method. There are also two references here, but they are made in the changeStr method, str + = "haha"; action. Note that str in changeStr no longer points to "test", but to "testhaha". The two references point to different spaces, the printed value does not change.



Finally, let's take a look at a small example.

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");}

Result:

Before   = test2After    = test2haha

The value changed here won't cause everyone's fuss. It's basically similar to the Person object example above.

Sb passes the reference copy to the changeStr method, and then calls the append method in the changeStr method, which is essentially changing the value of the space that the two references point to together, it is neither a reference transfer nor a reference pointing to another space.



Finally, let's talk about it. It's so hot.

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.