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

时间: 2024-12-18 06:57:17

01 String的引用传递的相关文章

Java中String是不是引用传递?

本文转自:http://www.jcodecraeer.com/a/chengxusheji/java/2012/0805/340.html 编者语:书上都说string是引用类型,但事实上我所看到的string和所谓的值类型没有什么区别,但通过看以下的文章,明白了: 1.string a="abc";之后,如果a="xy",则是a并没有改变内存中已经存在的"abc",而是又创建了另外一个实例.实际上相当于:string a=new String

Java中String的 "引用" 传递

1.来看一段有趣但又让人困惑的代码片段 public static void main(String[] args){ String x = new String("ab"); change(x); System.out.println(x); } public static void change(String x){ x = "cd"; } 打印结果:"ab" 2.这段代码真正做了什么呢?来解释一下这个过程 首先,当字符串"ab&q

java引用传递的基本应用

package com.cloud.day1; public class Demo1 { public static void main(String[] args) { // 引用传递的基本应用 Demo2 d2=new Demo2(); d2.temp=500; System.out.println("fun调用前:"+d2.temp); fun(d2); System.out.println("fun调用后:"+d2.temp); System.out.pri

java 中值传递和引用传递(转)

java中给函数传递参数的方式有两种:值传递和引用传递.一般而言,基本类型是值传递:引用类型是引用传递.但传值时到达发生了什么? 1.基本类型 8个基本类型(byte,short,int,long,float,double,char,boolean)是值传递. 1 public class ValueTest { 2 3 public static void main(String[] args) { 4 int a = 10; 5 changeVal(a); 6 System.out.prin

Java中String对象的传递解析

话不多说了,直接上代码. public class Demo { // static String str = "hello world!"; // static String str = new String("hello world!"); // static char[] ch = new char[]{'A','B','C'}; public static void test(String str,char[] ch,int a) { System.out.

由String作为方法参数,引起的值传递,引用传递,及StringBuffer

原文引用: http://www.cnblogs.com/zuoxiaolong/p/lang1.html http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html http://xueliang1yi.blog.163.com/blog/static/11455701620121140330271/ 一. 最开始的示例写代码最重要的就是实践,不经过反复试验而得出的说辞只能说是凭空遐想罢了.所以,在本文中首先以一个简单示例来抛出核心

String作为方法参数传递 与 引用传递

String作为方法参数传递 String 和 StringBuffer的区别见这里: http://wenku.baidu.com/view/bb670f2abd64783e09122bcd.html 一. 最开始的示例写代码最重要的就是实践,不经过反复试验而得出的说辞只能说是凭空遐想罢了.所以,在本文中首先以一个简单示例来抛出核心话题: public class StringAsParamOfMethodDemo { public static void main(String[] args

JAVA随笔篇二(深入分析JAVA简单类型、String和对象的值传递和引用传递)

关于JAVA的值传递和引用传递,翻看了很多资料和博客,感觉大多数讲的很乱,都是自己明白了之后就不讲了的样子,终于算是比较理解这几个概念了,下面做一个总结. 1.简单类型的参数传递 Java方法的参数是简单类型的时候,是按值传递的 (pass by value).下面举一个经典的swap函数: 无法交换值的方法: package TestTransferPack; public class TestTransfer { public static void main(String[] args)

对String值不可变的理解以及String类型的引用传递问题

今天复习java时,突然注意到了一句以前没有注意过的一句话,String 是final修饰的,其值是不可变的.当时看的一脸懵逼,String str = "abc"; str = "abcde"这两行代码没有任何的错误的.上网百度了许久,还是整理下来防止以后忘记吧. 首先要理解的是,string说的不可变是其值不可变,比如String str = "abc",不可变值得是abc不可变,当我们str = "abcde"时,abc