Should'nt p.name = "EJava";
being altered after method anotherMethod(p);
was invoked ? Can anyone explain why this code returns :
不应该是p.name =“EJava”;在方法anotherMethod(p)之后被改变;被调用了吗?谁能解释为什么这段代码会返回:
anotherMethod
EJava
someMethod
someMethod
instead of :
代替 :
anotherMethod
anotherMethod
someMethod
someMethod
Class Person
class Person {
public String name;
public int height;
}
Tester
class EJavaGuruPassObjects1 {
public static void main(String args[]) {
Person p = new Person();
p.name = "EJava";
anotherMethod(p);
System.out.println(p.name);
someMethod(p);
System.out.println(p.name);
}
static void someMethod(Person p) {
p.name = "someMethod";
System.out.println(p.name);
}
static void anotherMethod(Person p) {
p = new Person();
p.name = "anotherMethod";
System.out.println(p.name);
}
}
Should'nt p.name = "EJava";
being altered after method anotherMethod(p);
was invoked?
不应该是p.name =“EJava”;在方法anotherMethod(p)之后被改变;被调用了吗?
5 个解决方案
#1
3
You are creating a new Person
object in anotherMethod()
.It should be
您正在anotherMethod()中创建一个新的Person对象。它应该是
static void anotherMethod(Person p) {
p.name = "anotherMethod";
System.out.println(p.name);
}
#2
1
Object reference variables are passed by value in Java (like everything else). In someMethod(), the name of the actual object to which p points is changed. In anotherMethod(), it points to a new Person object with its information changed. Your original reference is unchanged. Outside of these methods, you still have some variable pointing to the original Person
object.
对象引用变量在Java中通过值传递(与其他所有内容一样)。在someMethod()中,p点的实际对象的名称被更改。在anotherMethod()中,它指向一个新的Person对象,其信息已更改。您的原始参考文献未更改。在这些方法之外,您仍然有一些指向原始Person对象的变量。
It's best to think about these variables as pointers. You create a Person p
, and it points to this new object with a name you've decided on. Let's say you create another variable, Person x
and set x = p
. Now x and p point to the same object. Changing one will reflect itself in both variables. However, if you then do p = new Person()
, then p will point to a NEW object, but x will still point to that original object first referenced by p.
最好将这些变量视为指针。您创建了一个Person p,它指向具有您决定的名称的新对象。假设您创建另一个变量Person x并设置x = p。现在x和p指向同一个对象。改变一个将反映在两个变量中。但是,如果然后执行p = new Person(),则p将指向NEW对象,但x仍将指向首先由p引用的原始对象。
#3
0
Java does not use references in the C++ sense. so your method
Java不使用C ++意义上的引用。所以你的方法
static void anotherMethod(Person p) {
p = new Person();
p.name = "anotherMethod";
System.out.println(p.name);
}
}
does nothing to the p
variable, it simply creates new object, calls it (temporarly) p and forgets about it once the method is completed.
对p变量没有任何作用,它只是创建新对象,调用它(临时)p并在方法完成后忘记它。
p is just a copy of the reference to the actual object, Java always passes arguments as copies.
p只是对实际对象的引用的副本,Java总是将参数作为副本传递。
#4
0
Java passes all objects by copies of the reference, not by reference.
Java通过引用的副本传递所有对象,而不是通过引用传递。
That means that p
in anotherMethod
is a copy of the reference p
that lives in the main method. If you assign the p
that lives in anotherMethod
is has no impact on what p
in your main method references, because it's just a copy.
这意味着anotherMethod中的p是主方法中的引用p的副本。如果你指定生活在anotherMethod中的p对主方法引用的p没有影响,因为它只是一个副本。
#5
-1
In someMethod
you are changing the internals of a passed object, hence it would persist even after returning back to main method, if its not your intention, then make name
as final
.
在someMethod中,你正在改变传递对象的内部,因此即使在返回main方法之后它仍然存在,如果它不是你的意图,那么将其命名为final。
In anotherMethod
you are creating a new object assigning to local variable and changing it, hence it wouldn't touch the passed object as you are expecting.
在anotherMethod中,您正在创建一个分配给局部变量并更改它的新对象,因此它不会像您期望的那样触及传递的对象。
#1
3
You are creating a new Person
object in anotherMethod()
.It should be
您正在anotherMethod()中创建一个新的Person对象。它应该是
static void anotherMethod(Person p) {
p.name = "anotherMethod";
System.out.println(p.name);
}
#2
1
Object reference variables are passed by value in Java (like everything else). In someMethod(), the name of the actual object to which p points is changed. In anotherMethod(), it points to a new Person object with its information changed. Your original reference is unchanged. Outside of these methods, you still have some variable pointing to the original Person
object.
对象引用变量在Java中通过值传递(与其他所有内容一样)。在someMethod()中,p点的实际对象的名称被更改。在anotherMethod()中,它指向一个新的Person对象,其信息已更改。您的原始参考文献未更改。在这些方法之外,您仍然有一些指向原始Person对象的变量。
It's best to think about these variables as pointers. You create a Person p
, and it points to this new object with a name you've decided on. Let's say you create another variable, Person x
and set x = p
. Now x and p point to the same object. Changing one will reflect itself in both variables. However, if you then do p = new Person()
, then p will point to a NEW object, but x will still point to that original object first referenced by p.
最好将这些变量视为指针。您创建了一个Person p,它指向具有您决定的名称的新对象。假设您创建另一个变量Person x并设置x = p。现在x和p指向同一个对象。改变一个将反映在两个变量中。但是,如果然后执行p = new Person(),则p将指向NEW对象,但x仍将指向首先由p引用的原始对象。
#3
0
Java does not use references in the C++ sense. so your method
Java不使用C ++意义上的引用。所以你的方法
static void anotherMethod(Person p) {
p = new Person();
p.name = "anotherMethod";
System.out.println(p.name);
}
}
does nothing to the p
variable, it simply creates new object, calls it (temporarly) p and forgets about it once the method is completed.
对p变量没有任何作用,它只是创建新对象,调用它(临时)p并在方法完成后忘记它。
p is just a copy of the reference to the actual object, Java always passes arguments as copies.
p只是对实际对象的引用的副本,Java总是将参数作为副本传递。
#4
0
Java passes all objects by copies of the reference, not by reference.
Java通过引用的副本传递所有对象,而不是通过引用传递。
That means that p
in anotherMethod
is a copy of the reference p
that lives in the main method. If you assign the p
that lives in anotherMethod
is has no impact on what p
in your main method references, because it's just a copy.
这意味着anotherMethod中的p是主方法中的引用p的副本。如果你指定生活在anotherMethod中的p对主方法引用的p没有影响,因为它只是一个副本。
#5
-1
In someMethod
you are changing the internals of a passed object, hence it would persist even after returning back to main method, if its not your intention, then make name
as final
.
在someMethod中,你正在改变传递对象的内部,因此即使在返回main方法之后它仍然存在,如果它不是你的意图,那么将其命名为final。
In anotherMethod
you are creating a new object assigning to local variable and changing it, hence it wouldn't touch the passed object as you are expecting.
在anotherMethod中,您正在创建一个分配给局部变量并更改它的新对象,因此它不会像您期望的那样触及传递的对象。