String的引用传递

一 引用传递的三个范例

范例一

package com.mtzsoft;


/**
 * 范例一
 * 
 * @author Administrator
 *
 */
public class Test1 {

    public static void main(String[] args) {
        
        Demo d1 = new Demo();
        d1.setTemp(50);
        
        System.out.println("fun1调用之前temp=" + d1.getTemp());
        fun1(d1);
        System.out.println("fun1调用之后temp=" + d1.getTemp());

    }

    public static void fun1(Demo d2) {
        d2.setTemp(100);
    }
}
class Demo{
    
    private int temp=0;

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }
    
}

 控制台打印结果:

 

 

 调用fun1前值为50,调用后为100,方法所修改的值被保存下来了,那么我们进行内存分析如下:

    * 引用传递(1)内存分析
         *
         *                 fun1(d1)   把d1的引用传递给d2  d2/d1共用内存空间
         *                 --------------                                                                       ---------------
         *                 |        栈         |                                                                     |           堆           |
         *                 ---------------                                                                      ---------------
         *                |       d1          |        -----------------------------------------> |     temp=50    |
         *                 ---------------                             ↑                                          ---------------
         *                |       d2          |    --------------------                                         
         *                 --------------                                                                         
         *                  fun1(d1)执行后,d2断开连接
         *                  --------------                                                                      ---------------
         *                 |        栈          |                                                                   |           堆      |
         *                 ---------------                                                                      ---------------
         *                |       d1          |        -----------------------------------------> |     temp=100|    d2/d1共用内存空间   d2修改temp的值
         *                 ---------------                             ↑                                        ---------------
         *                |       d2          |    ----------x---------                                     
         *                 --------------                               
         */

 

范例二

package com.mtzsoft;

/**
 * 范例二
 * 
 * @author Administrator
 *
 */
public class Test2 {

    public static void main(String[] args) {

        String str = "hello";

        System.out.println("fun2调用之前str=" + str);
        fun2(str);
        System.out.println("fun2调用之后str=" + str);
    }

    public static void fun2(String s) {
        s = "hello word";
    }
}

控制台打印结果:

 方法fun2调用前和调用后,str的值均为hello,那么String引用传递的内存分析:

        /**
         *
         * 引用传递(2)内存分析  (String的不可改变的特性)
         *
         *                 fun2(str)   把str的引用传递给s
         *                 --------------                                                                       ---------------
         *                |        栈            |                                                                  |           堆      |
         *                 ---------------                                                                      ---------------
         *                |       str          |        ----------------------------------------->|     "hello"        |
         *                ---------------                                             ↑                         ----------------
         *                |       s             |        -------------------------
         *                --------------                                                                         
         *                    
         *                 s="hello word"  开辟新内存空间           
         *                 --------------                                                                        -----------------
         *                |        栈          |                                                                     |           堆        |
         *                 ---------------                                                                      ------------------
         *                |       str          |        ----------------------------------------->|       "hello"         |
         *                ---------------                                         ↑x 断开                     ------------------
         *                |       s             |        ------------------------- ---------------> |    "hello word" |
         *                --------------                                                                         -------------------
         *                                        
         *                  fun2(str)执行后
         *                 --------------                                                                       -----------------
         *                 |        栈         |                                                                    |             堆       |
         *                 ---------------                                                                      ------------------
         *                |       str          |        ----------------------------------------->|       "hello"        |
         *                --------------                                                                         ------------------
         *                |       s             |        ------------------------- ---------------> |    "hello word"   |
         *                  --------------                                                                       -------------------
         *
         */

 

范例三

 

package com.mtzsoft;

/**
 * 范例三
 * 
 * @author Administrator
 *
 */
public class Test3 {

    public static void main(String[] args) {

        TestString t = new TestString();
        t.setTemp("hello");

        System.out.println("fun3调用之前temp=" + t.getTemp());
        fun3(t);
        System.out.println("fun3调用之后temp=" + t.getTemp());
    }

    public static void fun3(TestString ts) {
        
        ts.setTemp("hello word");
    }
}

class TestString {

    private String temp = "";

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

}

 

控制台打印结果:

 这里被修改的值保留下来了,与范例一相同,引用传递内存分析:

        /**
         *
         * 引用传递(3)内存分析
         *
         *                 fun3(t)   把t的引用传递给t1
         *                 --------------                                                                       ----------------------------
         *                 |        栈         |                                                                    |                堆                   |
         *                 ---------------                                                                      -----------------------------
         *                |       t             |        ----------------------------------------->|       temp= "hello"             |
         *                ---------------                                             ↑                          -----------------------------
         *                |      ts             |        -------------------------
         *                 ----------------                                                                     
         *                    
         *                     t1.temp= "hello word";
         *                 --------------                                                                        -------------------------------
         *                |        栈          |                                                                     |                          堆              |
         *                 ---------------                                                                      -------------------------------
         *                |       t             |        ----------------------------------------->  |    temp= "hello word"           |
         *                ----------------                                        ↑x 断开                     -------------------------------
         *                |       ts           |        ----------------------
         *                -----------------                                                                   
         *
         */

 

通过三道引用传递的分析:范例一与范例二是完全一样的,只是第二个范例体现了String类的内容不可改变的特性。

 

转载于:https://www.cnblogs.com/meitanzai/p/5831788.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值