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