What does the "?" key do in Java? [duplicate] - java

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 8 years ago.
I've been trying to Google it, but googling the key "?" doesn't really work out that good.
I really want to know what it does and when to use it.
Thanks!
I've seen it a couple times, but here is an example of one I just saw
String name = perms.calculateRank().getColor() + player.getName();
//This is a custom ranking system ^
player.setPlayerListName(name.length() > 15 ? name.substring(0, 16) : name);
player.setDisplayName(name + ChatColor.RESET);
Chat.sendMessage(player, "Tab Name Set");

This is a ternary operator. In Java specifically, it is called the Conditional Operator. It is a way of writing short-hand simple if..else statements. For example:
if (a == b) {
c = 123;
} else {
c = 456;
}
is the same as:
c = a == b ? 123 : 456;

It is also used for a wildcard generic.
public List<?> getBizarreList();

The ternary operator someBoolean ? x : y evaluates to x if someBoolean is true, and y otherwise.

It is called ternary operator and it is only operator that takes 3 operands. In better sense, it is conditional operator that represent shorter format
General Syntax :
boolean expression ? value1 : value2
your example:
player.setPlayerListName(name.length() > 15 ? name.substring(0, 16) : name);
as same as
if( name.length() > 15)
player.setPlayerListName(name.substring(0, 16));
else
player.setPlayerListName(name);

Related

what is the meaning and use of line... int ct = count.containsKey(ch) ? count.get(ch) : 0; in the code? [duplicate]

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 7 years ago.
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();
}
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.");
Thats an if/else statement equilavent to
if(row % 2 == 1){
System.out.print("<");
}else{
System.out.print("\r>");
}
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
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.
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);
}
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 ();
it is a ternary operator and in simple english it states "if row%2 is equal to 1 then return < else return /r"

what does "0" stand for in conditional operator? [duplicate]

This question already has answers here:
Ternary Operator
(4 answers)
Closed 4 years ago.
Consider the following code:
i = (i == array.length-1) ? 0 : i + 1;
As I understand, the conditional operator works as follows:
booleanCondition ? executeThisPartIfBooleanConditionIsTrue : executeThisPartIfBooleanConditionIsFalse
What does 0 execute?
I do not think the "positive conditional test result" really has a formal name.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
?: Ternary (shorthand for
if-then-else statement)
using a boolean example
isConditionTrue = 1 == 2 ? true : false;
this in your example
i = (i == array.length-1) ? 0 : i + 1;
has the same result as
if (i == array.length-1)
{i= 0 ;}
else {i = i + 1;}
This is known as the Elvis Operator (see [https://en.wikipedia.org/wiki/Elvis_operator]) and given the following example:
x = A ? B : C;
... it means that if A is evaluated to 'true' than x gets assigned the value B otherwise it gets assigned the value C.
In your example it means, that if 'i==array.length-1' then 'i' is set to '0' otherwise 'i' is set to 'i+1'.

Could someone explain this bit of code to me (simple) [duplicate]

This question already has answers here:
What is a Question Mark "?" and Colon ":" Operator Used for? [duplicate]
(7 answers)
Closed 7 years ago.
I have these two methods. I understand the "getTotalSalary" one but don't really get the way in which "getAverageSalary" is written. I don't understand why the question mark and colon is used as well as the "(size() != 0)" and 0 at the end.
This is the coding:
public double getTotalSalary() {
double total = 0;
for (Employee e : empReg) {
total = total + e.getSalary();
}
return total;
}
public double getAverageSalary() {
return (size() != 0) ? this.getTotalSalary() / this.size() : 0;
}
empReg is the name of the ArrayList. Employee is a class which consists of "name" and "salary". getSalary is obviously a method returning the salary.
The question mark is called the ternary operator, and it is used to make a decision based on an evaluation. It is often used to replace if statements, since they do the same thing. For example, an if statement with that would be written:
if (size != 0)
return this.getTotalSalary() / this.size();
else
return 0;
In my experience, I only use it if I want to reduce the code size. However, it does make the code a bit more difficult to read.
You cannot divide by zero.
? and : is a ternary operator. It means that if the expression before ? is true, then this.getTotalSalary() / this.size()will be returned, otherwise return 0.
It's called ternary operator in java, here you have some examples: http://alvinalexander.com/java/edu/pj/pj010018
See this discussion: What is a Question Mark "?" and Colon ":" Operator Used for?
It explains
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;

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

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 7 years ago.
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();
}
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.");
Thats an if/else statement equilavent to
if(row % 2 == 1){
System.out.print("<");
}else{
System.out.print("\r>");
}
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
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.
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);
}
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 ();
it is a ternary operator and in simple english it states "if row%2 is equal to 1 then return < else return /r"

Single expression in ternary operator in Java

I know you can have
String answer = (5 == 5) ? "yes" : "no";
Is it somehow possible to have only:
String answer = (5 == 5) ? "yes";
It gives a compile error when I try.
NOTE: (5==5) is just an example. In its place will be statement which could be either true or false.
if one line is important
String answer = (5 == 5) ? "yes": null;
Since a String's default value is null.
You're looking for an if statement:
if (5 == 5)
answer = "yes";
Your idea is impossible because an expression (such as the conditional value) must always have a value.
In your code, if 5 != 5, the expression would have no value, which wouldn't make any sense.
No. you can't have it. You need to specify both the ? and :.
Use a straight if.

Categories