Font Awesome Icons

Tutorial

Operators in Java

Operators are the symbols which can manipulate the values of the operands.

For Example, the expression 7 + 2 = 9, here 7 and 2 are operands and + is called operator.

Java supports following types of operators:

1)Arithmetic Operators in Java

Arithmetic Operators are used to perform mathematical operations like addition, subtraction,multiplication,division,modulus etc.

Example Of Arithmetic Operators in Java

class ArithmeticOperator {

      public static void main(String[] args) {

            int a = 5;

            int b = 2;

            System.out.println(a + b); //prints 7

            System.out.println(a – b); //prints 3

            System.out.println(a * b); //prints 10

            System.out.println(a / b); //prints 2

            System.out.println(a % b); //prints 1

          }

 

}

2)Assignment Operators in Java

An Assignment Operator is an operator used to assign a new value to a variable. The assignment operator assigns the value on its right to the variable on its left.

Operator

Example

Actual operation

+=

x += 10

x = x + 10

-=

x -= 10

x = x – 10

*=

x *= 10

x = x * 10

/=

x /= 10

x = x / 10

%=

x %= 10

x = x / 10

<<=

x <<= 10

x = x << 10

>>=

x >>= 10

x = x >> 10

&=

x &= 10

x = x & 10

^=

x ^= 10

x = x ^ 10

|=

x |= 10

x = x | 10

 

 class AssignmentOperator{
public static void main(String[] args) {
int a = 5;
int b=2;
int c;
System.out.println(c = a); // print 5
System.out.println(b += a);// prints 7
System.out.println(b -= a);// prints 2
System.out.println(b *= a);// prints 10
System.out.println(b /= a);// prints 2
System.out.println(b %= a);// prints 2
System.out.println(b ^= a);// prints 7
}

}

3)Relational Operators in Java

 The operators used to make comparisons between two operands are called relational operators.Relational operators are binary operators and hence require two operands. The result of a relational operation is a Boolean value that can only be true or false according to the result of the comparison.

Operator

Operations

Example

<=

>=

==

!=

Less than

Greater than

Less than or equal to

Greater than equal to

Equal to

Not equal to

x<y

x>y

x<=y

x>=y

x=y

x!=y

class RelationalOperators
{
       public static void main (String[] args)
    {
          int x=5 ,y=3;
          System.out.println( x>y); //prints true
          System.out.println( x<y); //prints​ false
          System.out.println(x<=y); //prints​ false​
          System.out.println(x>=y); //prints true
          System.out.println(x==y); //prints false
          System.out.println(x!=y); //prints true

     }
}

4)Logical Operators in Java

Logical operators are used to determine the logic between variables or values:

 class LogicalOperator
{
public static void main(String[] args) {
int a = 7;
System.out.println(a<10 & a<20); //prints true
System.out.println(a<10 || a<20); //prints true
System.out.println(!(a<10 & a<20)); //prints false
}
}

 

5)Unary Operators in Java

Unary operators are the one that needs a single operand and are used to increment a value, decrement or negate a value.

Operator

Description

++

increments the value by 1. There is post-increment and pre-increment operators

decrements the value by 1. There is post decrement and pre decrement operators

!

invert a boolean value

 

 

 class UnaryOperator
{
public static void main(String[] args) {
int a = 5;
boolean b=true;
System.out.println(a++); //returns 5
System.out.println(++a); //returns 7
System.out.println(a–); //returns 7
System.out.println(–a); //returns 5
System.out.println(!b); // returns false
}
}

 

6)Bitwise Operators in Java

The Bitwise operators in Java programming are used to perform bit operations. If an operand is shorter than an int, it is promoted to int before doing the operations.Bitwise  operators are used on integral types (byteshortint and long) to perform bit-level operations.These operators are not commonly used. The following types of bitwise operators are available in Java.

 

(a)Bitwise OR in Java

Bitwise OR is a binary operator (operates on two operands). It’s denoted by |.
The | operator compares corresponding bits of two operands. If either of the bits is 1, it gives 1. If not, it gives 0. 

 class BitwiseOR
{
public static void main(String[] args) {

int a = 5, b = 7, result;

result = a | b;
System.out.println(result);//prints 7
}
}

(b)Bitwise AND in Java

Bitwise AND is a binary operator (operates on two operands). It’s denoted by &.
The & operator compares corresponding bits of two operands. If both bits are 1, it gives 1. If either of the bits is not 1, it gives 0. 

class BitwiseAnd
{
public static void main(String[] args) {

int a = 5, b = 7, result;

result = a & b;
System.out.println(result);//prints 5
}
}

(c)Bitwise Complement in Java

Bitwise complement is an unary operator . It is denoted by ~.
The ~ operator inverts the bit pattern. It makes every 0 to 1, and every 1 to 0.

 class Complement {
public static void main(String[] args) {

int a = 5, result;

result = ~a;
System.out.println(result);//prints -6
}
}

(7)Ternary Operators in Java

The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditional statements. This method is an alternative for using if-else and nested if-else statements. The order of execution for this operator is from left to right.

Syntax:

1
(Condition) ? (Statement1) : (Statement2);

 

class Ternary {
public static void main(String[] args) {
int age = 22;
String result = (age > 18) ? “eligible to vote!” : “Not eligible to vote”;
System.out.println(result);
}
}

(8)Shift Operators in Java

Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number. There are three different types of shift operators, namely left shift operator()<<, signed right operator(>>) and unsigned right shift operator(>>>).Following are the shift operators available in Java.

(a)Right Shift Operator(>>) in Java

In Java, the operator ‘>>’ is signed right shift operator. All integers are signed in Java. The operator ‘>>’ uses the sign bit (left most bit) to fill the trailing positions after shift. If the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler.

class RightShift {
public static void main(String args[]) {
System.out.println(4>>2);//4/2^2=1
System.out.println(3>>4); //3/2^4=0
}
}

(b)Left Shift Operator(<<) in Java

In Java, the operator ‘<<,’shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.

class LeftShift {
public static void main(String args[]) {
System.out.println(4<<2);//4*2^2=16
System.out.println(3<<4); //3*2^4=48
}
}

 

(c)Unsigned Right Shift Operator(>>>) in Java

The unsigned right shift operator >>> shifts zero into the leftmost position.When we apply >>> on a positive number, it gives the same output as that of >>. It gives a positive number when we apply >>> on a negative number. MSB is replaced by a 0.

class UnRightShift {
public static void main(String args[]) {
System.out.println(4>>>2);//1
System.out.println(3>>>4); //0
}
}