ステートメントは、86
のコピーを渡しますが、mn.show(wi)
は同じオブジェクトを保持する参照変数のコピーを渡します。
- なぜこれを行うことができますか?通常primitve変数のようなものを参照vriablesとコンパイラの契約は?(オブジェクトの参照変数ストアの参照ではないでしょうか?)ない理由のWi ++またはWi + = 2が意味する.I
ためautoboxing
の概念の我々が持っている場合とauto-unboxing
は、wi
は、primitive
に変換インクリメントされ、その後、その後、==> "整数のwi =新しい整数( "56")" と「int型のPI = 56
2.or Wrapper
に変換"なぜ(wi == pi)がtrueを返すのか? Integer
ラッパークラスのために、==
は128
までの値をtrueを返しますので
これはrefernce(アドレス)を格納することになっWIされていません。これは、これらのプログラム
class PassPrimitiveToMethod
{
public static void main(String [] args)
{
int a = 5;
System.out.println("Before Passing value to modify() a = " + a);
PassPrimitiveToMethod p = new PassPrimitiveToMethod();
p.modify(a);
System.out.println("After passing value to modify() a = " + a);
// the output is still the same because the copy of the value is passed to the method and not the copy of the bits like in refrence variables
// hence unlike the reference variables the value remains unchanged after coming back to the main method
}
void modify(int b)
{
b = b + 1;
System.out.println("Modified number b = " + b);
// here the value passed is the copy of variable a
// and only the copy is modified here not the variable
}
}
出力はメソッドにオブジェクト参照を渡す
Before Passing value to modify() a = 5
Modified number b = 6
After passing value to modify() a = 5
で勉強してくださいデザインによって
passignプリミティブに関するあなたの疑問については
およびオブジェクトの参照である
class PassReferenceToMethod
{
public static void main(String [] args)
{
Dimension d = new Dimension(5,10);
PassReferenceToMethod p = new PassReferenceToMethod();
System.out.println("Before passing the reference d.height = " + d.height);
p.modify(d); // pass the d reference variable
System.out.println("After passing the reference d.height = " + d.height);
// the value changes because we are passing the refrence only which points to the single and same object
// hence the values of the object are modified
}
void modify(Dimension dim)
{
dim.height = dim.height + 1;
}
}
出力は
です
class PassReferenceToMethod
{
public static void main(String [] args)
{
Dimension d = new Dimension(5,10);
PassReferenceToMethod p = new PassReferenceToMethod();
System.out.println("Before passing the reference d.height = " + d.height);
p.modify(d); // pass the d reference variable
System.out.println("After passing the reference d.height = " + d.height);
// the value changes because we are passing the refrence only which points to the single and same object
// hence the values of the object are modified
}
void modify(Dimension dim)
{
dim.height = dim.height + 1;
}
}
出力は、x * = 100、あなたのケースでhere ので、詳細に説明するように
Before passing the reference d.height = 10
After passing the reference d.height = 11
1)Autoboxing introudce in java 1.5 2) 'j = new Integer(5)'と 'Integer i = 5'を実行すると、整数値が-128から127の間で整数になります。 == j'はtrueを返します。3) 'new Integer'コンストラクタは常に' valueOf'または 'parseXXX'を使うべきではありません。 – nachokk
これはうまくいくでしょう。整数o =新しい整数( "86"); – Malwaregeek
@nachokk参照で2つのIntegerオブジェクトを比較すると正しいです(falseを返します)。しかし、 'int i = 56'と' Integer.valueOf(56) 'を比較すると '参照'の等価が真であると思います。なぜなら、' Integer'がアンボックスされ、二つの 'int'型が比較されるからです。 @chrylis実際に 'String'を受け入れる' Integer'コンストラクタがあります。 –