• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ternary Operator behavior in JAVA

 
Bishnusaran Sahu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Just run it and could you please let me know if there is any supporting doc from sun or somewhere else, where it says why the code doesn’t work.

No need to overcome the problem. Since we can get the expected result by twisting the code a bit or simply by using different variable or by using pre-incremental operation i.e. ++count1/++count2 .
My requirement is to know why does this happen in case of below particular situation?

Please refer the below code:



Output: I tested in java 5 and found that variable count2 does not get incremented.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't a problem with Ternary Operator behavior, and, the hint is, check the below code:

Could you please predict the values for i & k. And check them with a code snaps. You'll get them.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, Welcome to JR, => UseCodeTags when you post code snaps. And, use the EDIT button and insert the code tags. They will be more readable.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:And, Welcome to JR, ⇒ UseCodeTags . . .
Agree. Since you are new, I shall add the tags and you can see how much better it looks.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a common source of confusion for beginners. A search of this forum reveals this post of mine, with useful links in.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Abimaran Kugathasan wrote:And, Welcome to JR, ⇒ UseCodeTags . . .
Agree. Since you are new, I shall add the tags and you can see how much better it looks.

You are so nice! And further, I let him to search and find the appropriate answer. But, you gave the exact answer~!

Thanks!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote: . . . I let him to search and find the appropriate answer. But, you gave the exact answer~!

Thanks!
You're welcome. The post increment and post decrement operators cause no end of confusion, and they are very difficult to search for on JavaRanch because you can't search for ++ or --. So it is much easier if I know where there are old threads about this problem.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's OK. Thanks!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic