aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ternary statement" Watch "ternary statement" New topic
Author

ternary statement

mark crannage
Greenhorn

Joined: Jul 15, 2006
Posts: 13
Hi,

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 ?

thanks,

mc

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

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

thanks,

Mark
Rishi Kanth
Ranch Hand

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

Condition?true:false;

Rishi
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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).
 
 
subject: ternary statement