Value passing and reference passing for Java

Source: Internet
Author: User

First look at a piece of code:

User class:

1  Public classUser {2     PrivateString name;3 4      PublicString GetName () {5         returnname;6     }7 8      Public voidsetName (String name) {9          This. Name =name;Ten     } One  A}

Test class:

1  Public classDemo1 {2      Public Static voidMain (string[] args) {3User user=NewUser ();4List<user> list=NewArraylist<user>();5 list.add (user);6User.setname ("John Doe");7System.out.println (List.get (0). GetName ());8     }9}

According to their own understanding to get the results of their own view of the output, to see if and the next print out the results are consistent.

Program Run Result:

Here, we'll talk about value-passing and reference-passing concepts in Java:

Value passing: (formal parameter type is the basic data type): When the method is called, the actual parameter passes its value to the corresponding formal parameter, the formal parameter only initializes its own storage unit content with the value of the actual parameter, it is two different storage units, so the change of the formal parameter value in the method execution does not affect the value of the actual parameter.

Reference passing: (formal parameter type is a reference data type parameter): Also known as a pass-through address. Method invocation, the actual parameter is an object (or an array), then the actual parameters and formal parameters point to the same address, in the execution of the method, the operation of the formal parameters is actually the operation of the actual parameters, the result is preserved after the end of the method, so the change in the method execution parameters will affect the actual parameters.

Based on the above concepts, let's write a few demos together to really realize the difference between value passing and reference passing in Java:

Pass by value : refers to the passed parameter is passed as a copy of the value when the method is called. Demo

1  Public classTemptest {2     Private voidTest1 (inta) {3         //do something.4     }5      Public Static voidMain (string[] args) {6Temptest T =Newtemptest ();7         intA = 3;8T.test1 (a);//The parameter a passed here is passed by value9     }Ten } One             

Passing important characteristics by value: Passing a copy of a value, that is, passing it off is unrelated. Demo

1  Public classTemptest {2     Private voidTest1 (inta) {3A = 5;4System.out.println ("A= in Test1 method" +a);5     }6      Public Static voidMain (string[] args) {7Temptest T =Newtemptest ();8         intA = 3;9T.test1 (a);//after passing, the Test1 method changes the value of the variable without affecting the ATenSystem.out.println ("a= in the Main method" +a); One     } A } -                     

The operating result is:

Pass by reference : refers to the argument passed by reference when a method call is passed, in fact, the address of the reference that is passed, that is, the address of the memory space corresponding to the variable. Demo

1  Public classTemptest {2     Private voidTest1 (A a) {3     }4      Public Static voidMain (string[] args) {5Temptest T =Newtemptest ();6A A =NewA ();7T.test1 (a);//The parameter a passed here is passed by reference.8     }9 }Ten classa{ One      Public intAge = 0; A}

The important feature of passing by reference is the reference to the value, meaning that both before and after delivery point to the same reference (that is, the same memory space). Demo

1   Public classTemptest {2      Private voidTest1 (A a) {3A.age = 20;4System.out.println ("age= in Test1 method" +a.age);5      }6       Public Static voidMain (string[] args) {7Temptest T =Newtemptest ();8A A =NewA ();9A.age = 10;Ten T.test1 (a); OneSystem.out.println ("age= in the Main method" +a.age); A      } -  } -  classa{ the       Public intAge = 0; -}

The operating result is:

I believe you have seen the above examples and instructions, has been on the value of the transfer and the reference to pass a further understanding, and finally summed up!

Description

(1): "In Java parameter passing is passed by value" This sentence means: Pass by value is a copy of the value passed, by reference is actually passed the value of the referenced address, so collectively by value passed.

(2): There are only basic types in Java and strings that are defined in the following way are passed by value, others are passed by reference. is to define the string directly using double quotation marks: string str = "Java private";

Value passing and reference passing for Java

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.