krocz ze mną ..porównywania wartości Integer w Javie, dziwne zachowanie
Integer x = 23;
Integer y = 23;
if (x == y)
System.out.println("what else"); // All is well as expected
else
System.out.println("...");
Podczas
Integer x = someObject.getIndex();
Integer y = someOtherObject.getSomeOtherIndex();
if (x == y)
System.out.println("what else");
else
System.out.println("..."); // Prints this
Hmm ... Staram odlewania do int
int x = someObject.getIndex();
int y = someOtherObject.getSomeOtherIndex()
if (x == y)
System.out.println("what else"); // works fine
else
System.out.println("...");
Są one zarówno Liczby całkowite?
System.out.println(x.getClass().getName()); // java.lang.Integer
System.out.println(y.getClass().getName()); // java.lang.Integer
System.out.println(someObject.getIndex()); // java.lang.Integer
System.out.println(someOtherObject.getSomeOtherIndex()); // java.lang.Integer
Co wy myślicie? Co mogłoby wyjaśnić coś takiego?
możliwe duplikat [Jak = operator i == operator pracuje w Javie ?] (http://stackoverflow.com/questions/9824053/how-operator-and-operator-works-in-java) – assylias
Co robią 'getIndex();' i 'getSomeOtherIndex()'? –
Prawdopodobny duplikat [Dlaczego 128 == 128 zwraca fałsz, ale 127 == 127 zwraca wartość true podczas konwersji do wrapperów Integer?] (Https://stackoverflow.com/questions/1700081/why-does-128-128-return- false-but-127-127-return-true-when-converting-to-integ) – Tom