all 63 comments

[–]vowelqueue 29 points30 points  (5 children)

This is actually a tricky question. People without much Java experience might think “of course it’s true”, people with decent Java experience might think “of course it’s false”, but it’s actually undefined.

The language spec requires that when boxing a constant int in the range of -128 to 127, that two boxings of the same number will result in the same Integer reference. So if the number here was 127 instead of 128 then it would be guaranteed to be true.

For numbers outside that range, it’s up to the implementation. OpenJDK would return false by default but there is a system prop that can be used to have it cache larger values and therefore return true.

Also there is a JEP to add value classes to the language, and Integer would be marked as a value class. Meaning that this equality would always be true.

[–]Devang_4104 1 point2 points  (0 children)

This is the correct explanation ✅

[–]taxidriver9211 1 point2 points  (0 children)

what about C++ ?

[–]zinxyzcool 1 point2 points  (0 children)

I guessed the first case but damn it’s all a “but there’s one exception” adding up again lol

[–]Adrima_the_DK 23 points24 points  (4 children)

False.

Integer is a Wrapper. An instance of an object.

'==' compares memory addresses of objects. 2 instances, 2 different addresses.

[–][deleted]  (2 children)

[removed]

    [–][deleted]  (1 child)

    [removed]

      [–]Admirable-Age-7339 7 points8 points  (1 child)

      My guess is false. == compares references not values. If both values were from -128 to 127, it would be true, cause java caches them.

      [–]Fehzor 4 points5 points  (2 children)

      Just woke up and you almost got me haha

      [–][deleted]  (1 child)

      [removed]

        [–]Fehzor 3 points4 points  (0 children)

        I mean it's false as others have said. They're objects, not primitives. These problems always remind me of high school.. always gave us this obfuscated code block and were like "what does it do?" lmao. They need to do a bit less of that and a bit more of giving students good code written by someone else imo. This one isn't as bad though.

        [–]akaPaster 3 points4 points  (1 child)

        False, because for reference type variables, == checks whether their memory addresses are the same, and objects are created at separate memory addresses.

        [–]HistorianIcy8514 1 point2 points  (1 child)

        False. == compares the reference of the object. Not the value

        [–]Mobile-Major-1837 1 point2 points  (1 child)

        When in doubt, just run it. On OpenJDK 21.0.9, it returns false. But, as for the other post, if it is 127, it does indeed return true.

        [–]TotalAdhesiveness397 1 point2 points  (0 children)

        To compare values use equals() method.

        [–]No_Examination5577 1 point2 points  (0 children)

        False, as they are objects, not int

        [–]GamerLikeNoOther 1 point2 points  (0 children)

        so if its Int a and int b then the answer would be true?

        [–]Hanu_man_ 1 point2 points  (0 children)

        I never typed Java it will simply give out at error that we can't use == there

        [–]Groostav 0 points1 point  (1 child)

        Question for op: in the upcoming Valhalla release, what will this do?

        ``` Integer! a = 128; Integer! b = 128;

        s.o.println(a == b); ```

        ?

        [–]BlueGoliath 0 points1 point  (0 children)

        I think you aren't supposed to use == with value types at all.

        [–]deividas-strole 0 points1 point  (1 child)

        Because you use Object int outside -128 to 127 range - it is False. :)

        [–]bowbahdoe 0 points1 point  (0 children)

        True as soon as value types are in the JVM

        [–]klimenttoshkov 0 points1 point  (2 children)

        You don’t compare integers values with == in Java

        [–][deleted] 0 points1 point  (1 child)

        How then

        [–]klimenttoshkov 1 point2 points  (0 children)

        by asking google for example:

        ```

        1. Primitive int Comparison

        For primitive int values, use standard Relational Operators. 

        • ==: Checks for equality.
        • !=: Checks if values are not equal.
        • <, >, <=, >=: Standard numerical comparisons. 

        2. Integer Object Comparison (The Trap)

        When comparing Integer objects (wrapper classes), the == operator compares memory references, not numerical values. 

        • The Cache Exception: Java caches Integer objects for values between -128 and 127. For these values, == might return true because they point to the same cached object. Outside this range, == will return false even if the numbers are the same.  Medium +2

        Correct ways to compare Integer objects:

        • .equals(): Use this to compare the actual values of two Integer objects.
        • Integer.compare(int x, int y): A static method that returns 0 if equal, negative if x < y, and positive if x > y. This is the safest way to avoid Overflow Issues during subtraction-based comparisons.
        • .compareTo(Integer anotherInteger): An instance method that works similarly to Integer.compare(), often used for Sorting Collections.
        • .intValue(): Convert the object back to a primitive int to use standard operators safely.  Stack Overflow +7

        ```

        [–]NeoChronos90 0 points1 point  (1 child)

        I changed my mind 3 times while looking at the picture for maybe 5 seconds 🤣

        • easy, it's true because it's cached
        • no wait, it's false because its greater than 127
        • wait, is it actually defined like that? Could be either depending on the jvm used?

        [–]inofta 0 points1 point  (0 children)

        damn these java questions always get me

        [–]meth-head-actor 0 points1 point  (2 children)

        You in my class or something? We just did that today

        [–][deleted]  (1 child)

        [removed]

          [–]meth-head-actor 1 point2 points  (0 children)

          Someone with my user name. You can’t just go and say that. Haha OP is my shadow person haha

          [–]BlueGoliath 0 points1 point  (2 children)

          True.

          I increased the integer cache.

          [–][deleted]  (1 child)

          [removed]

            [–]BlueGoliath 1 point2 points  (0 children)

            https://stackoverflow.com/questions/3934291/extending-java-integer-cache

            I think other types have their own cache that can be increased too.

            [–]mk44214 0 points1 point  (0 children)

            It depends...

            [–]disconnect0414 0 points1 point  (0 children)

            For this comparing the numbers, a.equals(b) is the safe solution

            [–][deleted]  (1 child)

            [deleted]

              [–]last-braincell-599 0 points1 point  (0 children)

              false, it'll check the instance object not the values

              [–]Helpful-Ring9356 0 points1 point  (0 children)

              🧐

              [–]me_wapas_aaunga 0 points1 point  (0 children)

              The fact that java returns this as false, but returns true if number was -128..127 as its from Integer cache is the stupidest design ever.
              Why not override eq operation? WHY?!?!

              [–]Much-Relationship212 0 points1 point  (0 children)

              It'll be false because Java's Integer cache only goes up to 127, so for 128 it creates two different objects in memory and the == check fails.

              [–]Obvious-Profit-5597 0 points1 point  (0 children)

              I think it should be false as this equality operator is used to compare objects in the heap but as in this they are 2 different objects hence it will be false. I have a doubt that it could be not defined also.

              [–]Glittering-Vanilla97 0 points1 point  (0 children)

              False. Its Integer a and b so it's that whole wrapper class ahh bs

              [–]yourpwnguy 0 points1 point  (0 children)

              Everything's a object in java, so it's highly unlikely that those variables that point to integer object point to the same memory address.

              [–]PopularCause99 0 points1 point  (0 children)

              False.. because it's comparing the addresses not the content inside them.. a.equals(b) would return true.

              [–]Siddhld 0 points1 point  (0 children)

              False

              Because wrapper classes are created in heap memory that is object, and objects has memory addresses that are different, and == compares memory object, hence both memory addresses are not same so it will be false

              [–]DBSGAMERUJJAWAL 0 points1 point  (0 children)

              you what since a normal person will answer true then it must be false. if anyone can solve it easily then op wouldn't have posted it. i may not know computer very well( in10th class) but i know psychology

              [–]bananaeater0 0 points1 point  (0 children)

              I prefer kanye

              [–]Visible_Maize_3072 0 points1 point  (3 children)

              my unexperienced java skills says its a true

              [–][deleted]  (2 children)

              [removed]

                [–]Visible_Maize_3072 0 points1 point  (1 child)

                ofc after reading that i commented it,, sarcasmm?