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 of g() is dead code.
  • Just g() is invoked and the call-site of f() 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.

Great site thanks admin. More

Great site thanks admin. More power

Thank you for posting this. I

Thank you for posting this. I did not know that this existed and gave me really good information that I can use in the future.

Check out https://pghguttercleaning.com/blog/ for other helpful posts of your interest.

I'm puzzled. This reminds me

I'm puzzled. This reminds me a lot about lorem ipsum. There's a story behind that though. Anyway, please drop by or visit https://www.airanddryerventcleaninggilbert.com/ for quality air duct and dryer vent cleaning in Gilbert, AZ.

Thank you for

Thank you for sharing
http://google.com

Such an informative site!

Such an informative site! Thank you.

I like this site it’s a

I like this site it’s a master piece! Glad I noticed this on google.

Hello, its nice article about

Hello, its nice article about media print, we all know media
is a enormous source of information.

Looking forward to more great

Looking forward to more great posts from this site.

Same here. I'm glad that they

Same here. I'm glad that they shared this with us. Now, I learned something new. Anyway, if you need flooring for your home in Mesa, I suggest you get the installation from https://www.floorinstallationmesa.com for quality with affordability.

Thanks for sharing this one.

Thanks for sharing this one.

Glad to find this site, keep

Glad to find this site, keep on posting

I love coding using Java.

I love coding using Java.

Thanks for sharing this great

Thanks for sharing this great article.

Glad to check this awesome

Glad to check this awesome site. I like the content of this article very informative one.

Great! I really agree with

Great! I really agree with your thoughts!

-Grant

Yes, this is so great! Love

Yes, this is so great! Love

I appreciate how you explain

I appreciate how you explain your points in here. Much thanks!

It was great, Very excited

It was great, Very excited about this update. Thank you so much.

Great work. I am looking

Great work. I am looking forward to this. Thanks for sharing the information.
https://www.treeservicemodesto.com/

This is awesome post, I like

This is awesome post, I like the content you shared.

This is really wonderful!

This is really wonderful! Thank you!

Great article you shared

Great article you shared here.

Thanks for the heads up and

Thanks for the heads up and clarification. What IDE are you using?>

Amazing piece! I'll bookmark

Amazing piece! I'll bookmark this and will share it to a friend.

Great site, I hope to see

Great site, I hope to see more helpful and informative topics here. https://cincinnatiseo.io/

Wow this is good to know, I

Wow this is good to know, I just realized it because of this post. Thanks! By that, if you have any concerns about stamped concrete, contact us!

This is great information,

This is great information, thank you for sharing!

What a nice article provide

What a nice article provide you have made some nice content share i want to share with my friends keep it up dear

This is really great and I

This is really great and I like the content a lot! Thank you!

I am no expert, but I believe

I am no expert, but I believe you just made an excellent point. You certainly fully understand what you are speaking about, and I can truly get behind that.

Cheers for informing me!

Cheers for informing me!

Impressive! thanks for

Impressive! thanks for sharing!

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

[...] Puzzling Java statement of the day | antforge.org antforge.org/blog/2010/07/30/puzzling-java-statement – view page – cached 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. Tweets about this link [...]