-
Notifications
You must be signed in to change notification settings - Fork 3
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
Labels
Comments
我个人选择使用这个 在使用新术语和扩充旧术语含义两者之间,我更倾向于使用新术语。借助一位网友的说法,这个新术语能给人以一种更高层面的视角: |
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;
} 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. #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;
} |
show codeimport 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);
}
} |
在 Java 中,对象类型无疑是共享传递,但原始类型依然存疑,主要是没有一个好的方式来确定原始类型的地址。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JS Python Java 这类语言的「函数传参」真是一个有趣的话题。
先来让我列举一下比较流行的回答:
这个回答表示没想过太多。
不可变(原生)传值,可变(非原生)传引用
这个回答表示想过一些,但只是为了解释现象。
一律传值(这里的值是 value of object reference)
这个回答表示仔细想过或者调研过,选择扩充旧术语的含义。
一律 Call By Sharing
这个回答表示仔细想过或者调研过,选择使用新术语。
The text was updated successfully, but these errors were encountered: