Tuesday, June 28, 2011

You too Integer !!!!!!!!

No doubt. You should be one of ‘The One’ if you can answer the output correctly……


class Integer {
   
    public static void main(final String args[]) {

        final java.lang.Integer a = 127;
        final java.lang.Integer b = 127;

        System.out.println(a == b);  // output 1

        final java.lang.Integer c = 128;
        final java.lang.Integer d = 128;

        System.out.println(c == d); // output 2

        // output 1 != output 2     ---- TRUE
    }
}
Looks tricky... Sheep is in Integer.valueOf() method... Can anyone explain this?

4 comments:

Ragzzy-R said...

Dude How Dude why the 128 is not returning true????

Kumar said...

Ha ha that's the Beauty. Integer offset range playing the role. Let me know I can explain it you.

Anonymous said...

explain it plz

Kumar said...

Hi Anonymous, Thanks for dropping here. 128 is the offset value which defined in valueOf() method. You will understand if you see the Integer.valueOf() API. Let me know then also you didn't get the point. I can explain you with nice example.

Post a Comment

Related Posts Plugin for WordPress, Blogger...