aspose file tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "syntax question?" Watch "syntax question?" New topic
Author

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.
Thanks
Cardwell
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 );
}
else
{
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.
Regards,
Manfred.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: syntax question?