将对象传递给方法java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19186576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing objects to methods java
提问by user1050619
I'm using three objects: StringBuilder
, Integer
, and testobject
- that are passed to a method to change its state.
我正在使用三个对象:StringBuilder
、Integer
和testobject
- 它们被传递给一个方法来改变它的状态。
As expected, the StringBuilder
and testobject
points to the same object and the state is change but it does not work for Integer
object.
正如预期的那样,StringBuilder
和testobject
指向同一个对象并且状态发生了变化,但它不适用于Integer
对象。
class testobject{
int x = 1;
}
public class test{
public static void main(String[] args){
StringBuilder s1 = new StringBuilder("String");
go(s1);
System.out.println(s1);
Integer s2 = new Integer("20");
go1(s2);
System.out.println(s2);
testobject s3 = new testobject();
go2(s3);
System.out.println(s3.x);
}
static void go(StringBuilder s1){
s1.append("Builder");
}
static void go1(Integer s2){
s2 = 1;
}
static void go2(testobject s3){
s3.x = 5;
}
Result:
结果:
StringBuilder
20
5
ExpectedResult:
预期结果:
StringBuilder
1
5
回答by Jon Skeet
Look at your three methods:
看看你的三种方法:
static void go(StringBuilder s1){
s1.append("Builder");
}
static void go1(Integer s2){
s2 = 1;
}
static void go2(testobject s3){
s3.x = 5;
}
In go
and go2
, you're making a modification to the object that the parameter value refers to.
在go
and 中go2
,您正在修改参数值所引用的对象。
In go1
, you're changing the value of the parameter variable itself. That's very different, and because Java always uses pass-by-value, that change isn't seen by the caller.
在 中go1
,您正在更改参数变量本身的值。这是非常不同的,因为Java 总是使用 pass-by-value,调用者不会看到这种变化。
It's important to understand that objectsaren't passed to the methods at all. Instead, references are. The value of s1
, s2
and s3
are all references. If you think of the variables as like pieces of paper, each piece of paper has a house address on it, which was copied from a piece of paper declared in main
.
了解对象根本不会传递给方法很重要。相反,参考是。s1
、s2
和的值s3
都是引用。如果你把变量想象成一张纸,每张纸上都有一个房子地址,它是从main
.
The method bodies of go
and go2
are like visiting the house whose address is on the piece of paper, and painting the front door. If you then visit the houses using the originalpieces of paper, you still see the new colours on the front doors.
该方法机构go
和go2
都喜欢逛,其地址是在一张纸的房子,画前门。如果您随后使用原始纸张参观房屋,您仍然会看到前门上的新颜色。
The method body of go1
is like scribbling out the address written on the piece of paper, and writing a new one on there instead. That doesn't make any change to a house, nor does it change the original piece of paper.
的方法体go1
就像是把写在纸上的地址写下来,然后在上面写一个新的地址。这不会改变房子,也不会改变原来的一张纸。
回答by siva
If the argument you want to pass is an object instead a of primitive type, then Java treats pass by object as pass by value, because Java supports a pass-by-valueconcept.
如果您要传递的参数是一个对象而不是原始类型,那么 Java 将按对象传递视为按值传递,因为 Java 支持按值传递概念。
Here a and p are the same reference:
这里 a 和 p 是同一个引用:
public class ObjPass {
int value;
public static void main(String[] args) {
ObjPass p = new ObjPass();
p.value = 5;
System.out.println("Before calling: " + p.value);
increment(p);
System.out.println("After calling: " + p.value);
}
public static void increment(ObjPass a){
a.value++;
}
}
Output:
输出:
Before calling: 5
After calling: 6