What will be written to the console by the following code:

01: public class Clazz {
02:     public static void main(String[] args) {
03:         Integer integer = new Integer(1024);
04:         Long aLong = new Long(1024);
05:         if (integer.equals(aLong))
06:             System.out.println("eq");
07:         if (integer.intValue() == aLong.longValue())
08:             System.out.println("==");
09:     }
10: }
Explanation
Integer's equals method is overriden as following:

public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Accordingly, due to the argument having Long type result will be false.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback