Java provides a rich set of operators to manipulate variables. We can divide all Java operators into the following categories −
Arithmetic operators are used in mathematical expressions in the same way as in algebra. The following table lists the usage examples of arithmetic operators −
Suppose the value of the integer type variable A
is: 10
, and the value of the variable B
is: 20
, then -
operator | describe | example |
---|---|---|
+ |
The addition operator adds the first operand to the second operand | A + B The result is:30 |
- |
Subtraction operator, subtracts the second operand from the first operand | A - B The result is:-10 |
* |
Multiplies two operands | A * B The result is:200 |
/ |
Divide the left operand by the right operand and return the modulo value | B / A The result is:2 |
% |
Divide the left operand by the right operand and return the remainder | B / A The result is:0 |
++ |
increment the value of the operand1 |
A++ , then A the value is:11 |
-- |
Subtracts the value of the operand1 |
A-- , then A the value is:9 |
Java Arithmetic Example
System.out.println("a + b = " + (a + b) );
|
The Java language supports the following relational operators. Assuming the value of the variable A is 10 , the value of the variable B
is 20
then -
operator | describe | example |
---|---|---|
== |
The equal operator, checks if the values of the two operands are equal, if they are equal then the condition becomes true. | A==B The result is false. |
!= |
The not equal operator, checks if the values of the two operands are not equal, if not then the condition becomes true. | A!=B The result is true. |
> |
The greater than operator, checks if the value of the left operand is greater than the value of the right operand, if greater then the condition becomes true. | A>B The result is false. |
< |
The less than operator, checks if the value of the left operand is less than the value of the right operand, if less then the condition becomes true. | A<B The result is true. |
>= |
Greater than or equal to operator, checks if the value of the left operand is greater than or equal to the value of the right operand, if greater than or equal to, then the condition becomes true. | A>=B The result is false. |
<= |
Less than or equal to operator, checks if the value of the left operand is less than or equal to the value of the right operand, if less than or equal then the condition becomes true. | A<=B The result is true. |
Relational Operator Sample examples
Java defines several bitwise operators that can be applied to integer types such as: long
, int
, short
, char
and byte
. Bitwise operators manipulate bits and perform bitwise operations. Assuming a = 60
and b = 13
; are in binary format, 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
The bitwise operators are listed in the table below, assuming an integer variable A=60
, variable B=13
, then −
operator | describe | example |
---|---|---|
& |
The binary AND operator, if present in both operands, it copies the result into the result. | A & B The result is: 12 , that is:0000 1100 |
Ι | The binary OR operator, copies a bit if present in either operand. | The result of A Ι B is: 61 , that is:0011 1101 |
^ |
Binary XOR operator, copies the bit if it is set in one operand but not in both operands. | A ^ B The result is: 49 , that is:0011 0001 |
~ |
The binary unary complement operator is unary and has the effect of "flipping" bits. | ~A The result is: -61 , that is:1100 0011 |
<< |
Binary left shift operator, the value of the left operand is shifted to the left by the number of bits specified by the right operand. | A << 2 The result is: 240 , that is:1111 0000 |
>> |
Binary right shift operator, the value of the left operand is shifted to the right by the number of bits specified by the right operand. | A >> 2 The result is: 15 , that is:1111 |
>>> |
Right shift zero padding operator. The left operand value is shifted right by the number of bits specified by the right operand, and the shifted value is filled with zeros. | A >>>2 The result is: 15 , that is:0000 1111 |
Bitwise operator Example
public class Test { public static void main(String args[]) { c = a & b; /* 12 = 0000 1100 */ c = a | b; /* 61 = 0011 1101 */ c = a ^ b; /* 49 = 0011 0001 */ c = ~a; /*-61 = 1100 0011 */ c = a << 2; /* 240 = 1111 0000 */ c = a >> 2; /* 15 = 1111 */ c = a >>> 2; /* 15 = 0000 1111 */
|
The following table lists the logical operators −
Assuming the value of the Boolean variable ,the value of the A
is: true and variable B
is: false
, then -
operator | describe | example |
---|---|---|
&& |
Logical AND operator. The condition is true if both operands are non-zero. | (A && B) The result is:false |
ΙΙ | Logical OR operator. If either of the two operands is non-zero, the condition becomes true. | (AΙΙB) The result is:true |
! |
Logical NOT operator. Used to invert the logical state of its operand. The logical NOT operator will be if the condition is true false . |
!(A && B) The result is:true |
Logical operator sample examples
Following are the assignment operators supported by Java language −
operator | describe | example |
---|---|---|
= |
Simple assignment operator. Assigns the value of the right operand to the left operand. | C = A + B Assign A + B the value of C . |
+= |
Addition and assignment operators. It adds the right operand to the left operand and assigns the result to the left operand. | C += A Equal to C = C + A . |
-= |
Subtraction and assignment operators. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A Equal to C = C - A . |
*= |
Multiply and assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A Equal to C = C * A . |
/= |
Divide and assignment operator. It divides the left operand by the right operand and assigns the result to the left operand. | C /= A Equal to C = C / A . |
%= |
Modulo and assignment operators. It uses two operands to calculate the modulus and assign the result to the left operand. | C %= A Equal to C = C % A . |
<<= |
Left shift AND assignment operator. | C <<= 2 same C = C << 2 as |
>>= |
Right shift AND assignment operator. | C >>= 2 same C = C >> 2 as |
&= |
Bitwise AND assignment operator. | C &= 2 same C = C & 2 as |
^= |
Bitwise XOR and assignment operators. | C ^= 2 same C = C ^ 2 as |
Ι= | Bitwise inclusive or AND assignment operator. | C Ι = 2 is the same as C = C Ι = 2 |
Assignment operator example code
public class Test { public static void main(String args[]) { c = a + b; c += a ; c -= a ; c *= a ; a = 10; a = 10; c <<= 2 ; c >>= 2 ; c >>= 2 ; c &= a ; c ^= a ; c |= a ;
|
There are few other operators supported by the Java language.
6.1. The conditional operator (?:)
The conditional operator is also known as the ternary operator. This operator consists of three operands and evaluates a Boolean expression. The goal of the operator is to determine which value should be assigned to the variable. The operator is written as -
variable x = (expression) ? value if true : value if false
Here is a sample code:
public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
Execute the above sample code and get the following result −
Value of b is : 30
Value of b is : 20
6.2. The instanceof operator
This operator is only used for object reference variables. operator checks whether an object is of a specific type (class type or interface type). instanceof
The operator is written as -
( Object reference variable ) instanceof (class/interface type)
Evaluates true if the variable on the left side of the operator refers to an object of the class/interface type on the right side. Following is an example −
public class Test {
public static void main(String args[]) {
String name = "Kobe";
boolean result = name instanceof String;
System.out.println( result );
}
}
Execute the above sample code and get the following results:
true
This operator will still return if the objects being compared are compatible with the type on the right true
. Following is another example -
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 );
}
}
Executing the sample code above yields the following results:
true