b***i 发帖数: 3043 | 1 看起来,如果a是数组,则数组里面的值可以修改,
如果是int, 则不能改。这里面的规则是什么? |
r*****s 发帖数: 985 | 2 pass by reference and pass by value
【在 b***i 的大作中提到】 : 看起来,如果a是数组,则数组里面的值可以修改, : 如果是int, 则不能改。这里面的规则是什么?
|
b***i 发帖数: 3043 | 3 我 的 理解是,java把参数放进堆栈,然后呼叫函数,
如果堆栈中是int,显然不能改变。
如果堆栈中是一个数组的地址,则数组内容可变。
【在 r*****s 的大作中提到】 : pass by reference and pass by value
|
g*****g 发帖数: 34805 | 4 It's always pass by reference for object and pass by value
for primitive. Array is an object and values in object can
be changed.
【在 b***i 的大作中提到】 : 看起来,如果a是数组,则数组里面的值可以修改, : 如果是int, 则不能改。这里面的规则是什么?
|
e********3 发帖数: 18578 | 5 Java一直都是pass by value的,只不过object的value是reference罢了。
【在 g*****g 的大作中提到】 : It's always pass by reference for object and pass by value : for primitive. Array is an object and values in object can : be changed.
|
x*****p 发帖数: 1707 | 6 Java is always pass by references. For primitive types, in Java 5, it is
considered as a object by autoboxing. Thus it is still pass by references.
The reason why the value of a primitive can not be changed outside the
function is because that the corresponding object of the primitive is
immutable.
Look at the following two examples.
1. void foo(String str) { str = "inFoo"; }
String s = "abc";
foo(s);
System.out.println(s);
The output is abc, not inFoo. String is an object, but it is immutable. so
its value can not be changed and thus looks like pass by value.
2. class A { int x = 10; }
void foo(A a) { a.x = 20; }
A a = new A();
foo(a);
System.out.println(a.x);
The output is 20, not 10. It is pass by references and the value is changed
after the function call. |
b***i 发帖数: 3043 | 7 我的理解
java是pass by value。所以,传入一个整数作为参数到函数里面是不能改变函数外面
那个 变量的值 的。
而那些复杂的类,传入的是类的地址的值。在这里,类的值就是变量地址的值。因为
java里面不讲地址,但本质上就是堆栈里面的地址。当你传入类变量的地址,当然可以
根据地址改变地址指向的那个数据,但是不能改变该变量本身的地址。这和C语言里面
传入一个指针,可以改变指针指向的数据,但是不能改变指针本身是一样的。
【在 x*****p 的大作中提到】 : Java is always pass by references. For primitive types, in Java 5, it is : considered as a object by autoboxing. Thus it is still pass by references. : The reason why the value of a primitive can not be changed outside the : function is because that the corresponding object of the primitive is : immutable. : Look at the following two examples. : 1. void foo(String str) { str = "inFoo"; } : String s = "abc"; : foo(s); : System.out.println(s);
|
g**e 发帖数: 6127 | 8 java is pass by value
【在 x*****p 的大作中提到】 : Java is always pass by references. For primitive types, in Java 5, it is : considered as a object by autoboxing. Thus it is still pass by references. : The reason why the value of a primitive can not be changed outside the : function is because that the corresponding object of the primitive is : immutable. : Look at the following two examples. : 1. void foo(String str) { str = "inFoo"; } : String s = "abc"; : foo(s); : System.out.println(s);
|
m****r 发帖数: 6639 | 9 我觉得, 其实大家都知道怎么回事。 混乱在于, 对pass by reference/pass by
value的理解错误。
java is pass by value.
pass by reference has a simple test:
swap(YourType a, YourType b) {
YourType temp;
temp = a;
a = b;
b = temp;
}
如果这个过了, 就是pass by reference。
【在 g**e 的大作中提到】 : java is pass by value
|
x*****p 发帖数: 1707 | 10 In C/C++, value and reference has strict definition. Reference only means
the address of an object and the value means the content of the object.
However, Java messed up the two concepts. Lots of books say that Java is
pass by value, and actually mean "pass by the value of the reference". So in
Java, value and reference are the same thing. That is why it confused so
many people and create a big fight among even top Java players in the world.
In US industry, the answer is unique: Java is pass by reference. |
|
|
g**e 发帖数: 6127 | 11 pass by the value of the reference
这句话靠谱
US哪个industry说java pass by reference的?
in
world.
【在 x*****p 的大作中提到】 : In C/C++, value and reference has strict definition. Reference only means : the address of an object and the value means the content of the object. : However, Java messed up the two concepts. Lots of books say that Java is : pass by value, and actually mean "pass by the value of the reference". So in : Java, value and reference are the same thing. That is why it confused so : many people and create a big fight among even top Java players in the world. : In US industry, the answer is unique: Java is pass by reference.
|
e********g 发帖数: 2524 | 12 exactly, for primitive types(8 types, boolean, char, short, int, long, float
, double) types pass by value
for objects pass the reference of objects
【在 e********3 的大作中提到】 : Java一直都是pass by value的,只不过object的value是reference罢了。
|
a**e 发帖数: 5794 | 13 C语言你传一个结构的变量给函数,变量的值是不变的;
Java你传一个类的对象给方法,那对象的值是可变的。
所以C的传值更严格。Sun自己的Java Tutorial在1.4和以前都说是传
参,到1.5时才改为传值。
【在 b***i 的大作中提到】 : 我的理解 : java是pass by value。所以,传入一个整数作为参数到函数里面是不能改变函数外面 : 那个 变量的值 的。 : 而那些复杂的类,传入的是类的地址的值。在这里,类的值就是变量地址的值。因为 : java里面不讲地址,但本质上就是堆栈里面的地址。当你传入类变量的地址,当然可以 : 根据地址改变地址指向的那个数据,但是不能改变该变量本身的地址。这和C语言里面 : 传入一个指针,可以改变指针指向的数据,但是不能改变指针本身是一样的。
|
x*****p 发帖数: 1707 | 14 美国工业界,都是回答Java is pass by reference,or the value of the reference
。 |
G****o 发帖数: 155 | 15 support... pass by reference... |
h*****0 发帖数: 4889 | 16 这理解得完全莫名其妙……
【在 x*****p 的大作中提到】 : Java is always pass by references. For primitive types, in Java 5, it is : considered as a object by autoboxing. Thus it is still pass by references. : The reason why the value of a primitive can not be changed outside the : function is because that the corresponding object of the primitive is : immutable. : Look at the following two examples. : 1. void foo(String str) { str = "inFoo"; } : String s = "abc"; : foo(s); : System.out.println(s);
|
h*****0 发帖数: 4889 | 17 对于对象,是这样的。对于int这种,你居然还敢说passed by reference,还找出一个
不靠谱的例子,说出"auto-boxing"这种跟本话题完全不相关的内容。
in
world.
【在 x*****p 的大作中提到】 : In C/C++, value and reference has strict definition. Reference only means : the address of an object and the value means the content of the object. : However, Java messed up the two concepts. Lots of books say that Java is : pass by value, and actually mean "pass by the value of the reference". So in : Java, value and reference are the same thing. That is why it confused so : many people and create a big fight among even top Java players in the world. : In US industry, the answer is unique: Java is pass by reference.
|
h*****0 发帖数: 4889 | 18 因为Java里其实没有对象变量。你看起来是传一个类的对象给方法,但其实传的是那个
对象的指针。Java里是不能用对象给对象赋值的。只能指针间赋值。
【在 a**e 的大作中提到】 : C语言你传一个结构的变量给函数,变量的值是不变的; : Java你传一个类的对象给方法,那对象的值是可变的。 : 所以C的传值更严格。Sun自己的Java Tutorial在1.4和以前都说是传 : 参,到1.5时才改为传值。
|
h*****0 发帖数: 4889 | 19 这个绝对是指非基本类型。
reference
【在 x*****p 的大作中提到】 : 美国工业界,都是回答Java is pass by reference,or the value of the reference : 。
|