• 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类的内容不可改变的特性。

  • 相关阅读:
    android 自定义dialog 易扩展
    android 圆角item shape
    模板方法模式
    观察者模式
    工厂方法模式(选自《设计模式之禅》)
    单例模式
    如何快速创建静态WEB站点
    React Native 插件系列之lottie-react-native
    JavaJavaScript小问题系列之JSON解析
    React Native 插件系列之PushNotificationIOS
  • 原文地址:https://www.cnblogs.com/meitanzai/p/5831788.html
Copyright © 2020-2023  润新知