About Java referenced and value variables

First, the difference between the reference variable and the value of a variable

  1. The value of the data types are stored in the stack are.

  2. The reference type, which is the variable data (data which is an address value) is stored in the stack , the reference type is really data stored in the stack are.

  3. Eight basic data types - type values, stored in the stack in. Other data types (String, arrays, objects ......) - reference types, stored in the stack in.

 

 

 

Second, the value and reference types of calls in the process

  1. If a value type passed as actual parameters of the method, the parameter exchange methods will not affect the change in the parameter itself.

 1 public class Example01 {
 2 
 3     public static void main(String[] args) {
 4         int x=10;
 5         int y=20;
 6         System.out.println("x:"+x+" y:"+y);
 7         exchange(x,y);
 8         System.out.println("x:"+x+" y:"+y);
 9 
10     }
11     public static void exchange(int x,int y){
12         int tmp=x;
13         x=y;
14         y=tmp;
15         System.out.println("x:"+x+" y:"+y);
16     }
17 }

   result:

 

 

     2. With reference types as the actual parameter passing, passing parameters by value or nature, but this value is referenced memory address only. Parameter changes will affect the itself.

 1 public class Example01 {
 2 
 3     public static void main(String[] args) {
 4         
 5         Score score=new Score();
 6         score.xx=10;
 7         score.yy=20;
 8         System.out.println("xx:"+score.xx+" yy:"+score.yy);
 9         exchange(score);
10         System.out.println("xx:"+score.xx+" yy:"+score.yy);
11     }
12     public static void exchange(Score score){
13         int tmp=score.xx;
14         score.xx=score.yy;
15         score.yy=tmp;
16         System.out.println("xx:"+score.xx+" yy:"+score.yy);
17     }
18 }

  result:

 

 

 

Picture Resolution:

 

 

 

 

 

     3. The typical intellectual error demonstration

 1 public class Example01 {
 2 
 3     public static void main(String[] args) {
 4         
 5         String x="I love you";
 6         String y="I hate you";
 7         System.out.println("x:"+x+"     y:"+y);
 8         exchange(x, y);
 9         System.out.println("x:"+x+"     y:"+y);
10         
11     }
12     public static void exchange(String x,String y){
13         String tmp=x;
14         x=y;
15         y=tmp;
16         System.out.println("x:"+x+"     y:"+y);
17     }
18 }

 

   result:

 

   Wondering: Why here obviously is a reference type, but it still did not affect the main function of the change in the value of the argument it?

   Analysis: confused because this concept, in fact, Java All values are transmitted , this code x, y address value is stored in the swap function exchange (), the parameter x, y exchange value, i.e. exchanged address value. But there is no real exchange of data.

  When the function execution is completed, return to the main function, the failure parameter, and x, y address value is not stored in the exchange, and the positive value is really no data exchange, so nothing has changed.

 

Guess you like

Origin www.cnblogs.com/Unlimited-Rain/p/12458931.html