jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ternary statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ternary statement" Watch "ternary statement" New topic

ternary statement

mark crannage

Joined: Jul 15, 2006
Posts: 13

Can ternary operators be used instead of simple if else statements ?

eg. (i>5)?doSomething() : DoSomethingElse();

Ive tried coding similar to this but with no success. Can terenary operators only be used to assign values ?

eg. booleanVar = (j>5)?true:false;

is there any other way to do the same logic as a simple if else statement ?



(disabled smily)
[ July 26, 2006: Message edited by: Barry Gaunt ]
mark crannage

Joined: Jul 15, 2006
Posts: 13
ha, i dont think any compiler will compile the smiley face, replace that with :


Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
Hey You can use the teneray operators instead of a If-else statement


Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
No, you can't use the ternary operator like that.

This is from the Java Language Specification.

Note that it is a compile-time error for either the second or the third operand expression to be an invocation of a void method. In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear (�14.8).
I agree. Here's the link: http://aspose.com/file-tools
subject: ternary statement
It's not a secret anymore!