Java Operators and Expression - Types of Operators

Java provides a rich set of operators to manipulate variables. We can divide all Java operators into the following categories −

  • arithmetic operator
  • relational operator
  • bitwise operator
  • Logical Operators
  • assignment operator
  • other operators

1. Arithmetic operators

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 is: 10, and the value of the variable is: 20, then -

operator describe example
+ The addition operator adds the first operand to the second operand A + BThe result is:30
- Subtraction operator, subtracts the second operand from the first operand A - BThe result is:-10
* Multiplies two operands A * BThe result is:200
/ Divide the left operand by the right operand and return the modulo value B / AThe result is:2
% Divide the left operand by the right operand and return the remainder B / AThe result is:0
++ increment the value of the operand1 A++, then Athe value is:11
-- Subtracts the value of the operand1 A--, then Athe value is:9

 

 

Java Arithmetic Example


   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--) );


      System.out.println("d++   = " +  (d++) );
      System.out.println("++d   = " +  (++d) );
   }
}


 

 

2. Relational operators

The Java language supports the following relational operators. Assuming the value of the variable A is 10 , the value of the variable is 20then -

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==BThe 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!=BThe 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>BThe 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<BThe 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>=BThe 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<=BThe result is true.

Relational Operator Sample examples

 

3. Bitwise operators

Java defines several bitwise operators that can be applied to integer types such as: long, int, short, charand 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 & BThe 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 ^ BThe result is: 49, that is:0011 0001
~ The binary unary complement operator is unary and has the effect of "flipping" bits. ~AThe 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 << 2The 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 >> 2The 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 >>>2The result is: 15, that is:0000 1111

 

Bitwise operator  Example

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;       /* 15 = 1111 */
      System.out.println("a >> 2  = " + c );

      c = a >>> 2;      /* 15 = 0000 1111 */
      System.out.println("a >>> 2 = " + c );
   }
}


 

 

4. Logical operators

The following table lists the logical operators −

Assuming the value of the Boolean variable ,the value of the is: true and variable 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

 

 

5. Assignment operator

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 << 2as
>>= Right shift AND assignment operator. C >>= 2 same C = C >> 2as
&= Bitwise AND assignment operator. C &= 2 same C = C & 2as
^= Bitwise XOR and assignment operators. C ^= 2 same C = C ^ 2as
Ι= 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[]) {
      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 >>= 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 );
   }
}


 

 

6. Other operators

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
Java

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)
Java

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