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

  1. Arithmetic Operators
  2. Relational Operators
  3. Bitwise Operators
  4. Logical Operators
  5. Assignment Operators
  6. Misc Operators

The Arithmetic Operators:

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:

Example :

 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--));
   System.out.println("d++ = "+(d++));
   System.out.println("++d = "+(++d));
}
}

The Relational Operators:

Example:

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

The Bitwise Operators:

Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performsbit-by-bit operation. Assume if a = 60; and b = 13; now 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
Example:
The following simple example program demonstrates the bitwise operators. 
Copy and paste the following Java program in Test.java file and 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 );
System.out.println("~a = "+ c );
System.out.println("a << 2 = "+ c );
System.out.println("a >> 2 = "+ c );
System.out.println("a >>> 2 = "+ c );
}
}

The Logical Operators:

Example:
The following simple example program demonstrates the logical operators. 
Copy and paste the following Java program in Test.java file and 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));
}
}

The Assignment Operators:


Example:
The following simple example program demonstrates the assignment operators. 
Copy and paste the following Java program in Test.java file and 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 );
System.out.println("c <<= 2 = "+ c );
System.out.println("c >>= 2 = "+ c );
System.out.println("c >>= a = "+ c );
System.out.println("c &= 2 = "+ c );
System.out.println("c ^= a = "+ c );               
System.out.println("c |= a = "+ c );
}
}

News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.