Java,java官網下載

來源:互聯網
上載者:User

Java,java官網下載

對於賦值操作”=”, 基礎資料型別 (Elementary Data Type)儲存了實際的值,
而”將一個對象賦值給另一個對象”, 是將引用從一個地方複製到另一個地方.
對象的這種現象就是“別名現象”.
樣本:

//: Main.javaclass FloatType {    float f;}/** * 別名機制樣本 */public class Main {    public static void main(String[] args) {        FloatType f1 = new FloatType();        FloatType f2 = new FloatType();        f1.f = 4; f2.f = 8;        System.out.println("f1.f = " + f1.f + ", f2.f = " + f2.f);        f1.f = 16; f2.f = 32;        System.out.println("f1.f = " + f1.f + ", f2.f = " + f2.f);        f1 = f2; // 賦值操作        f2.f = 64; // 別名現象, f1.f的值也被修改        System.out.println("f1.f = " + f1.f + ", f2.f = " + f2.f);    }}/** * Output: * f1.f = 4.0, f2.f = 8.0 * f1.f = 16.0, f2.f = 32.0 * f1.f = 64.0, f2.f = 64.0 *///:~

方法調用中, 將一個對象作為參數傳遞給方法, 其實傳遞引用, 也會產生別名現象.

//: Main.javaclass FloatType {    float f;}/** * 方法調用別名機制 */public class Main {    static void change(FloatType of) {        of.f = 32;    }    public static void main(String[] args) {        FloatType of = new FloatType();        of.f = 4;        System.out.println("of.f = " + of.f);        change(of); // 方法調用的引用傳遞        System.out.println("of.f = " + of.f);    }}/** * Output: * of.f = 4.0 * of.f = 32.0 *///:~

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.