1 /以下内容来源于:《java开发实战经典》书中
 2 //首先看一个例子
 3 public class RefDemo02{
 4  public static void main(String args[]){
 5   String str1 = "hello" ;   // 实例化字符串对象
 6   System.out.println("fun()方法调用之前:" + str1) ;
 7   fun(str1) ;      // 调用fun()方法
 8   System.out.println("fun()方法调用之后:" + str1) ;
 9  }
10  public static void fun(String str2){  // 此处的方法由主方法直接调用
11   str2 = "MLDN" ;     // 修改字符串内容
12  }
13 };
14 //运行结果://  fun()方法调用之前:hello
15 //  fun()方法调用之后:hello
16  

 

有关String的引用传递浅析

 
再来看一个例子

class Demo{
 String temp = "hello" ;  // 此处为了方便,属性暂时不封装
};
public class RefDemo03{
 public static void main(String args[]){
  Demo d1 = new Demo() ; // 实例化Demo对象,实例化之后里面的temp=30 
  d1.temp = "world" ;  // 修改temp属性的内容
  System.out.println("fun()方法调用之前:" + d1.temp) ;
  fun(d1) ;
  System.out.println("fun()方法调用之后:" + d1.temp) ;
 }
 public static void fun(Demo d2){  // 此处的方法由主方法直接调用
  d2.temp = "MLDN";      // 修改temp值
 }
};

 

有关String的引用传递浅析

相关文章:

  • 2022-12-23
  • 2021-05-25
  • 2021-11-19
  • 2021-04-23
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2021-06-20
  • 2022-12-23
相关资源
相似解决方案