Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>Operator Types in Java

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



Types of Operators and Operator Precedence in Java

Operators in Java are special symbols those perform specific operations on one, two, or three operands, and then return a result


Types of Operators in Java

  • Arithmetic Operators

  • Relational Operators

  • Bitwise Operators

  • Logical Operators

  • Assignment Operators

  • Unary Operators

  • Misc Operators


Arithmetic Operators

You can 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.

Operator Description
+ Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator

Relational Operators

The relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand.

Table shows all relation operators supported by Java.

operator description
== Check if two operand are equal
!= Check if two operand are not equal.
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand

Logical operators

Java supports following 3 logical operator. Suppose a=1 and b=0;

operator description example
&& Logical AND (a && b) is false
|| Logical OR (a || b) is true
! Logical NOT (!a) is false

Bitwise operators

Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte

operator description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift

Assignment Operators

One of the most common operators that you'll encounter is the simple assignment operator "=", it assigns the value on its right to the operand on its left:

operator description example
= assigns values from right side operands to left side operand a=b
+= adds right operand to the left operand and assign the result to left a+=b is same as a=a+b
-= subtracts right operand from the left operand and assign the result to left operand a-=b is same as a=a-b
*= mutiply left operand with the right operand and assign the result to left operand a*=b is same as a=a*b
/= divides left operand with the right operand and assign the result to left operand a/=b is same as a=a/b
%= calculate modulus using two operands and assign the result to left operand a%=b is same as a=a%b

Unary Operators

The unary operators require only one operand;

Operator Description
+ Unary plus operator; indicates positive value (numbers are positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean

Miscellaneous Operators

Conditional Operator ( ? : )

Operator Description
? : It is called ternary operator, is checks a condition similar like if else
Instanceof The instanceof operator compares the object with a class.

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −

variable x = (expression) ? value if true : value if false


Java Operator Precedence

Java has well-defined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators. An order in which the operators in an expression are evaluated called operator precedence. Precedence rules can be overridden by explicit parentheses.

The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence.

Operator Description Level Associativity
[] . () ++ -- access array element access object member invoke a method post-increment post-decrement 1 left to right
++ -- + - ! ~ pre-increment pre-decrement unary plus unary minus logical NOT bitwise NOT 2 right to left
() new cast object creation 3 right to left
* / % multiplicative 4 left to right
+ - + additive string concatenation 5 left to right
<< >> >>> shift 6 left to right
< <= > >= instanceof relational type comparison 7 left to right
== != equality 8 left to right
& bitwise AND 9 left to right
^ bitwise XOR 10 left to right
| bitwise OR 11 left to right
&& conditional AND 12 left to right
|| conditional OR 13 left to right
?: conditional 14 right to left
= += -= *= /= %= &= ^= |= <<= >>= >>>= assignment 15 right to left

Share the article to help your friends