Puzzling Java statement of the day
Some days ago I stumbled across a Java statement which I thought was trivial at first, only to discover that I had no idea. I have a reasonable understanding of what a JVM does and how Java bytecode is executed. But as this example shows once again, that doesn’t necessarily spread to the Java programming language. The snippet below should explain my point.
int lorem = 1, ipsum = 2, dolor = 3;
if (lorem == (lorem = ipsum))
f();
if ((ipsum = dolor) == ipsum)
g();
Which of the above two methods f()
and g()
is actually invoked? Can you tell without compiling the code? Possible answers are:
- None of the two methods get invoked, both call-sites are dead code.
- Just
f()
is invoked and the call-site ofg()
is dead code. - Just
g()
is invoked and the call-site off()
is dead code. - Both methods are invoked, the conditions are pointless.
Ironically, I finally understood what was going on after looking at the generated bytecode (good old javap
is your friend). I am not posting the disassembled code because that would spoil the fun. But once you look at it, the answer appears to be quite obvious.
Cool! That was so impressive.
Cool! That was so impressive. I am still learning on how to do this.
Cool! That was so impressive.
Cool! That was so impressive. I am still learning on how to do this.
And it took you five whole
And it took you five whole minutes https://bestguitarlessonsdallas.com
I learned a lot from this.
I learned a lot from this. Keep sharing!
Thanks for sharing this
Thanks for sharing this brilliant idea! I will definitely try this.
Thank you for this very
Thank you for this very informative content. It's great to find great read. Continue posting. https://hobartroofing.com/
Amazing! Keep it up!
Amazing! Keep it up! www.painterslowell.com
Definitely, something to look
Definitely, something to look forward to!
https://www.leviticusfashions.com
I appreciate how you explain
I appreciate how you explain your points in here. Much thanks!
This is more brainer, I tried
This is more brainer, I tried this.
Impressive. I know how hard
Impressive. I know how hard it is to make this with no mistake. Great sample:)
Thank you for this very
Thank you for this very informative content. It's great to find great read. Continue posting. https://lakemacquariedecking.com.au/pool-decking/
The two methods you made is
The two methods you made is absolutely awesome. I wish I could do that also.
Eclipse spoiled the fun for
Eclipse spoiled the fun for me, since it highlights comparisons between equal expressions (since Galileo I believe), and yes, the Eclipse code analyser gets this right. My first guess when reading the post was right, but I wouldn't have put money on it.
Here's the relevant JLS section: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#1...
I had a marvelous time
I had a marvelous time ruining everything https://inhomecarespokane.com
Awesome.. This is incredible
Awesome.. This is incredible post. What a great work of yours. I admire your content. This will be shared to others. https://www.bathroomrenovationscentralcoast.com/
This is definitely
This is definitely puzzling!
https://www.charlottejunkremovalservice.com/
Pingback
So I just behave for you
So I just behave for you https://privateinvestigatoromaha.com
The statement are so
The statement are so satisfying. I love your post. This is so impressive. What more impressive are what is in my website. Do a visit here
https://lakemacquariedecking.com.au/
Awesome.. This is incredible
Awesome.. This is incredible post. What a great work of yours. I admire your content. This will be shared to others. https://buildinginspectorsnewcastle.com.au/building-inspectors-lake-macq...
Love this update!
Love this update!
Agreed! I find this quite
Agreed! I find this quite awesome. and has shared with my friends as well.
Very interesting blog. A lot
Very interesting blog. A lot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know.
thank you for so much for the
thank you for so much for the content https://www.roofingspecialistscheltenham.co.uk