Learn about available Java operators, and precedence order and understand their usages with examples. We will also try to understand when to use which operator and what to expect in the result.
1. Java Operators
An operator is a symbol that performs a specific operation on one, two, or three operands, producing a result. The type of the operator and its operands determine the kind of operation performed on the operands and the type of result produced.
Operators in Java can be categorized based on two criteria:
 Number of operands – There are three types of operators based on the number of operands. An operator is called a unary, binary, or ternary operator based on the number of operands. If an operator takes one operand, it is called a unary operator; if it takes two operands, it is called a binary operator; if it takes three operands, it is called a ternary operator.
 Type of operation they perform – An operator is called an arithmetic operator, a relational operator, a logical operator, or a bitwise operator, depending on the kind of operation it performs on its operands.
Operator  Description 

+ – * /  Arithmetic unary or binary operators 
++ —  Increment and decrement unary operators 
== !=  Equality operators 
< > <= =>  Relational operators 
! &   Logical operators 
&&  ?:  Conditional operators 
= += = *= /= %=  Assignment operators 
&= = ^= <<= >>= >>>=  Assignment operators 
&  ~ ^ << >> >>>  Bitwise operators 
> ::  Arrow and method reference operators 
new  Instance creation iterator 
instanceOf  Type comparison operator 
+  String concatenation operator 
Let us learn about few most used operators with examples.
2. Assignment Operator
 An assignment operator (=) is used to assign a value to a variable.
 It is a binary operator. It takes two operands.
 The value of the righthand operand is assigned to the lefthand operand.
 The lefthand operand must be a variable.
//26 is the righthand operand.
//counter is the lefthand operand, which is a variable of type int.
int counter = 26;
Java ensures that the value of the righthand operand of the assignment operator is assignment compatible to the data type of the lefthand operand. Otherwise, a compiletime error occurs.
In case of reference variables, you may be able to compile the source code and get a runtime ClassCastException
error if the object represented by the righthand operand is not assignment compatible to the reference variable as the lefthand operand.
3. Arithmetic Operators
 Operators like (+ (plus), – (minus), * (multiply), / (divide)) are called arithmetic operators in Java.
 It can only be used with numeric type operands. It means, both operands to arithmetic operators must be one of types
byte
,short
,char
,int
,long
,float
, anddouble
.  These operators cannot have operands of
boolean
primitive type and reference type.
int sum = 10 + 20;
int difference = 50  20;
long area = 20l * 30l;
int percentage = 20 / 100;
3.1. Unary Arithmetic Operators
Operator  Description 

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

'+'  Addition – Adds values on either side of the operator 
''  Subtraction – Subtracts right hand operand from left hand operand 
'*'  Multiplication – Multiplies values on either side of the operator 
'/'  Division – Divides left hand operand by right hand operand 
'%'  Modulus – Divides left hand operand by right hand operand and returns remainder 
4. String Concatenation Operator
The '+'
operator is overloaded in Java. An operator is said to be overloaded if it is used to perform more than one function.
4.1. Concatenating Two Strings
So far, you have seen its use as an arithmetic addition operator to add two numbers. It can also be used to concatenate two strings.
String str1 = "Hello";
String str2 = " World";
String str3 = str1 + str2; // Assigns "Hello World" to str3
4.2. Concatenating Primitive Types to String
The string concatenation operator is also used to concatenate a primitive and a reference data type value to a string.
int num = 26;
String str1 = "Alphabets";
String str2 = num + str1; // Assigns "26Alphabets" to str2
4.3. Concatenate null
If a reference variable contains the ‘null‘ reference, the concatenation operator uses a string “null”.
String str1 = "I am ";
String str2 = null;
String str3 = str1 + str2; // Assigns "I am null" to str3
5. Relational Operators
 All relational operators are binary operators.
 They take two operands.
 The result produced by a relational operator is always a Boolean value
true
orfalse
.  They are mostly used in Java control statements such as if statements, while statements etc.
Let’s see below all available relational operators in java.
Operator  Description 

'=='  Equals to – Checks if the values of two operands are equal or not, if yes then condition becomes true. 
'!='  Not equals to – Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. 
'>'  Greater than – Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. 
'<'  Less than – Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. 
'>='  Greater than or equals to – Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. 
'<='  Less than or equals to – Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. 
int result = 20;
if( result > 10) { //true
//some operation
}
boolean isEqual = ( 10 == 20 ); //false
6. Boolean Logical Operators
 All Boolean logical operators can be used only with boolean operand(s).
 They are mostly used in control statements to compare two (or more) conditions.
Operator  Description 

'!'  returns true if the operand is false, and false if the operand is true. 
'&&'  returns true if both operands are true. If either operand is false, it returns false. 
'&'  returns true if both operands are true. If either operand is false, it returns false. 
''  returns true if either operand is true. If both operands are false, it returns false. 
''  returns true if either operand is true. If both operands are false, it returns false. 
'^'  it returns true if one of the operands is true, but not both. If both operands are the same, it returns false. 
'&=;'  if both operands evaluate to true, &= returns true. Otherwise, it returns false. 
'='  if either operand evaluates to true, != returns true. Otherwise, it returns false. 
'^='  if both operands evaluate to different values, that is, one of the operands is true but not both, ^= returns true. Otherwise, it returns false. 
int result = 20;
if( result > 10 && result < 30) {
//some operation
}
if( result > 10  result < 30) {
//some operation
}
 The logical AND operator (&) works the same way as the logical shortcircuit AND operator (&&), except for one difference. The logical AND operator (&) evaluates its righthand operand even if its lefthand operand evaluates to false.
 The logical OR operator works the same way as the logical shortcircuit OR operator, except for one difference. The logical OR operator evaluates its righthand operand even if its lefthand operand evaluates to true.
7. Bitwise Operators
A bitwise operator manipulates individual bits of its operands. Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Operator  Description 

'&'  Binary AND Operator copies a bit to the result if it exists in both operands. 
''  Binary OR Operator copies a bit if it exists in either operand. 
'^'  Binary XOR Operator copies the bit if it is set in one operand but not both. 
'~'  Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. 
<<  Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. 
>>  Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. 
>>>  Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. 
8. Ternary Operator
 Java has one conditional operator. It is called a ternary operator as it takes three operands.
 The two symbols of “?” and “:” make the ternary operator.
 If the booleanexpression evaluates to true, it evaluates the trueexpression; otherwise, it evaluates falseexpression.
booleanexpression ? trueexpression : falseexpression
int number1 = 40; int number2 = 20; int biggerNumber = (number1 > number2) ? number1 : number2; //Compares both numbers and return which one is bigger
9. Operators Precedence Table
Java has welldefined rules for specifying the order in which the operators in an expression are evaluated when the expression has several operators. For example, multiplication and division have higher precedence than addition and subtraction.
Precedence rules can be overridden by explicit parentheses.
When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3
is treated as 1 + (2 * 3)
because the precedence of multiplication is higher than addition.
In the above expression, if you want to add values first then use explicit parentheses like this – (1 + 2) * 3
.
Precedence  Operator  Type  Associativity 

15  () [] ·  Parentheses Array subscript Member selection  Left to Right

14  ++ —  Unary postincrement Unary postdecrement  Right to left 
13  ++ — + – ! ~ ( type )  Unary preincrement Unary predecrement Unary plus Unary minus Unary logical negation Unary bitwise complement Unary type cast  Right to left 
12  * / %  Multiplication Division Modulus  Left to right 
11  + –  Addition Subtraction  Left to right 
10  << >> >>>  Bitwise left shift Bitwise right shift with sign extension Bitwise right shift with zero extension  Left to right 
9  < <= > >= instanceof  Relational less than Relational less than or equal Relational greater than Relational greater than or equal Type comparison (objects only)  Left to right 
8  == !=  Relational is equal to Relational is not equal to  Left to right 
7  &  Bitwise AND  Left to right 
6  ^  Bitwise exclusive OR  Left to right 
5    Bitwise inclusive OR  Left to right 
4  &&  Logical AND
 Left to right 
3    Logical OR  Left to right 
2  ? :  Ternary conditional  Right to left 
1  = += = *= /= %=  Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment  Right to left 
That’s all for the operators in java.
Happy Learning !!