Object-oriented reference delivery

Source: Internet
Author: User

Reference delivery plays an important role in Java. Here are three examples:

Example one:

Class Demo{int temp=30;//Here for convenience, not encapsulated};p ublic class Test{public static void Main (String args[]) {Demo d1=new demo ();// Instantiate the Demo object, temp=30d1.temp= 50 after instantiation,//Modify the contents of the Temp property System.out.println (Before the "fun () method call:" +d1.temp); fun (D1); System.out.println (After the "fun () method call:" +d1.temp);} public static void Fun (Demo D2) {//method here is called directly by the Main method d2.temp = 1000;//modifies the Temp value}}
Memory Analysis Diagram:

Example two:

public class Test{public static void Main (string args[]) {string str1= "Hello"; System.out.println ("Fun () method call before:" +STR1); fun (STR1); System.out.println (After the "fun () method call:" +STR1);} public static void Fun (String str2) {//method here is called directly by the Main method str2= "world";//Modify string Contents}}
Memory Analysis Diagram:


Example three:

Class demo{string temp= "Hello";}; public class Test{public static void Main (String args[]) {Demo d1=new demo ();//Instantiate Demo object, instantiate temp= "Hello" d1.temp= "World" ";//Modify Temp Property content    System.out.println (" Fun () method call before: "+d1.temp); fun (D1); System.out.println (After the "fun () method call:" +d1.temp);} public static void Fun (Demo D2) {//Here method calls d2.temp= "!!!" directly by the main method; /Modify Temp Value}}

Memory Analysis Diagram:


Example one is exactly the same as the flow of example three, and the second example is special because the string class is a special class whose contents are immutable.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Object-oriented reference delivery

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.