资讯专栏INFORMATION COLUMN

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

Lowky / 570人阅读

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.. 
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65062.html

相关文章

  • 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 评论0 收藏0
  • 【译】十个刁钻的 Java 面试题

    摘要:原文地址这里列出了十个常见而又刁钻的开发人员面试题及答案,这些题目是我从上找来的。如果你是初中级开发人员,而且近期准备面试的话,这些题目可能对你有些帮助。成员即没有访问修饰符的成员可以在当前包下的所有类中访问到。 原文地址:https://dzone.com/articles/10... 这里列出了十个常见而又刁钻的 Java 开发人员面试题及答案,这些题目是我从 StackOverf...

    xuhong 评论0 收藏0
  • 理解引用

    摘要:我会解释里面神秘的引用,一旦你理解了引用,你就会明白通过引用来了解的绑定是多么轻松,你也会发现读的规范容易得多了。二理论把引用定义成。看看运算符的说法这也就是为什么我们对一个无法解析的引用使用操作符的时候并不会报错。 Know thy reference (原文:know thy reference - kangax) 一、前言 翻译好不是件容易的事儿,我尽量讲得通顺,一些术语会保留原...

    curlyCheng 评论0 收藏0
  • 快速掌握JavaScript面试基础知识(一)

    摘要:根据调查,自年一来,是最流行的编程语言。在一个函数体中声明的变量和函数,周围的作用域内无法访问。也就是说被大括号包围起来的区域声明的变量外部将不可访问。一个常见的误解是使用声明的变量,其值不可更改。 译者按: 总结了大量JavaScript基本知识点,很有用! 原文: The Definitive JavaScript Handbook for your next developer ...

    acrazing 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<