Java Operators
Operator is a symbol which tells to the compiler to perform some operation. Java provides a rich set of operators do deal with various types of operations. Sometimes we need to perform arithmetic operations then we use plus (+) operator for addition, multiply(*) for multiplication etc.
Operators are always essential part of any programming language.
Java operators can be divided into following categories:
- Arithmetic operators
- Relation operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
- Misc operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations like: addition, subtraction etc and helpful to solve mathematical expressions. The below table contains Arithmetic operators.
Operator |
Description |
+ |
adds two operands |
- |
subtract second operands from first |
* |
multiply two operand |
/ |
divide numerator by denumerator |
% |
remainder of division |
++ |
Increment operator increases integer value by one |
-- |
Decrement operator decreases integer value by one |
Example:
Lets create an example to understand arithmetic operators and their operations.
class Arithmetic_operators1{
public static void main(String as[])
{
int a, b, c;
a=10;
b=2;
c=a+b;
System.out.println("Addtion: "+c);
c=a-b;
System.out.println("Substraction: "+c);
c=a*b;
System.out.println("Multiplication: "+c);
c=a/b;
System.out.println("Division: "+c);
b=3;
c=a%b;
System.out.println("Remainder: "+c);
a=++a;
System.out.println("Increment Operator: "+a);
a=--a;
System.out.println("decrement Operator: "+a);
}
}
Relation operators
Relational operators are used to test comparison between operands or values. It can be use to test whether two values are equal or not equal or less than or greater than etc.
The following 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 |
Example:
In this example, we are using relational operators to test comparison like less than, greater than etc.
class Relational_operators1{
public static void main(String as[])
{
int a, b;
a=40;
b=30;
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) );
}
}
Logical operators
Logical Operators are used to check conditional expression. For example, we can use logical operators in if statement to evaluate conditional based expression. We can use them into loop as well to evaluate a condition.
Java supports following 3 logical operator. Suppose we have two variables whose values are: a=true and b=false.
Operator |
Description |
Example |
&& |
Logical AND |
(a && b) is false |
|| |
Logical OR |
(a || b) is true |
! |
Logical NOT |
(!a) is false |
Example:
In this example, we are using logical operators. There operators return either true or false value.
class Logical_operators1{
public static void main(String as[])
{
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));
}
}
Bitwise operators
Bitwise operators are used to perform operations bit by bit.
Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte.
The following table shows all bitwise operators supported by Java.
Operator |
Description |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise exclusive OR |
<< |
left shift |
>> |
right shift |
Now lets see truth table for bitwise &
, |
and ^
a |
b |
a & b |
a | b |
a ^ b
|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted. Both operands have the same precedence.
Example:
Lets create an example that shows working of bitwise operators.
a = 0001000
b = 2
a << b = 0100000
a >> b = 0000010
class Bitwise_operators1{
public static void main(String as[])
{
int a = 50;
int b = 25;
int c = 0;
c = a & b;
System.out.println("a & b = " + c );
c = a | b;
System.out.println("a | b = " + c );
c = a ^ b;
System.out.println("a ^ b = " + c );
c = ~a;
System.out.println("~a = " + c );
c = a << 2;
System.out.println("a << 2 = " + c );
c = a >> 2;
System.out.println("a >>2 = " + c );
c = a >>> 2;
System.out.println("a >>> 2 = " + c );
}
}
Assignment Operators
Assignment operators are used to assign a value to a variable. It can also be used combine with arithmetic operators to perform arithmetic operations and then assign the result to the variable. Assignment operator supported by Java are as follows:
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 |
Example:
Lets create an example to understand use of assignment operators. All assignment operators have right to left associativity.
class Assignment_operators1{
public static void main(String as[])
{
int a = 30;
int b = 10;
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 = 20;
c = 25;
c /= a ;
System.out.println("c /= a = " + c );
a = 20;
c = 25;
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 >>= 2 = " + 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 );
}
}
Misc. operator
There are few other operator supported by java language.
Conditional operator
It is also known as ternary operator because it works with three operands. It is short alternate of if-else statement. It can be used to evaluate Boolean expression and return either true or false value
epr1 ? expr2 : expr3
Example:
In ternary operator, if epr1 is true then expression evaluates after question mark (?) else evaluates after colon (:). See the below example.
class Conditional_operators1{
public static void main(String as[])
{
int a, b;
a = 20;
b = (a == 1) ? 30: 40;
System.out.println( "Value of b is : " + b );
b = (a == 20) ? 30: 40;
System.out.println( "Value of b is : " + b );
}
}
instanceOf
operator
It is a java keyword and used to test whether the given reference belongs to provided type or not. Type can be a class or interface. It returns either true or false.
Example:
Here, we created a string reference variable that stores “studytonight”. Since it stores string value so we test it using isinstance operator to check whether it belongs to string class or not. See the below example.
class instanceof_operators1{
public static void main(String as[])
{
String a = "Studytonight";
boolean b = a instanceof String;
System.out.println( b );
}
}