Eclipse posted this Java puzzle to Twitter today:
...do you know what will be printed without coding it and trying it?
Eclipse posted this Java puzzle to Twitter today:
...do you know what will be printed without coding it and trying it?
For further actions, you may consider blocking this person and/or reporting abuse
NΓ‘ndor HolozsnyΓ‘k -
Fedor Bystrov -
Daniel Rendox -
Mysterio -
Once suspended, awwsmm will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, awwsmm will be able to comment and publish posts again.
Once unpublished, all posts by awwsmm will become hidden and only accessible to themselves.
If awwsmm is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Andrew (he/him).
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community safe. Here is what you can do to flag awwsmm:
Unflagging awwsmm will restore default visibility to their posts.
Top comments (11)
that one should throw a NPE exception at line 10. Almost had me but then I read it again.
b canβt possibly be true. Can it? :-)
I don't know anything about Java. Why not? To the completely untrained eye it sure looks true.
Java has 2 ways of determining if two objects are βthe sameβ. The == operator, and the equals method. Both work differently, and both are shown in the example.
The == operator compares object identity. Creating two String objects and comparing them with == would return false, even if the text is βABCβ both times. Like: two persons can both be called βMartin Fowlerβ, but have distinct identities.
So, my first instinct told me: there are multiple occurrences of βABCβ. So multiple String objects. So the == returns false. Then, Java doesnβt even evaluate the second and operand (after &&). The result of the riddle: false is printed.
Then I looked at the riddle again. Java does heavy optimizing. Strings in Java are immutable, and in the example, βABCβ is not changed. So chances are that there only is exactly 1 βABCβ object.
In that case, the == yields true and the second and operand (with Y) is evaluated. And because Y is null, and calling any method (including equals) on null throws a NullPointerException...
The riddles answer is: nothing is printed. A NullPointerException is thrown.
I guess :-) Am I right?
The answer lies in the Java String pool!
Interesting, thanks for sharing!
NullPointerException
forY.equals(null)
.You can't invoke any method on null reference object :)
Gonna say True. Though not sure if b is True 100% lol
False. :)