File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes question about this code 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 » Beginning Java
Bookmark "question about this code" Watch "question about this code" New topic
Author

question about this code

Larry Lai
Ranch Hand

Joined: May 13, 2002
Posts: 44
Hi everyone,
why this code has a "?"
radius= (circleRadius>=0.0? circleRadius:0.0);


Thanks

Larry
Ken Cobbs
Greenhorn

Joined: Jun 18, 2002
Posts: 29
Sorry about that blank post. Tyring to figure out how to make a post.
Any way, I believe that the '?' your asking about is like putting an 'if' statement in the middle of an expression. The example I see in 'Just Java2' 5th edition gives an example on page 131.
So
radius = ( circleRadius >= 0.0 ? circleRadius : 0.0 );
would mean that circleRadius would be evaluated if it 's >= 0.0 (true) and 0.0 if it's not >= 0.0(false).
My first post. Hope I didn't confuse 'ya.
Thanks,
Ken
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ken, you can delete the dead post via the edit icon.
The expression "boolean expression" ? "true alternative" : "false alternative" has been inherited from C/C++.
If "boolean expression" is true then the result of the expression is the result of the "true alternative" expression, else it is the result of the "false alternative" expression. The type of the result expression is the "widest" of the types of the two alternatives.
So true ? 9 : -1.0 will result in 9.0 (a double) being returned, and false ? 9 : -1.0 will result in -1.0 (also a double) being returned.
Its a bit tricky so look this up in your Java books because it can appear in Java Certification exams.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Larry Lai
Ranch Hand

Joined: May 13, 2002
Posts: 44
Hey Barry,
Thanks a lot
I understand it now.
Larry
Ken Cobbs
Greenhorn

Joined: Jun 18, 2002
Posts: 29
Thanks Barry,
I'll get this posting down(sometime).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about this code