gpt4 book ai didi

java对象引用在方法中被改变并理解结果

转载 作者:行者123 更新时间:2023-12-02 00:22:08 25 4
gpt4 key购买 nike

以下程序输出

In second v.i:15
In first v.i:20

为什么这两种情况都不是 15。传递值对象,然后在第二个方法中更改对象引用。第二个方法应该是 15,并且在第一个方法中似乎也应该是 15

public class Test {
/**
* @param args
*/
class Value{
public int i = 15;
}
public static void main(String[] args) {
Test t = new Test();
t.first();
}
public void first(){
Value v = new Value();
v.i = 25;
second(v);
System.out.println("In First v.i:" + v.i);
}
public void second(Value v){
v.i = 20;
Value val = new Value();
v = val;
System.out.println("In second v.i:" + v.i);
}
}

最佳答案

Java 方法实现是通过调用[引用,如果是对象,]但不是完全是通过引用调用

您正在传递一个对象Value v表示内联方法范围变量v引用在方法中创建的对象v 第一个()。这意味着对 v 引用的同一对象的任何修改也将反射(reflect)在调用端。但是,在您的第二方法中,您正在为Value创建一个新对象,但指向方法范围变量v。这个新对象的内存位置与传入方法参数的内存位置不同。要识别差异,请检查使用其引用变量创建的对象的 hashCode

因此,在方法 second 中更改 v 的实例变量不会返回给该方法的调用者,除非该方法返回更改后的对象。您的方法在此处返回一个 void

大多数时候,程序员会对调用者和被调用方法中使用的相同引用名称感到困惑。

请查看以下示例以了解其中的差异。我已经包含了第三个和第四个方法,以进一步解释它。

public class Test {
class Value {
int i = 15;
}

public void second( Value v ) {
System.out.println( " 2.1.1: entered: v.i = " + v.i ); // 25
System.out.println( " 2.1.2: v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );

v = new Value();
v.i = 9;
System.out.println( " 2.2.1: new V: v.i = " + v.i ); // 9
System.out.println( " 2.2.2: v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
} // second(v)

public Value third( Value v ) {
System.out.println( " 3.1.1: entered: v.i = " + v.i ); // 25
System.out.println( " 3.1.2: v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );

v = new Value();
v.i = 9;
System.out.println( " 3.2.1: created: v.i = " + v.i ); // 9
System.out.println( " 3.2.2: v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );

return v;
} // third(v)

public Value fourth( final Value v ) {
System.out.println( " 4.1.1:entered: v.i = " + v.i ); // 9
System.out.println( " 4.1.2:v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );

/**********************************
// The final local v can't be assigned. It must be blank and not using a compound assignment.
// meaning, you are not allowed to change its memory location,
// but can alter its content, if permitted
// v = new Value();
//**********************************/

v.i = 45;
System.out.println( " 4.2.1:changed: v.i = " + v.i ); // 45
System.out.println( " 4.2.2:v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );

return v;
} // fourth(v)

public void first() {
System.out.println( "1.1.1: entered: ..." );

Value v = new Value();
System.out.println( "1.2.1: created; v.i = " + v.i ); // 15
v.i = 25;
System.out.println( "1.2.2: changed: v.i = " + v.i ); // 25
System.out.println();

System.out.println( "1.3.1: before calling second(v) ..." );
System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
second( v );
System.out.println( "1.3.2: returning from second(v) ..." );
System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
System.out.println();

System.out.println( "1.4.1: before calling third(v) ..." );
System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
v = third( v );
System.out.println( "1.4.2: returning from third(v) ..." );
System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
System.out.println();

System.out.println( "1.5.1: before calling fourth(v) ..." );
System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
v = fourth( v );
System.out.println( "1.5.2: returning from fourth(v) ..." );
System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
} // first()

public static void main( String ... a ) {
Test _this = new Test();
_this.first();
} // psvm(...)
} // class Test

当您运行上面的示例时,您可能会看到如下输出:

1.1.1: entered: ...
1.2.1: created; v.i = 15
1.2.2: changed: v.i = 25

1.3.1: before calling second(v) ...
v.i = 25, v.hashCode() = 1671711; v = Test$Value@19821f
2.1.1: entered: v.i = 25
2.1.2: v.hashCode() = 1671711; v = Test$Value@19821f
2.2.1: new V: v.i = 9
2.2.2: v.hashCode() = 11394033; v = Test$Value@addbf1
1.3.2: returning from second(v) ...
v.i = 25, v.hashCode() = 1671711; v = Test$Value@19821f

1.4.1: before calling third(v) ...
v.i = 25, v.hashCode() = 1671711; v = Test$Value@19821f
3.1.1: entered: v.i = 25
3.1.2: v.hashCode() = 1671711; v = Test$Value@19821f
3.2.1: created: v.i = 9
3.2.2: v.hashCode() = 4384790; v = Test$Value@42e816
1.4.2: returning from third(v) ...
v.i = 9, v.hashCode() = 4384790; v = Test$Value@42e816

1.5.1: before calling fourth(v) ...
v.i = 9, v.hashCode() = 4384790; v = Test$Value@42e816
4.1.1:entered: v.i = 9
4.1.2:v.hashCode() = 4384790; v = Test$Value@42e816
4.2.1:changed: v.i = 45
4.2.2:v.hashCode() = 4384790; v = Test$Value@42e816
1.5.2: returning from fourth(v) ...
v.i = 45, v.hashCode() = 4384790; v = Test$Value@42e816
<小时/>

如果您确实想在被调用方法中保存对对象 instanceVariableV 所做的更改,例如 fifth(),另一种可能性是声明 v 作为实例变量

下面的例子将解释这些差异。

public class Test {
Value instanceVariableV = null; // v

// rest of other variables and methods here
// ...

public void fifth() {
System.out.println( " 5.1.1:entered: instanceVariableV = " + instanceVariableV ); // null
// null, hence no hashCode(), and no toString() will work

// let us create an instance of Value
instanceVariableV = new Value();
System.out.println( " 5.2.1:created: instanceVariableV = " + instanceVariableV ); // Test$Value@9304b1
System.out.println( " 5.2.2: instanceVariableV.i = " + instanceVariableV.i ); // 15
System.out.println( " 5.2.3: hashCode = " + instanceVariableV.hashCode() ); // 9634993

instanceVariableV.i = 20;
System.out.println( " 5.3.1:changed: instanceVariableV.i = " + instanceVariableV.i ); // 20
System.out.println( " 5.3.2: hashCode = " + instanceVariableV.hashCode() ); // 9634993 // not changed
} // fifth()

public void first() {
// continuation of code

System.out.println( "1.6.1: before calling fifth() ..." );
System.out.println( " instanceVariableV = " + instanceVariableV );
fifth();
System.out.println( "1.6.2: returning from fifth() ..." );
System.out.println( " instanceVariableV = " + instanceVariableV );
if ( instanceVariableV != null ) {
// must be different from the one when created new
System.out.println( " .i = " + instanceVariableV.i );
// this won't differ
System.out.println( " .hashCode() = " + instanceVariableV.hashCode() );
}
} // first()

public static void main( String ... a ) {
// ...
System.out.println( "\r\nmain(...): vInstanceVariable = " + _this.instanceVariableV );
if ( _this.instanceVariableV != null ) {
// must be different from the one when created new
System.out.println( " .i = " + _this.instanceVariableV.i );
// this won't differ
System.out.println( " .hashCode() = " + _this.instanceVariableV.hashCode() );
}
} // psvm(...)

当您运行上述扩展示例时,您可能会看到如下输出:

1.6.1: before calling fifth() ...
instanceVariableV = null
5.1.1:entered: instanceVariableV = null
5.2.1:created: instanceVariableV = Test$Value@9304b1
5.2.2: instanceVariableV.i = 15
5.2.3: hashCode = 9634993
5.3.1:changed: instanceVariableV.i = 20
5.3.2: hashCode = 9634993
1.6.2: returning from fifth() ...
instanceVariableV = Test$Value@9304b1
.i = 20, .hashCode() = 9634993

main(...): vInstanceVariable = Test$Value@9304b1
.i = 20
.hashCode() = 9634993

希望这对您有帮助。

其他引用:

  1. Does Java pass by reference or pass by value?
  2. Is java pass by reference? (A posting on SO)

关于java对象引用在方法中被改变并理解结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10748996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com