Java – Operators

Operators are used to carrying out operations on one or more operands. The following are the different types of Java operators:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • Bitwise operators
  • Miscellaneous operators

Java Arithmetic operators

To execute arithmetic operations on two operands, arithmetic operators are employed.

Operator Name Description
+ Addition Add two values
Subtraction Subtract one value from another
* Multiplication Multiply two values
/ Division Divide one value by another
% Modulo Returns remainder of division operation


Java Assignment operators

Assignment operators are used to assigning values from the right-hand side expression to the operand on the left-hand side.

Operator Expression Equivalent to
= a = 5 a = 5
+= a += b a = a + b
-= a -= b a = a – b
*= a *= b a = a * b
/= a /= b a = a / b
%= a %= b a = a % b
&= a &= b a = a & b
|= a |= b a = a | b
^= a ^= b a = a ^ b
>>= a >>= b a = a >> b
>>>= a >>>= b a = a >>> b
<<= a <<= b a = a << b

Note: >>>= is just like the >>= operator, except that the bits shifted in on the left are always zero.

Java Comparison operators

To compare the values of two operands, comparison operators are employed. When the values match, it returns true, and when they don’t, it returns false.

Operator Description
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

 

Java Increment/Decrement operators

To raise or reduce the value of a variable, increment and decrement operators are employed.

Operator Name Description
++x Pre-increment Increases the value of x by 1, then returns x.
x++ Post-increment Returns x, then increases the value of x by 1.
–x Pre-decrement Decreases the value of x by 1, then returns x.
x– Post-decrement Returns x, then decreases the value of x by 1.

 

Java Logical operators

To combine two or more criteria, logical operators are needed.

Operator Name Description
&& AND Returns true when all conditions are true
|| OR Returns true when any of the conditions is true
! NOT Returns opposite boolean result

 

Java Bitwise operators

To execute bitwise operations on two operands, bitwise operators are employed.

Operator Name Description
& AND Returns 1 if both bits at the same position in both operands are 1, else returns 0
| OR Returns 1 if one of two bits at the same position in both operands is 1, else returns 0
^ XOR Returns 1 if only one of two bits at the same position in both operands is 1, else return 0
~ NOT Reverse all the bits
>> Right shift The left operand is moved right by the number of bits present in the right operand
>>> Right shift with Zero This operator is just like the >> operator, except that the bits shifted in on the left are always zero.
<< Left shift The left operand value is moved left by the number of bits present in the right operand

 

Java Miscellaneous operators

Other Java operators are described in the table below:

Operator Description
instanceof operator Returns true if the object is an instance of a specific type (class, subclass or interface), false otherwise.
ternary operator (?:) Returns one of the two values based on the value of the boolean expression.

 

Java Operators Precedence

Operator precedence (order of operations) refers to a set of rules that specify which procedures should be executed first when evaluating a given expression.

Multiplication, for example, takes precedence over addition. As a result, the phrase 1 + 2 x 3 is understood as 1 + (2 x 3) = 7, rather than (1 + 2)x  3 = 9. When an expression contains an exponent, it takes precedence over both addition and multiplication. As a result, 3 + 5^2 = 28 and 3 x 5^2 = 75.

The precedence and associativity of Java operators are listed in the table below. Operators are listed in ascending order from top to bottom. Operators with higher precedence are rated first, followed by operators with lower precedence. The associativity of the operators dictates the sequence in which operations are done when they have the same precedence.

Precedence Operator Description Associativity
16 . Object member access Left to Right
[ ] Array element access
( ) Parentheses
15 a++  a– Postfix Increment, Postfix Decrement NA
14 ! Logical NOT Right to Left
~ Bitwise NOT
+a  -a Unary plus, Unary negation
++a  –a Prefix Increment, Prefix Decrement
13 ( ) Cast
new Object creation
12 *  /  % Multiplication, Division, Remainder Left to Right
11 +  – Addition, Subtraction, string concatenation
10 <<  >>  >>> Bitwise left shift, right shift and unsigned right shift
9 <  <=  >  >= Less than, Less than or equal, Greater than, and Greater than or equal
instanceof Instance of Object
8 ==  != Equality, Inequality
7 & Bitwise AND
6 ^ Bitwise XOR
5 | Bitwise OR
4 && Logical AND
3 || Logical OR
2 a?b:c Conditional (ternary) operator Right to Left
1 = Direct assignment
+=  -=  *=  /=  %= Compound assignment by sum, difference, product, quotient and remainder
<<=  >>=  >>>= Compound assignment by Bitwise left shift, right shift and unsigned right shift
&=  ^=  |= Compound assignment by Bitwise AND, XOR and OR

 

Next: Java Strings

Leave a Reply

Your email address will not be published. Required fields are marked *