jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Ternary Operator behavior in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Ternary Operator behavior in JAVA" Watch "Ternary Operator behavior in JAVA" New topic
Author

Ternary Operator behavior in JAVA

Bishnusaran Sahu
Greenhorn

Joined: May 01, 2008
Posts: 1
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

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Oct 13, 2005
Posts: 39395
    
  28
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

Joined: Oct 13, 2005
Posts: 39395
    
  28
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Oct 13, 2005
Posts: 39395
    
  28
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

Joined: Nov 04, 2009
Posts: 2066

That's OK. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ternary Operator behavior in JAVA