Why using '|' in java [duplicate] - java

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.

Related

Ternary operation on a hashMap forbidden by 'Not a statement' [duplicate]

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);

What does this lean of code mean? [duplicate]

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
capacity = (cap > 0) ? cap : CAPACITY;
I'm just looking through my lecture notes, and I can't figure out what this line of code does. Can someone help me?
It's called conditional expression and in your case it means that if cap is greater than 0, the capacity variable will get the value "cap", if false then it will get the value "CAPACITY".

Java plus equals or equals plus operator? [duplicate]

This question already has answers here:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 7 years ago.
Is += the same as =+?
I can't find any reason for why the plus sign is reversible.
For what reasons would I need to use one of the other? Where can i find docs on this i tried searching but didnt see the use of both.
It's not the same.
x+=5 is equivalent to x=x+5.
x=+5 (or x=(+5)) is equivalent to x=5.

Python inline if when setting variable [duplicate]

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.

why does 2^0 return 2 in java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the ^ operator do in Java?
The power ^ in Java?
I am sorry if this is a duplicate, but i didnĀ“t found anything in SO.
So can someone explaint me why
System.out.println((2^0));
this does return 2?
i was expecting a 1.
Because the ^ operator does not mean "raise 2 to the 0th power". It's a bitwise exclusive OR operator.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
In order to do that, your code should look like this:
double one = Math.pow(2.0, 0.0); // Silly, but you can do it.
Don't be surprised if the answer comes out to something that's not exactly 1.0. You'll need to know about how floating point numbers work.
The ^ sign means XOR and not pow. Try Math.Pow(2.0, 0.0) instead.
^ in Java is Bitwise exclusive-OR.
so 2(1 0) ^(XOR) 0(0 0) =1 0 ie 2 !!!
Got it?

Categories

Resources