問題描述
我希望有人可以為我澄清這里發生的事情.我在整數類中研究了一會兒,但因為整數 覆蓋 +
運算符我無法弄清楚出了什么問題.我的問題在于這一行:
整數 i = 0;我 = 我 + 1;//← 我認為這是在以某種方式創建一個新對象!
這是我的推理:我知道 java 是按值傳遞的(或按引用值傳遞),所以我認為在以下示例中,整數對象應每次遞增.
公共類 PassByReference {公共靜態整數公司(整數 i){我=我+1;//我認為這一定是**偷偷**創建一個新整數...System.out.println("公司:"+i);返回我;}公共靜態無效主要(字符串[]參數){整數整數 = 新整數(0);for (int i =0; i<10; i++){公司(整數);System.out.println("main:"+integer);}}}
這是我的預期輸出:
<上一頁>公司:1主要:1公司:2主要:2公司:3主要:3公司:4主要:4公司:5主要:5公司:6主要:6...這是實際輸出.
<上一頁>公司:1主要:0公司:1主要:0公司:1主要:0...為什么會這樣?
有兩個問題:
- 整數是按值傳遞,而不是按引用傳遞.更改方法內的引用不會反映到調用方法中傳入的引用中.
- 整數是不可變的.沒有像
Integer#set(i)
這樣的方法.否則,您可以直接使用它.
要讓它工作,你需要重新分配 inc()
方法的返回值.
integer = inc(integer);
<小時>
要了解有關按值傳遞的更多信息,這里有另一個示例:
public static void main(String...args) {字符串[] 字符串 = 新字符串 [] { "foo", "bar" };更改參考(字符串);System.out.println(Arrays.toString(strings));//仍然是 [foo, bar]更改值(字符串);System.out.println(Arrays.toString(strings));//[foo, foo]}公共靜態無效changeReference(字符串[]字符串){字符串 = 新字符串 [] { "foo", "foo" };}公共靜態無效changeValue(字符串[]字符串){字符串[1] = "foo";}
I am hoping that someone can clarify what is happening here for me. I dug around in the integer class for a bit but because integer is overriding the +
operator I could not figure out what was going wrong. My problem is with this line:
Integer i = 0;
i = i + 1; // ← I think that this is somehow creating a new object!
Here is my reasoning: I know that java is pass by value (or pass by value of reference), so I think that in the following example the integer object should be incremented each time.
public class PassByReference {
public static Integer inc(Integer i) {
i = i+1; // I think that this must be **sneakally** creating a new integer...
System.out.println("Inc: "+i);
return i;
}
public static void main(String[] args) {
Integer integer = new Integer(0);
for (int i =0; i<10; i++){
inc(integer);
System.out.println("main: "+integer);
}
}
}
This is my expected output:
Inc: 1 main: 1 Inc: 2 main: 2 Inc: 3 main: 3 Inc: 4 main: 4 Inc: 5 main: 5 Inc: 6 main: 6 ...
This is the actual output.
Inc: 1 main: 0 Inc: 1 main: 0 Inc: 1 main: 0 ...
Why is it behaving like this?
There are two problems:
- Integer is pass by value, not by reference. Changing the reference inside a method won't be reflected into the passed-in reference in the calling method.
- Integer is immutable. There's no such method like
Integer#set(i)
. You could otherwise just make use of it.
To get it to work, you need to reassign the return value of the inc()
method.
integer = inc(integer);
To learn a bit more about passing by value, here's another example:
public static void main(String... args) {
String[] strings = new String[] { "foo", "bar" };
changeReference(strings);
System.out.println(Arrays.toString(strings)); // still [foo, bar]
changeValue(strings);
System.out.println(Arrays.toString(strings)); // [foo, foo]
}
public static void changeReference(String[] strings) {
strings = new String[] { "foo", "foo" };
}
public static void changeValue(String[] strings) {
strings[1] = "foo";
}
這篇關于如何通過引用正確傳遞 Integer 類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!