私の理解では、Javaのオブジェクトは参照によって渡されます。より正確には、オブジェクトへの参照は値によって渡されます。では、文字列を宣言して、文字列の値を変更する関数に渡すと、元の文字列が変更されないのはなぜですか? 例えば:
class Thing
{
static void func(String x){ x = "new"; }
public static void main(String [] args)
{
String y = "old";
func(y);
System.out.print(y);
}
}
y の値がまだ「古い」のはなぜですか?
編集: なぜ次は Thing something の属性 x を 90 に設定するのですか?
class Thing { int x = 0;
static void func(Thing t){ t.x = 90; }
public static void main(String [] args){
Thing something = null;
something = new Thing();
func(something);
System.out.print(something.x);
}
}