-1

This may be a very stupid question, but what does this line in java mean?

Seat tempSeat = rowClass ? allSeatsC[i][j] : allSeatsE[i][j];

I know it has something to do with an if function but I was trying to re-write it with if. But I just cannot figure out how does it work.

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
Fellow Rémi
  • 159
  • 1
  • 1
  • 9
  • This is called the conditional operator. See http://stackoverflow.com/questions/2615498/java-conditional-operator-result-type for more. – Marius Bancila Mar 20 '14 at 11:05
  • possible duplicate of [Question mark and colon mean in statement? what does it mean?](http://stackoverflow.com/questions/6957214/question-mark-and-colon-mean-in-statement-what-does-it-mean) – Scott Solmer Mar 20 '14 at 11:06

3 Answers3

0
Seat tempSeat;
if(rowClass)
    tempSeat=allSeatsC[i][j];
else
    tempSeat=allSeatsE[i][j]
Barmar
  • 741,623
  • 53
  • 500
  • 612
Dev
  • 3,410
  • 4
  • 17
  • 16
0

Its called a ternary or "elvis" operator. Basically an in-line if statement

More info: http://en.wikipedia.org/wiki/%3F:#Java (link gets broken by formatter)

These are really handy for simple tests like the one above.

Geoff Williams
  • 1,320
  • 10
  • 15
0

The ? is a ternary operator.

It is similar to an if-else statement, but it would return the expression between the ? and the :, if the first expression is true, otherwise, it returns the last expression.

Seat tempSeat = rowClass ? allSeatsC[i][j] : allSeatsE[i][j];

is equivalent to

Seat tempSeat;
if(rowClass){
    tempSeat = allSeatsC[i][j];
}else{
    tempSeat = allSeatsE[i][j];
}
Kayla
  • 485
  • 5
  • 14