• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

老王的蜕变

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

有关String的引用传递浅析

 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  

 

 
再来看一个例子

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值
 }
};

 

posted on 2013-04-18 15:20  老王的蜕变  阅读(222)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3