It's not a secret anymore!
The moose likes Beginning Java and the fly likes syntax question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "syntax question?" Watch "syntax question?" New topic

syntax question?

cardwell cupp
Ranch Hand

Joined: Aug 16, 2001
Posts: 66
I have been reviewing some code lately, and have come across a line that uses a ? and a : I am assuming that this is some kind of shortcut for an if statment. Is that correct. Could anyone give me an explanation for those 2 symbols? How do you use them.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Cardwell,
Yes you are right. It is called conditional statement. An example:
System.out.println( "x = " + (x == null) ? null : x.intValue() );
The above example is equal to the following:
if( x == null )
System.out.println( "x = " + null );
System.out.println( "x = " + x.intValue() );
The '?' character means choose one of the following. The ':' character separates the two choices. The first choice is the 'true' value while the second one is the 'false' value.
I agree. Here's the link:
subject: syntax question?
It's not a secret anymore!