国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[Interview] Pass-by-value vs. Pass-by Reference

Lowky / 667人閱讀

Java is pass-by-value.

Pass by value: make a copy in memory of the actual parameter"s value that is passed in.

Pass by reference: pass a copy of the address of the actual parameter.

This code will not swap anything:

void swap(Type a1, Type a2) {
    Type temp = a1;
    a1 = a2;
    a2 = temp;
}

For this code, since the original and copied reference refer the same object, the member value gets changed:

class Apple {
    public String color = "red";
}
public class main {
    public static void main(String[] args) {
        Apple a1 = new Apple();
        System.out.println(a1.color); //print "red"
        changeColor(a1);
        System.out.println(a1.color); //print "green"
    }
    public static void changeColor(Apple apple) {
        apple.color = "green";
    }
}

Java does manipulate objects by reference, and all object variables are references. However, Java doesn"t pass method arguments by reference; it passes them by value.

Some more examples
public class Main {
    public static void main(String[] args) {
        Student s = new Student("John");
        changeName(s);
        System.out.printf(s); // will print "John"
        modifyName(s);
        System.out.printf(s); // will print "Dave"
    }
    public static void changeName(Student a) {
        Student b = new Student("Mary");
        a = b;
    }
    public static void modifyName(Student c) {
        c.setAttribute("Dave");
    }
}
public static void changeContent(int[] arr) {
   // If we change the content of arr.
   arr[0] = 10;  // Will change the content of array in main()
}

public static void changeRef(int[] arr) {
   
   arr = new int[2];  // If we change the reference
   arr[0] = 15; // Will not change the array in main()
}

public static void main(String[] args) {
    int [] arr = new int[2];
    arr[0] = 4;
    arr[1] = 5;
    changeContent(arr);
    System.out.println(arr[0]);  // Will print 10..
    changeRef(arr);
    System.out.println(arr[0]);  // Will still print 10.. 
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/65062.html

相關(guān)文章

  • Java Interview Questions (1)

    What is Java? Java is a high-level platform-independent object oriented programming language. List some features of Java? Object Oriented, Platform Independent, Multi-threaded, Interpreted, Robust, pa...

    xuxueli 評(píng)論0 收藏0
  • 【譯】十個(gè)刁鉆的 Java 面試題

    摘要:原文地址這里列出了十個(gè)常見(jiàn)而又刁鉆的開(kāi)發(fā)人員面試題及答案,這些題目是我從上找來(lái)的。如果你是初中級(jí)開(kāi)發(fā)人員,而且近期準(zhǔn)備面試的話,這些題目可能對(duì)你有些幫助。成員即沒(méi)有訪問(wèn)修飾符的成員可以在當(dāng)前包下的所有類(lèi)中訪問(wèn)到。 原文地址:https://dzone.com/articles/10... 這里列出了十個(gè)常見(jiàn)而又刁鉆的 Java 開(kāi)發(fā)人員面試題及答案,這些題目是我從 StackOverf...

    xuhong 評(píng)論0 收藏0
  • 理解引用

    摘要:我會(huì)解釋里面神秘的引用,一旦你理解了引用,你就會(huì)明白通過(guò)引用來(lái)了解的綁定是多么輕松,你也會(huì)發(fā)現(xiàn)讀的規(guī)范容易得多了。二理論把引用定義成。看看運(yùn)算符的說(shuō)法這也就是為什么我們對(duì)一個(gè)無(wú)法解析的引用使用操作符的時(shí)候并不會(huì)報(bào)錯(cuò)。 Know thy reference (原文:know thy reference - kangax) 一、前言 翻譯好不是件容易的事兒,我盡量講得通順,一些術(shù)語(yǔ)會(huì)保留原...

    curlyCheng 評(píng)論0 收藏0
  • 快速掌握J(rèn)avaScript面試基礎(chǔ)知識(shí)(一)

    摘要:根據(jù)調(diào)查,自年一來(lái),是最流行的編程語(yǔ)言。在一個(gè)函數(shù)體中聲明的變量和函數(shù),周?chē)淖饔糜騼?nèi)無(wú)法訪問(wèn)。也就是說(shuō)被大括號(hào)包圍起來(lái)的區(qū)域聲明的變量外部將不可訪問(wèn)。一個(gè)常見(jiàn)的誤解是使用聲明的變量,其值不可更改。 譯者按: 總結(jié)了大量JavaScript基本知識(shí)點(diǎn),很有用! 原文: The Definitive JavaScript Handbook for your next developer ...

    acrazing 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<