2017-11-23 5 views
-2

誰かが私に論理を説明することができますなぜaとbは両方とも等しいとxとyは同じではありませんが同じ値を持っています。誰かが私に論理を説明することができますそれは両方が等しいと思われます

Integer a = 40;`// integer a 
Integer b =40; // integer b 
Integer x = 400; // why x and y are not equal 
Integer y = 400; // how is that possible 

if (a==b){ //first condition 
    System.out.println("a=b"); //if true print 
}else { 
    System.out.println("a!=b"); // if not true 
} 

if(x==y){ // second condition 
    System.out.println("x=y"); // if true print 
}else{ 
    System.out.println("x!y"); // if not true print 
} 
+0

オブジェクトを比較するのに '=='を使用しないでください。 'x.intValue()'を使うか、 '.equals()'と比較するか、最初に '' int''のプリミティブ型として作成してください。 – Touniouk

答えて

0

オブジェクトのそのほとんどがcompareTo()を上書きするため、==とオブジェクト(または本当に悪い習慣)を比較すること間違いあり、かつ明白なIntegeも例外ではありません。

+0

またはいずれの場合もequals() –

関連する問題