This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What does this mean: Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "What does this mean: "int x = true ? 10 : 20 ; "" Watch "What does this mean: "int x = true ? 10 : 20 ; "" New topic
Author

What does this mean: "int x = true ? 10 : 20 ; "

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
class XTC20 {
public static void main ( String [ ] args ) { // ---> line 1
int x = true ? 10 : 20 ; // ---> line 2
System . out . println ( x ) ; // ---> line 3
}
};
What does code in line 2 mean?
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
booleanExpression ? expression1 : expression2;
expression1 is your answer if the booleanExpression is true else it is expression2.
Its a conditional statement, an if else statement.
[ November 08, 2002: Message edited by: kevin comario ]
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
It means: if condition is true, than asign the first value after the question mark to the variable, else the second. The condition is BEFORE the question mark, so the layout looks like this:
int x = <condition> ? <assign value to x if condition is true> : <assign value to x if condition is value>
In your case this would mean:
if true = true then x is 10, else x = 20. In your case x = always 10, because true is always true.
An example that would make (a little) more sense is this one


if you compile (javac XTC20) it, you can run it like this:
> java XTC20 10
output will be 10
> java XTC20 20
output will be 20
> java XTC20 30
output will be 20

have fun!
Erik
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The "query-colon" operator (which is a short hand if-else operator) is Java's only ternary operator.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
jiang jiangwei
Greenhorn

Joined: Nov 12, 2002
Posts: 6
conditionExpression?
executeExpression1:
executeExpression2
if the "conditionExpression" is true then run the
"executeExpression1" else run latter.
executeExpression is not only value ,it also could be function.
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
It is called a conditional or ternary operator because it requires three operands and two operators. C and C++ have is functionality as well.


Mr. C<br /> <br />Author and Instructor<br />My book:<br /><a href="http://www.aw-bc.com/catalog/academic/product/0,1144,1576761614,00.html" target="_blank" rel="nofollow">http://www.aw-bc.com/catalog/academic/product/0,1144,1576761614,00.html</a>
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What does this mean: "int x = true ? 10 : 20 ; "
 
Similar Threads
Cute message from Mr. Compiler
intersting
?: operator
static variables
Operator ?: doubt