Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试时该怎么回答函数传参方式这个问题? #102

Open
xxleyi opened this issue May 25, 2019 · 5 comments
Open

面试时该怎么回答函数传参方式这个问题? #102

xxleyi opened this issue May 25, 2019 · 5 comments
Labels

Comments

@xxleyi
Copy link
Owner

xxleyi commented May 25, 2019

JS Python Java 这类语言的「函数传参」真是一个有趣的话题。

先来让我列举一下比较流行的回答:


  • 一律传引用

这个回答表示没想过太多。


不可变(原生)传值,可变(非原生)传引用

这个回答表示想过一些,但只是为了解释现象。


一律传值(这里的值是 value of object reference)

这个回答表示仔细想过或者调研过,选择扩充旧术语的含义。


一律 Call By Sharing

这个回答表示仔细想过或者调研过,选择使用新术语。


@xxleyi
Copy link
Owner Author

xxleyi commented May 25, 2019

我个人选择使用这个 Call By Sharing 新术语,虽然也有很多人,尤其是在 Java 社区中普遍反对引入 Call By Sharing 这样一个新术语,而是选择给 Call By Value 赋予新的含义。

在使用新术语和扩充旧术语含义两者之间,我更倾向于使用新术语。借助一位网友的说法,这个新术语能给人以一种更高层面的视角:a high view

@xxleyi
Copy link
Owner Author

xxleyi commented May 25, 2019

show code

#include <cstdio>

int main() {
  int a1, a2;
  int *b, *c;
  a1 = 33;
  b = &a1;
  
  a2 = a1;
  c = &a2;
  if (b == c){
    printf("is same\n");
  } else {
    printf("is not same\n");
  }
  return 0;
}

image

public class YourClassNameHere {
    public static void main(String[] args) {
      String s = "hello";
      String g;
      g = s;
      System.out.println("Are the reference addresses similar? "+(s==g));
      g = "goodbye";
      System.out.println("Are the reference addresses similar? "+(s==g));
    }
}

// Note that == checks the equality of reference address. 
// If you want to check the equality of the value of the strings, use the equals() method.

image

#include <cstdio>

int main() {
  int a;
  int *b, *c;
  a = 33;
  b = &a;
  
  a = 44;
  c = &a;
  if (b == c){
    printf("no change");
  }
  return 0;
}

image

@xxleyi
Copy link
Owner Author

xxleyi commented May 25, 2019

@xxleyi
Copy link
Owner Author

xxleyi commented May 26, 2019

show code

import java.util.*; 
  
public class Demo { 
    public static void main(String args[]) 
    { 
        // Creating an empty Stack 
        Stack<String> stack = new Stack<String>(); 
        stack.push("Welcome"); 
        System.out.println("Stack before call : " + stack); 
        changeStack(stack, stack);
        System.out.println("Stack after call: " + stack); 
        System.out.println("##################################");
        Integer intA = 1111;
        System.out.println("Integer before call : " + intA);
        changeInteger(intA, intA);
        System.out.println("Integer after call : " + intA);
    } 
    
    public static void changeStack(Stack<String> a, Stack<String> b){
      a.push("change");
      if (a == b) {
        System.out.println("call by value");
        }
      a = new Stack<String>();
      a.push("new Stack");
      System.out.println("new Stack a : " + a);
      }
      
    public static void changeInteger(Integer a, Integer b){
      Integer c = 33333;
      Integer d = 33333;
      if (a == b && c != d) {
        System.out.println("call by value");
        }
      a = 2222;
      System.out.println("new Integer a : " + a);
      }
}

Demo Online

@xxleyi
Copy link
Owner Author

xxleyi commented May 30, 2019

在 Java 中,对象类型无疑是共享传递,但原始类型依然存疑,主要是没有一个好的方式来确定原始类型的地址。

阶段性探索结果

@xxleyi xxleyi added WHY and removed 有看头 labels Mar 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant