What does the += operator do in Java?
The "common knowledge" of programming is that x += y
is an equivalent shorthand notation of x = x + y
. As long as x
and y
are of the same type (for example, both are int
s), you may consider the two statements equivalent.
However, in Java, x += y
is not identical to x = x + y
in general.
If x
and y
are of different types, the behavior of the two statements differs due to the rules of the language. For example, let's have x == 0
(int) and y == 1.1
(double):
int x = 0;
x += 1.1; // just fine; hidden cast, x == 1 after assignment
x = x + 1.1; // won't compile! 'cannot convert from double to int'
+=
performs an implicit cast, whereas for +
you need to explicitly cast the second operand, otherwise you'd get a compiler error.
Quote from Joshua Bloch's Java Puzzlers:
(...) compound assignment expressions automatically cast the result of
the computation they perform to the type of the variable on their
left-hand side. If the type of the result is identical to the type of
the variable, the cast has no effect. If, however, the type of the
result is wider than that of the variable, the compound
assignment operator performs a silent narrowing primitive
conversion [JLS 5.1.3].
What is the Java ?: operator called and what does it do?
Yes, it is a shorthand form of
int count;
if (isHere)
count = getHereCount(index);
else
count = getAwayCount(index);
It's called the conditional operator. Many people (erroneously) call it the ternary operator, because it's the only ternary (three-argument) operator in Java, C, C++, and probably many other languages. But theoretically there could be another ternary operator, whereas there can only be one conditional operator.
The official name is given in the Java Language Specification:
§15.25 Conditional Operator ? :
The conditional operator
? :
uses the boolean value of one expression to decide which of two other expressions should be evaluated.
Note that both branches must lead to methods with return values:
It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.
In fact, by the grammar of expression statements (§14.8), it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.
So, if doSomething()
and doSomethingElse()
are void methods, you cannot compress this:
if (someBool)
doSomething();
else
doSomethingElse();
into this:
someBool ? doSomething() : doSomethingElse();
Simple words:
booleanCondition ? executeThisPartIfBooleanConditionIsTrue : executeThisPartIfBooleanConditionIsFalse
What does the |= operator do in Java?
|=
is a bitwise-OR-assignment operator. It takes the current value of the LHS, bitwise-ors the RHS, and assigns the value back to the LHS (in a similar fashion to +=
does with addition).
For example:
foo = 32; // 32 = 0b00100000
bar = 9; // 9 = 0b00001001
baz = 10; // 10 = 0b00001010
foo |= bar; // 32 | 9 = 0b00101001 = 41
// now foo = 41
foo |= baz; // 41 | 10 = 0b00101011 = 43
// now foo = 43
What does the arrow operator, '->', do in Java?
That's part of the syntax of the new lambda expressions, to be introduced in Java 8. There are a couple of online tutorials to get the hang of it, here's a link to one. Basically, the ->
separates the parameters (left-side) from the implementation (right side).
The general syntax for using lambda expressions is
(Parameters) -> { Body }
where the ->
separates parameters and lambda expression body.
The parameters are enclosed in parentheses which is the same way as for methods and the lambda expression body is a block of code enclosed in braces.
What does :: Java operator do in this context?
These are method references. It's just a simpler way to write a lambda expression:
.map(Tests::doubleIt)
is equivalent to
.map(i -> Tests.doubleIt(i))
You can also refer to instance methods using someObject::someMethod
, or even to constructors using SomeClass::new
.
What is the /= operator in Java?
It's a combination division-plus-assignment operator.
a /= b;
means divide a
by b
and put the result in a
.
There are similar operators for addition, subtraction, and multiplication: +=
, -=
and *=
.
%=
will do modulus.
>>=
and <<=
will do bit shifting.
What does += operator do?
From:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1.
What does the >> symbol mean in Java?
12 is 1100
in binary. A right shift (>> is the bitwise right shift operator) by one bit produces
1100 -> 0110
which comes out to be 6.
Thus we have that,
6 - 1 = 5
Related Topics
What Is a Nosuchbeandefinitionexception and How to Fix It
Custom Thread Pool in Java 8 Parallel Stream
What Does "Possible Lossy Conversion" Mean and How to Fix It
How to Override Equals Method in Java
Calling Remove in Foreach Loop in Java
How to Return Multiple Objects from a Java Method
How to "Decompile" Java Class Files
How to Measure Time Elapsed in Java
Splitting a Java String by the Pipe Symbol Using Split("|")
What's the Difference Between Instant and Localdatetime
How to Build Jars from Intellij Properly
How to Lock a File Using Java (If Possible)
How to Put a Control in the Jtableheader of a Jtable
How to Set the Environment Variables For Java in Windows
How to Compare Objects by Multiple Fields
Want Current Date and Time in "Dd/Mm/Yyyy Hh:Mm:Ss.Ss" Format