java数据传递例子+内存分析

一、引用传递

1、例子1

package com.jikexueyuan.ref;

class Ref1{
    int temp = 10;
    
}

public class RefDemo01 {
    public static void main(String args[]){
        Ref1 r1 =new Ref1();
        r1.temp = 20;
        System.out.println(r1.temp);
        tell(r1);
        System.out.println(r1.temp);
    }
    public static void tell(Ref1 r2){
        r2.temp = 30;
    }

}

20
30

2、例子2
 1 package com.jikexueyuan.ref;
 2 
 3 public class RefDemo02 {
 4 
 5     public static void main(String[] args) {
 6         String str1 = "Hello";
 7         System.out.println(str1);
 8         tell(str1);
 9         System.out.println(str1);
10     }
11     public static void tell(String str2){
12         str2="jike";
13     }
14 
15 }

Hello
Hello


 1 package com.jikexueyuan.ref;
 2 
 3 class Ref2{
 4     String temp = "hello";
 5 }
 6 
 7 public class RefDemo03 {
 8     public static void main(String args[]){
 9         Ref2  r1 = new Ref2();
10         r1.temp="jike";
11         System.out.println(r1.temp);
12         tell(r1);
13         System.out.println(r1.temp);
14     }
15 
16     public static void tell(Ref2  r2){
17         r2.temp="xueyuan";
18     }
19 }
jike
xueyuan


原文地址:https://www.cnblogs.com/linjulin/p/5959458.html