poniedziałek, 4 lutego 2013

Iteresting facts and traps in Java


I am learning now for some test, and I like to share with my obeservations
Let's start with some example class.

How do you think, what will be the ouput?
Equals values -test 1
The same object! - test 2
Equals values - test 2
Suprised? Now you are wondering but how this is possible? Tests 2 tells me that the objects are equal and they have same value, but why this is not the same with Test 1? This is because two instances of the following warpper objects (created via boxing) will be always equal(in meaning ==) when their primitive are equals! Integer in range between -127...128 is a Short, so in first test we were out of the range short, and condition (a==b) failed.


Brak komentarzy:

Prześlij komentarz