13

I dont know what the question mark (?) stand for in java, I was doing a small program, a Nim-game. were looking in a book, for help and saw this statement:

int pinsToTake = (min >= 2) ? 2 : 1;

I don't understand it, what will ? represent, can it be something to do with if-statement but you put it in a variable? and the : can be something "else"? (this things that I just said can be very misleading)

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Alexein
  • 666
  • 1
  • 11
  • 19

9 Answers9

31

someval = (min >= 2) ? 2 : 1;

This is called ternary operator, which can be used as if-else. this is equivalent to

if((min >= 2) {
   someval =2;
} else {
   someval =1
}

Follow this tutorial for more info and usage.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
8

Its ternary operator also referred to as the conditional operator, have a look reference

like Object bar = foo.isSelected() ? getSelected(foo) : getSelected(baz);

eg. operand1 ? operand2 : operand3

  • if operand1 is true, operand2 is returned, else operand3 is returned
  • operand1 must be a boolean type
  • operand1 can be an expression that evaluates to a boolean type
  • operand1 and operand2 must be promotable numeric types or castable object references, or null
  • if one of operand2 or operand3 is a byte and the other a short, the type of the returned value will be a short
  • if one of operand2 or operand3 is a byte, short or char and the other is a constant int value which will fit within the other operands range, the type of the returned value will be the type of the other operand
  • otherwise, normal binary numeric promotion applies
  • if one of operand2 or operand3 is a null, the type of the return will be the type of the other operand
  • if both operand2 and operand3 are different types, one of them must be compatible (castable) to the other type reference
Harmeet Singh
  • 2,555
  • 18
  • 21
4

it means:

if(min >= 2) 
   someval =2;
else 
   someval =1

Its called a ternary operator See this java example too

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
4

That's a Ternary Operator. Check Oracle's doc for further info. Long story short, it is an if-else statement that can be done in a single line and used inside methods and to define variable values.

Syntax:

boolean_expression ? do_if_true : do_if_false;

Parallelism with if-else statement:

if(boolean_expression)
    //do_if_true;
else 
    //do_if_false;

I didn't use brackets on purpose, since you can only execute one line of code in do_if_true and do_if_false.

Example of use:

boolean hello = true;
String greetings = hello ? "Hello World!" : "No hello for you...";

This will set someString as "Hello World!" since the boolean variable hello evaluates to true. On the other hand, you can nest this expressions:

boolean hello = true;
boolean world = false;

String greetings = hello ? (world ? "Hello World!" : "Hello Stranger!") : "No hello for you...";

In this case, greetings will have as a value "Hello Stranger!";

Fritz
  • 9,987
  • 4
  • 30
  • 49
1

It's called the Ternary If operator, it's just short-hand for an if...else

codebox
  • 19,927
  • 9
  • 63
  • 81
0

"? :" is a ternary operator equivalent to an if else statement.

In your example:

   pinsToTake = (min >= 2) ? 2 : 1 

if min >= 2 then assign 2 to pinsToTake, else assign 1

Razvan
  • 9,925
  • 6
  • 38
  • 51
0
max = (a > b) ? a : b;

(a > b) ? a : b; is an expression which returns one of two values, a or b.
The condition, (a > b), is tested. If it is true the first value, a, is returned. If it is false, the second value, b, is returned.
Whichever value is returned is dependent on the conditional test, a > b. The condition can be any expression which returns a boolean value.

0

It is called conditional operator.This is how it works. if min is greater than or equal to 2 ,then first value after ? that is 2 here will be assign to corresponding variable ,otherwise second value that is 1 here will be assign.

Amrit Pal
  • 23
  • 1
  • 6
0

This link will tell you all you need.

Summary for archival sakes:

It's called the conditional operator. It's a ternary operator that takes three terms:

BooleanExpression ? Expr1 : Expr2

The BooleanExpressionis evaluated. If it's true, the value of the whole expression is Expr1. If it's false, the value of the whole expression is Expr2.

So it serves the same kind of purpose as an if statement, but it's a term rather than a whole statement. That means you can embed it in places where you can't use a whole statement.

Michael A
  • 9,480
  • 22
  • 70
  • 114