What is a Question Mark "?" and Colon ":" Operator Used for?

JavaOperatorsTernary OperatorConditional Operator

Java Problem Overview


Two questions about using a question mark "?" and colon ":" operator within the parentheses of a print function: What do they do? Also, does anyone know the standard term for them or where I can find more information on their use? I've read that they are similar to an 'if' 'else' statement.

int row = 10;
int column;
while (row >= 1)
{
	column = 1;
	while(column <= 10)
	{
		System.out.print(row % 2 == 1 ? "<" : "\r>");
		++column;
	}
	--row;
	System.out.println();
}

Java Solutions


Solution 1 - Java

This is the ternary conditional operator, which can be used anywhere, not just the print statement. It's sometimes just called "the ternary operator", but it's not the only ternary operator, just the most common one.

Here's a good example from Wikipedia demonstrating how it works:

> A traditional if-else construct in C, Java and JavaScript is written: > > if (a > b) { > result = x; > } else { > result = y; > } > > This can be rewritten as the following statement: > > result = a > b ? x : y;

Basically it takes the form:

boolean statement ? true result : false result;

So if the boolean statement is true, you get the first part, and if it's false you get the second one.

Try these if that still doesn't make sense:

System.out.println(true ? "true!" : "false.");
System.out.println(false ? "true!" : "false.");

Solution 2 - Java

Thats an if/else statement equilavent to

if(row % 2 == 1){
  System.out.print("<");
}else{
  System.out.print("\r>");
}

Solution 3 - Java

a=1;
b=2;

x=3;
y=4;

answer = a > b ? x : y;

answer=4 since the condition is false it takes y value.

A question mark (?)
. The value to use if the condition is true

A colon (:)
. The value to use if the condition is false

Solution 4 - Java

Also just though I'd post the answer to another related question I had,

a = x ? : y;

Is equivalent to:

a = x ? x : y;

If x is false or null then the value of y is taken.

Solution 5 - Java

Maybe It can be perfect example for Android, For example:

void setWaitScreen(boolean set) {
	findViewById(R.id.screen_main).setVisibility(
			set ? View.GONE : View.VISIBLE);
	findViewById(R.id.screen_wait).setVisibility(
			set ? View.VISIBLE : View.GONE);
}

Solution 6 - Java

They are called the ternary operator since they are the only one in Java.

The difference to the if...else construct is, that they return something, and this something can be anything:

  int k = a > b ? 7 : 8; 
  String s = (foobar.isEmpty ()) ? "empty" : foobar.toString (); 

Solution 7 - Java

it is a ternary operator and in simple english it states "if row%2 is equal to 1 then return < else return /r"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeependView Question on Stackoverflow
Solution 1 - JavaBrendan LongView Answer on Stackoverflow
Solution 2 - JavafmucarView Answer on Stackoverflow
Solution 3 - JavarbeView Answer on Stackoverflow
Solution 4 - Javamoo mooView Answer on Stackoverflow
Solution 5 - JavamehmetView Answer on Stackoverflow
Solution 6 - Javauser unknownView Answer on Stackoverflow
Solution 7 - JavaAnurag RamdasanView Answer on Stackoverflow