java-value passing and reference passing

Source: Internet
Author: User
Tags stringbuffer

  

Objects are never passed in a Java application, and only object references are passed . Therefore, the object is passed by reference. The fact that Java applications pass objects by reference does not imply that Java applications pass parameters by reference. Parameters can be object references, while Java applications pass object references by value .

A variable in a Java application can be one of the following two types: a reference type or a base type. When passed as a parameter to a method, the two types are handled the same way. Both types are passed by value, and no one is passed by reference.

 Java actually only has value passing, and there is no real reference passing.

  passing by value means that when a parameter is passed to a function, the function receives a copy of the original value . Therefore, if the function modifies the parameter, only the copy is changed, and the original value remains unchanged.

  passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value instead of a copy of the value. Therefore, if the function modifies the parameter, the original value in the calling code changes accordingly.

1, the object is passed by reference;
2. Java application has only one kind of parameter passing mechanism, namely passing by value;
3, passing by value means that when a parameter is passed to a function, the function receives a copy of the original value;
4. Passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value instead of a copy of the value.

① Reference Delivery

Package Quotedemo;public class Test1 {public static void main (string[] args) {stringbuffer S1 = new StringBuffer ("good"); StringBuffer s2 = s1;s2.append ("Morning"); System.out.println (S1);}}

In this sentence of the above procedure:

StringBuffer s2 = S1;

Causes the object S1 and object S2 to point to the same address in memory, thus pointing to the same object.

Results of the operation:

Good morning

Here is the reference pass .

When an object assignment is performed, a reference to the object is passed, so the object is a reference pass, but actually the passing object here is actually a value pass . Because an object is a pointer, this assignment is an assignment between pointers, and in Java the passing of such an object is called a reference pass .

② Value passing

Package Quotedemo;public class Test2 {public static void main (string[] args) {int i1 = 5;int I2 = I1;i2 = 6; System.out.println ("I1 =" + I1); System.out.println ("i2 =" + I2);}}

Output Result:

I1 = 5i2 = 6

The original data type is passed by value, and this by-pass also refers to the next problem when assigning a value: Java applications have only one parameter passing mechanism, that is, passing by value.

For this sentence:

I2 = I1;

Gets the copy, so the subsequent operations are operations on the copy, not the i1 operation.

③ more complex reference passing

Package Quotedemo;class test12{public static void Main (string[] args) {stringbuffer s= new StringBuffer ("good"); StringBuffer s2=new StringBuffer ("bad"); test (S,S2); System.out.println (s);//9system.out.println (S2);//10}static void Test (stringbuffer s,stringbuffer S2) { System.out.println (s);//1system.out.println (S2);//2s2=s;//3s=new stringbuffer ("new");//4//s2=s;// 3system.out.println (s);//5system.out.println (S2);//6s.append ("hah");//7s2.append ("hah");//8// SYSTEM.OUT.PRINTLN (s2);}}

Results of the output:

Goodbadnewgoodgoodhahbad

Note that in the test function, the first two lines of output s and S2 are still the original s and S2 values, but when the S2 = s is executed, S2 points to the original S, and the next s = new StringBuffer ("new"), the S will point to the local "new", Then the output s and S2 are new and good (because S2 actually points to the original s), so the S2.append ("hah") that was executed afterwards is actually a "hah" behind the original S.

To make a slight change, let S point to the new string before calling S2 = S.

Package Quotedemo;class test12{public static void Main (string[] args) {stringbuffer s= new StringBuffer ("good"); StringBuffer s2=new StringBuffer ("bad"); test (S,S2); System.out.println (s);//9system.out.println (S2);//10}static void Test (stringbuffer s,stringbuffer S2) { System.out.println (s);//1system.out.println (S2);//2//s2=s;//3s=new stringbuffer ("new");//4s2=s;// 3system.out.println (s);//5system.out.println (S2);//6s.append ("hah");//7s2.append ("hah");//8system.out.println ( s2);}}

Results of the output:

Goodbadnewnewnewhahhahgoodbad

  

Other examples are:

Package quotedemo;class message{private int num = 10;public Message (int num) {this.num = num;} public void setnum (int num) {this.num = num;} public int Getnum () {return num;}} public class Testdemo {public static void main (string[] args) {message msg = new Message (+); fun (msg); System.out.println (Msg.getnum ());} public static void Fun (Message temp) {temp.setnum (100);}}

The actual execution process is as follows:

Java applications have only one parameter passing mechanism, which is passed by value.

java-value passing and reference passing

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.