This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 9 years ago.
In Java, setting a variable to a value based on a condition could be done in one line
like so:
variable = (!true) ? 1 : 2
This would result to '2'.
Is there python equivalent to this code?
Thank you.
variable = 1 if not True else 2
General ternary syntax:
<value_if_true> if <condition> else <value_if_false>
This is called a conditional expression in Python, and is mostly equivalent to the "ternary operator" in C-family languages (although it's not actually an operator). The original proposal PEP 308 has more details.
Related
This question already has answers here:
Ternary operator gives "Not a statement" error [duplicate]
(2 answers)
Java Ternary without Assignment
(4 answers)
Closed 1 year ago.
I am trying to make a simple ternary operation on a hash map:
hashMap.get(number) == 1 ?
hashMap.remove(number) :
hashMap.merge(number, 1, Math::subtractExact);
I am getting a 'Not a statement' error from my IDE. What I don't understand is where this is coming from: both calls to remove and merge sound like statements to me.
You have to assign the value of the ternary operator to some variable:
Integer value =
hashMap.get(number) == 1 ?
hashMap.remove(number) :
hashMap.merge(number, 1, Math::subtractExact);
This question already has answers here:
Why do we usually use || over |? What is the difference?
(28 answers)
Closed 8 years ago.
I was looking at the android source code today and I found this:
if (mLeftDragger.continueSettling(true) | mRightDragger.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
The return type of both these methods is boolean.
What is the purpose of this operator and what happens there actually?
|| short circuits where as | does not. Short circuiting is when the right hand expression does not get evaluated if there is no need for it to be evaluated. | will calculate all Boolean expressions (left to right). Check out this answer.
This question already has answers here:
Operator overloading in Java
(10 answers)
Why doesn't Java need Operator Overloading? [closed]
(10 answers)
Closed 9 years ago.
I think that there is something like this in C/C++, but what about in java? Can I add a method that will add two of them together, but instead of doing say obj1.add(obj2) you can do obj1 + obj2? Can you do the same for -, *, /, and == & other comparatives? Can you create a primitive, or would that take modifying the JVM?
That's called operator overloading. And no Java does not support it. You can find more information here.
No you cannot. There is no operator overloading in Java. The only thing that comes close to an overloaded operator in Java is + which can concatenate strings and perform standard addition.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java “?” Operator for checking null - What is it? (Not Ternary!)
Java Null-pointer-safe accessor
Recently I read in one of the java forums about ?. operator. They wrote that ?. could not make it to the java 7. Can anybody explain what exactly ?. is?
Also, I like to know if this operator has any specific name or not like ?: is known as ternary operator.
It's called the Null-safe operator.
I believe you mean the Null-safe operator, explained here. It was under consideration for Java 7, but subsequently dropped.
The other common use for the ? in Java is the ternary operator, which has been in Java since the dark ages and is explained here.
The two are completely different features however, the only common element is that they use the ? in some way.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Operators : |= bitwise OR and assign example
boolean bAlive;
bAlive |= this.properties.containsKey(name);
In the above , the code uses '|'.
Why using '|' ?
Thanks in advance.
The boolean is being OR'ed with the value on the right.
If this.properties.containsKey(name) is TRUE, then bAlive is set to TRUE.
Otherwise, bAlive remains the same.