Basic Operators of the Language– Java provides a rich set of operators for manipulating variables. All Java operators can be divided into the following groups:
- arithmetic operators;
- comparison operators;
- bitwise operators;
- logical operators;
- assignment operators;
- other operators.

Arithmetic operators
Arithmetic Operators – Used in mathematical expressions in the same way they are used in algebra. Suppose integer variable A is 10 and variable B is 20. The following table lists the arithmetic operators in Java:
Operator | Description | Example |
+ | Adds values on either side of the operator | A + B will give 30 |
– | Subtracts the right-hand operand from the left-hand operand | A – B will give -10 |
* | Multiplies values on either side of the operator | A * B will give 200 |
/ | The division operator divides the left operand by the right operand | B / A will give 2 |
% | Divides the left operand by the right operand and returns the remainder | B% A will give 0 |
++ | Increment – increases the value of the operand by 1 | B ++ will give 21 |
– | Decrement – decreases the value of the operand by 1 | B– will give 19 |
Example
The following simple example shows programmatically arithmetic operators. Copy and paste the following java code into the test.java file, compile and run this program:
public class Test {
public static void main (String args []) {
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println ("a + b =" + (a + b));
System.out.println ("a - b =" + (a - b));
System.out.println ("a * b =" + (a * b));
System.out.println ("b / a =" + (b / a));
System.out.println ("b% a =" + (b% a));
System.out.println ("c% a =" + (c% a));
System.out.println ("a ++ =" + (a ++));
System.out.println ("b-- =" + (a--));
// Check the difference in d ++ and ++ d
System.out.println ("d ++ =" + (d ++));
System.out.println ("++ d =" + (++ d));
}
}
This will produce the following output:
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++ = 10
b-- = 11
d++ = 25
++d = 27
Comparison Operators
The following comparison operators are supported in the Java language. Suppose variable A is 10 and variable B is 20. The following table lists the relational or comparison operators in Java:
Operator | Description | Example |
== | Checks if the values of two operands are equal or not, if yes, then the condition becomes true | (A == B) – not correct |
! = | Checks if the values of two operands are equal or not, if the values are not equal, then the condition becomes true | (A! = B) – value is true |
> | Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true | (A> B) – not correct |
< | Checks if the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true | (A <B) – value is true |
> = | Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true | (A> = B) – values are not correct |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true | (A <= B) – value is true |
Example
The following simple example shows programmatically the comparison operators in Java. Copy and paste the following java code into the test.java file, compile and run this program:
public class Test {
public static void main(String args[]) {
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}
You will get the following output:
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false
Bitwise operators
Java defines several bitwise operators that can be applied to integer types: int, long, short, char, and byte. In Java, the bitwise operator works on bits and performs the operation bit by bit. Suppose if a = 60; and b = 13; then in binary they will be as follows:
a = 0011 1100
b = 0000 1101
—————–
a & b = 0000 1100
a | b = 0011 1101
a ^ b = 0011 0001
~ a = 1100 0011
Suppose the integer variable A is 60 and the variable B is 13. The following table lists the bitwise operators in Java:
Operator | Description | Example |
& (bitwise and) | The binary AND operator copies a bit into the result if it exists in both operands. | (A & B) will give 12, which is 0000 1100 |
| (bitwise or) | The binary OR operator copies a bit if it exists in any of the operands. | (A | B) will give 61 which is 0011 1101 |
^ (bitwise boolean or) | The binary XOR operator copies a bit if it is set in one operand, but not in both. | (A ^ B) will give 49 which is 0011 0001 |
~ (bitwise complement) | Binary’s complement operator and has the effect of “flipping” bits. | (~ A) will give -61, which is the complement form of 1100 0011 in binary notation |
<< (shift left) | Binary left shift operator. The value of the left operands is moved left by the number of bits specified by the right operand. | A << 2 will give 240 which is 1111 0000 |
>> (shift right) | Binary right shift operator. The value of the right operands is moved right by the number of bits specified by the left operand. | A >> 2 will give 15 which is 1111 |
>>> (zero shift right) | Zero right shift operator. The value of the left operands is moved right by the number of bits specified by the right operand, and the shifted values are filled with zeros. | A >>> 2 will give 15 which is 0000 1111 |
Example
The following simple example shows programmatically bitwise operators in Java. Copy and paste the following java code into the test.java file, compile and run this program:
public class Test {
public static void main(String args[]) {
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
System.out.println("a & b = " + c );
c = a | b; /* 61 = 0011 1101 */
System.out.println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
c = ~a; /*-61 = 1100 0011 */
System.out.println("~a = " + c );
c = a << 2; /* 240 = 1111 0000 */
System.out.println("a << 2 = " + c );
c = a >> 2; /* 215 = 1111 */
System.out.println("a >> 2 = " + c );
c = a >>> 2; /* 215 = 0000 1111 */
System.out.println("a >>> 2 = " + c );
}
}
You will get the following output:
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 15
a >>> 15
Logical operators
Suppose boolean variable A is true and variable B holds false. The following table lists the logical operators in Java:
Operator | Description | Example |
&& | The logical operator “AND” is called. If both operands are non-zero, then the condition becomes true | (A && B) – false |
|| | The logical operator “OR” is called. If either of the two operands is non-zero, then the condition becomes true | (A || B) – true |
! | The logical operator “NOT” is called. Usage changes the logical state of its operand. If the condition is true, then the logical “NOT” operator will do false | ! (A && B) – true |
Example
The following simple example shows programmatically logical operators in Java. Copy and paste the following java code into the test.java file, compile and run this program:
public class Test {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
}
}
This will produce the following output:
a && b = false
a || b = true
!(a && b) = true
Assignment operators
The following assignment operators are supported by the Java language:
Operator | Description | Example |
= | Simple assignment operator, assigns values from the right side of the operands to the left side | C = A + B, will assign the value of A + B to C |
+ = | The “Append” assignment operator, it assigns the left operand the values of the right | C + = A, equivalent to C = C + A |
– = | The assignment operator “Subtraction”, it subtracts the left operand from the right operand | C – = A, equivalent to C = C – A |
* = | The assignment operator “Multiplication”, it multiplies the right operand by the left operand | C * = A is equivalent to C = C * A |
/ = | The assignment operator “Division”, it divides the left operand by the right operand | C / = A is equivalent to C = C / A |
% = | The assignment operator “Module”, it takes a module using two operands and assigns its result to the left operand | C% = A, equivalent to C = C% A |
<< = | Left shift assignment operator | C << = 2, it’s like C = C << 2 |
>> = | Shift right assignment operator | C >> = 2, it’s like C = C >> 2 |
& = | Bitwise AND assignment operator | C & = 2, it’s like C = C & 2 |
^ = | Bitwise exclusive “OR” assignment operator (“XOR”) | C ^ = 2, it’s like C = C ^ 2 |
| = | Bitwise OR (OR) assignment operator | C | = 2, it’s like C = C | 2 |
Example
The following simple example shows programmatically logical operators in Java. Copy and paste the following java code into the test.java file, compile and run this program:
public class Test {
public static void main(String args[]) {
int a = 10;
int b = 20;
int c = 0;
c = a + b;
System.out.println("c = a + b = " + c );
c += a ;
System.out.println("c += a = " + c );
c -= a ;
System.out.println("c -= a = " + c );
c *= a ;
System.out.println("c *= a = " + c );
a = 10;
c = 15;
c /= a ;
System.out.println("c /= a = " + c );
a = 10;
c = 15;
c %= a ;
System.out.println("c %= a = " + c );
c <<= 2 ;
System.out.println("c <<= 2 = " + c );
c >>= 2 ;
System.out.println("c >>= 2 = " + c );
c >>= 2 ;
System.out.println("c >>= a = " + c );
c &= a ;
System.out.println("c &= 2 = " + c );
c ^= a ;
System.out.println("c ^= a = " + c );
c |= a ;
System.out.println("c |= a = " + c );
}
}
You will get the following output:
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a = 0
c ^= a = 10
c |= a = 10
Other operators
There are several other operators supported by the Java language.
Ternary operator or conditional operator (? 🙂
Ternary operator is an operator that consists of three operands and is used to evaluate expressions of type boolean. The ternary operator in Java is also known as the conditional operator. This. The purpose of a ternary operator or conditional operator is to decide what value should be assigned to a variable. The operator is written as:
variable x = (expression)? if true: if false
Example
Below is an example:
public class Test {
public static void main (String args []) {
int a, b;
a = 10;
b = (a == 1)? 20:30;
System.out.println ("Value b:" + b);
b = (a == 10)? 20:30;
System.out.println ("Value b:" + b);
}
}
You will get the following output:
public class Test {
public static void main (String args []) {
int a, b;
a = 10;
b = (a == 1)? 20:30;
System.out.println ("Value b:" + b);
b = (a == 10)? 20:30;
System.out.println ("Value b:" + b);
}
}
You will get the following output:
B value: 30
B value: 20
Instanceof operator
Operator instanceof – Checks if an object is of a specific type (class type or interface type) and is used only for variables of the referenced object. The instanceof operator is written as:
(Reference object variable) instanceof (interface class / type)
Examples of
If the referenced object variable on the left side of the statement passes the test for the class / interface type on the right side, the result is true. Below is an example and description of the instanceof operator:
public class Test {
public static void main (String args []) {
String name = "Oleg";
// The following will return true because the type is String
boolean result = name instanceof String;
System.out.println (result);
}
}
You will get the following output:
true
This operator will still return true if the object being compared is type compatible for the assignment right. Another example follows:
class Vehicle {}
public class Car extends Vehicle {
public static void main(String args[]){
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println( result );
}
}
You will get the following output:
true
Operator precedence in Java
Operator precedence determines the grouping of terms in an expression. This affects how the expression is evaluated. Some operators have higher priority than others; for example the multiplication operator has higher precedence than the addition operator:
For example, x = 7 + 3 * 2. Here x is assigned the value 13, not 20, because the “*” operator has a higher precedence than “+”, so first “3 * 2” is multiplied and then “7” is added “.
In the table, the operators with the highest priority are placed at the top and the priority level is lowered towards the bottom of the table. In an expression, high precedence operators in Java will be evaluated from left to right.
Category | Operator | Associativity |
Postfix | () []. (dot) | From left to right |
Unary | ++ – -! ~ | From right to left |
Multiplicative | * /% | From left to right |
Additive | + – | From left to right |
Shift | >> >>> << | From left to right |
Relational | >> = <<= | From left to right |
Equality | ==! = | From left to right |
Bitwise “AND” (“AND”) | & | From left to right |
Bitwise exclusive “OR” (“XOR”) | ^ | From left to right |
Bitwise “OR” (“OR”) | | | From left to right |
Logical “AND” | && | From left to right |
Logical “OR” (“OR”) | || | From left to right |
Conditional | ?: | From right to left |
Assignment | = + = – = * = / =% = >> = << = & = ^ = | = | From right to left |
Comma | , | From left to right |