我一直以为Java使用pass-by-reference. 但是,我读了一篇博客文章,声称Java使用pass-by-value. 我不认为我明白作者所做的区别。
什么是解释?
我一直以为Java使用pass-by-reference. 但是,我读了一篇博客文章,声称Java使用pass-by-value. 我不认为我明白作者所做的区别。
什么是解释?
当前回答
Java 以值传输对象的参考。
因此,如果对参考论点所指出的对象进行任何修改,则将反映在原始对象上。
但是,如果引用论点指向另一个对象,则原始引用将指向原始对象。
其他回答
最短的答案:)
Java 有 pass-by-value (和 pass-reference-by-value.) C# 也有 pass-by-reference
在 C# 中,它是用“out”和“ref”的关键字实现的。
通过参考:变量以一种方式通过,以便在方法内部的重新分配,即使在方法之外也被反映出来。
下面是通过参考(C#)的例子,这个功能在Java中不存在。
class Example
{
static void InitArray(out int[] arr)
{
arr = new int[5] { 1, 2, 3, 4, 5 };
}
static void Main()
{
int[] someArray;
InitArray(out someArray);
// This is true !
boolean isTrue = (someArray[0] == 1);
}
}
此分類上一篇: MSDN 圖書館(C#):通過 ref 和 out 的序列
此分類上一篇: MSDN 圖書館(C#):通過價值和参考
有一个工作室在Java的参考,让我用这个例子解释:
public class Yo {
public static void foo(int x){
System.out.println(x); //out 2
x = x+2;
System.out.println(x); // out 4
}
public static void foo(int[] x){
System.out.println(x[0]); //1
x[0] = x[0]+2;
System.out.println(x[0]); //3
}
public static void main(String[] args) {
int t = 2;
foo(t);
System.out.println(t); // out 2 (t did not change in foo)
int[] tab = new int[]{1};
foo(tab);
System.out.println(tab[0]); // out 3 (tab[0] did change in foo)
}}
我希望这能帮助!
理解它在两个步骤:
您无法更改对象本身的参考,但您可以使用此已过的参数作为对象的参考。
如果您想要更改参考背后的值,您只会在同一名称“d”的字符串上宣布一个新的变量。
public static void foo(Dog d) {
d.Name = "belly";
System.out.println(d); //Reference: Dog@1540e19d
d = new Dog("wuffwuff");
System.out.println(d); //Dog@677327b6
}
public static void main(String[] args) throws Exception{
Dog lisa = new Dog("Lisa");
foo(lisa);
System.out.println(lisa.Name); //belly
}
Java 严格要求 pass-by-value. 通过使用 pass-by-values 的参数不会影响/改变原始变量. 在下一个程序中,我们启动了一种称为 'x' 的变量,并使用 pass-by-value 技术来证明变量的值如何保持不变。
public class Main
{
public static void main(String[] args)
{
//Original value of 'x' will remain unchanged
// in case of call-by-value
int x = 5;
System.out.println( "Value of x before call-by-value: " + x);
// 5
processData(x);
System.out.println("Value of x after call-by-value: " + x);
// 5
}
public static void processData(int x)
{
x=x+10;
}
}
通过值和通过参考值之间的差异
什么是 Pass by Value?
通过值,函数参数的值将复制到记忆中的另一个位置,在函数内访问或修改变量时,它只会访问复制。
通过参考,记忆地址转移到该函数,换句话说,函数可以访问实际变量。
定义
Pass by value 是指将函数参数值复制到另一个变量的机制,而 pass by reference 是指将实际参数转移到函数的机制。
变化
目前的参数
短篇小说:
非原始:Java通过参考值;原始:仅值。
结局。
现在,如果你想想想什么(1)意味着,想象你有一个类的苹果:
class Apple {
private double weight;
public Apple(double weight) {
this.weight = weight;
}
// getters and setters ...
}
然后,当你将这个类的一个例子转移到主要方法:
class Main {
public static void main(String[] args) {
Apple apple = new Apple(3.14);
transmogrify(apple);
System.out.println(apple.getWeight()+ " the goose drank wine...";
}
private static void transmogrify(Apple apple) {
// does something with apple ...
apple.setWeight(apple.getWeight()+0.55);
}
}
哦......但你可能知道这一点,你对当你做这样的事情时会发生什么感兴趣:
class Main {
public static void main(String[] args) {
Apple apple = new Apple(3.14);
transmogrify(apple);
System.out.println("Who ate my: "+apple.getWeight()); // will it still be 3.14?
}
private static void transmogrify(Apple apple) {
// assign a new apple to the reference passed...
apple = new Apple(2.71);
}
}