资讯专栏INFORMATION COLUMN

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

Lowky / 745人阅读

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:

</>复制代码

  1. void swap(Type a1, Type a2) {
  2. Type temp = a1;
  3. a1 = a2;
  4. a2 = temp;
  5. }

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

</>复制代码

  1. class Apple {
  2. public String color = "red";
  3. }
  4. public class main {
  5. public static void main(String[] args) {
  6. Apple a1 = new Apple();
  7. System.out.println(a1.color); //print "red"
  8. changeColor(a1);
  9. System.out.println(a1.color); //print "green"
  10. }
  11. public static void changeColor(Apple apple) {
  12. apple.color = "green";
  13. }
  14. }

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

</>复制代码

  1. public class Main {
  2. public static void main(String[] args) {
  3. Student s = new Student("John");
  4. changeName(s);
  5. System.out.printf(s); // will print "John"
  6. modifyName(s);
  7. System.out.printf(s); // will print "Dave"
  8. }
  9. public static void changeName(Student a) {
  10. Student b = new Student("Mary");
  11. a = b;
  12. }
  13. public static void modifyName(Student c) {
  14. c.setAttribute("Dave");
  15. }
  16. }

</>复制代码

  1. public static void changeContent(int[] arr) {
  2. // If we change the content of arr.
  3. arr[0] = 10; // Will change the content of array in main()
  4. }
  5. public static void changeRef(int[] arr) {
  6. arr = new int[2]; // If we change the reference
  7. arr[0] = 15; // Will not change the array in main()
  8. }
  9. public static void main(String[] args) {
  10. int [] arr = new int[2];
  11. arr[0] = 4;
  12. arr[1] = 5;
  13. changeContent(arr);
  14. System.out.println(arr[0]); // Will print 10..
  15. changeRef(arr);
  16. System.out.println(arr[0]); // Will still print 10..
  17. }

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

转载请注明本文地址: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元查看
<