2013-08-23 18 views
5

質問にはラッパークラスが含まれています。私は、ラッパークラスを使ってプリミティブ型リテラルを格納するとき、それをそのラッパークラスのオブジェクトとして格納しているので、オブジェクトの識別子は参照変数(C++のポインタのようなもの)になります。たとえば、Integer wi = new Integer("56")では、wiが参照変数です。しかし、それがtrueの場合:なぜラッパークラスオブジェクトの識別子が参照変数として機能しないのですか

  1. なぜwi++wi +=2を行うことができますか?コンパイラは、通常のプリミティブ変数のような参照変数を扱うのはなぜですか?参照変数はオブジェクトの参照を格納しませんか?

  2. Integer wi = new Integer("56")int pi = 56を指定すると、(wi == pi)がtrueを返すのはなぜですか。 wiは参照(アドレス)を保存するはずですか?

そして、別の質問:参照変数をパラメータとしてメソッドに渡されると、その参照変数に 起こるmodifictionはそれが価値だが、それにはない影響を与える必要がありますので、それが参照渡しとしてカウント:

public class Main { 
    void show(Integer x){ 
    x *=100 ; 
    } 

    void goo(int x){ 
    x *=100 ; 
    } 

    public static void main(String[] args) { 
    Main mn = new Main() ; 
    Integer wi = new Integer("86"); 
    int pi = 86 ; 

    mn.goo(pi); 
    System.out.println(pi); //output = 86 

    mn.show(wi); 
    System.out.println(wi); //output = 86, shouldn't it be 8600? 
    } 
} 
+0

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

+0

これはうまくいくでしょう。整数o =新しい整数( "86"); – Malwaregeek

+0

@nachokk参照で2つのIntegerオブジェクトを比較すると正しいです(falseを返します)。しかし、 'int i = 56'と' Integer.valueOf(56) 'を比較すると '参照'の等価が真であると思います。なぜなら、' Integer'がアンボックスされ、二つの 'int'型が比較されるからです。 @chrylis実際に 'String'を受け入れる' Integer'コンストラクタがあります。 –

答えて

4

ステートメントは、86のコピーを渡しますが、mn.show(wi)は同じオブジェクトを保持する参照変数のコピーを渡します。

  1. なぜこれを行うことができますか?通常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 
+3

Java言語仕様には、完全なセクション(http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.8)があります。これは起こり、ラッパータイプの特別な処理です。 – chrylis

0

Javaがコンセプト "の値によってコール" を使用しています。メソッドのshowは、ローカル変数を更新するだけです

0

コンパイラunboxes wiプリミティブデータ型。今はJavaのすべてが値渡しであるため、プリミティブ型です。仮引数の変更は実際の引数には影響しません。

mn.show(wi); 
1

Javaコンパイラが自動的にintIntegerの間で変換するintValueInteger.valueOfの呼び出しを挿入します。例えば、ここに質問からコードスニペットです:

void show(Integer x) { 
    int unboxed = x.intValue(); 
    unboxed *= 100; 
} 

あなたが見ることができるように、ラインx *= 100は本当にあなたが合格Integerオブジェクトを変更しません:

void show(Integer x){ 
    x *=100 ; 
} 

そして、ここは本当に何が起こるかでありますそのIntegerオブジェクトから抽出されたint値のみを変更します。

同様に、質問のwi == piというコードは実際にはあなたの観察を説明するwi.intValue() == piを意味します。

関連する問題