0

I'm really sorry for what must be a really stupid question. I don't exactly have formal training in java and a lot of times, when looking through code, I might see something like:

(  ) ?   : 

as in something like:

for (str == null) ? getString(this) : dontGetit(nope.this);

honestly i don't even know exactly it is or if it's remotely close, but hopefully one could recognize the scheme. I was hoping one could perhaps link some documentation on this because i have trouble even searching for it.

5 Answers 5

2

It is ternary operator in Java. Read here : http://www.janeg.ca/scjp/oper/ternary.html

Sign up to request clarification or add additional context in comments.

Comments

2

it an ternary operator used to evaluate boolean expressions. It is equivalent to if-else statement.

Syntax:

   variable_name= (boolean expression) ? value to assign if true : value to assign if false

using terenary operator:

    boolean isHappy = true;
    String mood = (isHappy == true)?"I'm Happy!":"I'm Sad!";

using if-else:

 if(isHappy) {
     mood="I'm Happy";
  }
  else {
     mood = "I'm, Sad!";
   }

Comments

1

this is conditional or ternary operator in java

( a ) ? b  : c;

if a is true b will be executed or run

if a is false c will be executed or run

Comments

1

It's the ternary operator . It's good in sets, and a shortcut for iteration

java ternary operator

Here's an easy ex.:

boolean isObese = true;

 String mood = (isObese == true)?"I need to quit fast-food!":"I'm healthy!";

..source

Comments

1

It's called ternary operator, it can be allocated to a variable and it is equivalent of an if-then-else statement.

Have a look at this tutorial and pay attention on something that hasn't been underlined enough by the other answers: the fact that the ternary operator is an expression that can be allocated to a variable or can be returned directly by a method. It has a functional feel into it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.