Java String 值傳遞 VS 引用傳遞

來源:互聯網
上載者:User

標籤:指標   private   改變   logs   物件類型   print   java   bsp   rgs   

在Java代碼中,如果方法參數中有String類型,那麼這個參數是值傳遞還是方法傳遞呢?

基本類型容易理解,物件類型也容易理解,可偏偏String這個物件類型讓人有些疑惑。別著急,看完你就清晰了。

其實,我們大可不必糾結於文字遊戲,在Java中宣稱只有值傳遞,而在C++程式員來看要區分為值傳遞和引用/指標傳遞,實質上是一致的。

 

1. 對於基本類型,如int、char,大家都能理解,屬於值傳遞,一個方法中對於基本類型的修改,不會反饋到方法調用者。

    public static void main(String[] args)    {        int a = 111;        change(a);        System.out.println("in method main, a = " + a);    }        private static void change(int a)    {        a = 789;        System.out.println("in method change, a = " + a);    }

輸出:

in method change, a = 789in method main, a = 111

 

2. 對於物件類型,值傳遞,傳遞的是對象的地址,而不是對象的內容。所以,在方法內只要不修改對象的地址,那麼對象內容的修改就會反饋到方法調用者,因為方法內外見到的是同一個地址。

    public static void main(String[] args)    {        Student studentA = new Student();        studentA.age = 18;        change(studentA);        System.out.println("in method main, age = " + studentA.age);    }        private static void change(Student student)    {        student.age = 46;        System.out.println("in method change, age = " + student.age);    }        private static class Student    {        private int age;                /**         * 取得age          * @return 返回 age         */        public int getAge()        {            return age;        }                /**         * 設定age         * @param age 要設定的age         */        public void setAge(int age)        {            this.age = age;        }    }

輸出:

in method change, age = 46in method main, age = 46

 

3. 接著第2條說,如果參數是物件類型,但是在方法內,參數重新指向了別的對象,即參數的地址發生了變化。那麼對這個參數的修改將不會反饋到方法調用者,因為方法內外見到的不是同一個地址。

    public static void main(String[] args)    {        Student studentA = new Student();        studentA.age = 18;        change(studentA);        System.out.println("in method main, age = " + studentA.age);    }        private static void change(Student student)    {        student = new Student();        student.age = 46;        System.out.println("in method change, age = " + student.age);    }

輸出:

in method change, age = 46in method main, age = 18

 

4. String類型。為什麼String類型作為參數傳遞,修改String不能反饋到方法調用者呢?因為在Java中有個常量池,每個字串都對應常量池中的一個對象,也就是說,如果對String對象重新賦值,就相當於將String指向了另一個對象,地址發生了改變。下面兩端代碼是等效的。

代碼一:

    public static void main(String[] args)    {        String str = "111";        change(str);        System.out.println("in method main, str = " + str);    }        private static void change(String str)    {        str = "222";        System.out.println("in method change, str = " + str);    }

代碼二:

    public static void main(String[] args)    {        String str = new String("111");        change(str);        System.out.println("in method main, str = " + str);    }        private static void change(String str)    {        str = new String("222");        System.out.println("in method change, str = " + str);    }

輸出都一樣:

in method change, str = 222in method main, str = 111

Java String 值傳遞 VS 引用傳遞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.