What does “?” mean in Java? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-21 07:59:05

问题


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)


回答1:


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.




回答2:


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



回答3:


it means:

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

Its called a ternary operator See this java example too




回答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!";




回答5:


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




回答6:


"? :" 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




回答7:


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.




回答8:


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.




回答9:


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.



来源:https://stackoverflow.com/questions/12006170/what-does-mean-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!