wellnwill Marathahalli Bangalore

Operators in Java

Operators are special symbols that perform specific operations on operands.

Types of Operators

  1. Arithmetic Operators
  2. Relational Operators
  3. Assignment Operators
  4. Logical Operators
  5. String Concatenation Operators
  6. new Operators
  7. Increment and Decrement Operators
  8. Conditional Operator

Arithmetic Operators

  • Arithmetic operators are used to perform arithmetic operations.
  • There are two types of arithmetic operator based on the number of operands -
    1. Unary Arithmetic Operator
    2. Binary Arithmetic Operator

Unary Arithmetic Operators

The unary arithmetic operators act on a single operand. They are used to change the sign of a value.

Operator Operator Name Used For
+ Unary plus The result of unary plus is same value with same sign
- Unary minus The result of unary minus is same value with opposite sign
 class Test10
 {
   public static void main(String argss[])
   {
       int x=99;
       int y=+x;
       int z=-x;
       System.out.println(x);
       System.out.println(y);
       System.out.println(z);
    }
} 

//O/P : 99, 99, -99
class Test11
{
 public static void main(String argss[])
  {
   char ch='A';
   System.out.println(ch);
   char ch2=+ch;  // compiler converts +ch as +65(ASCII of ‘A’)
   System.out.println(ch2);
  }
}
// O/P - CompileTimeError
Note- If we use char type operands with arithmetic operators 
then ASCII value will be used by compiler.
 class Test12 
 {
   public static void main(String argss[]){
    char ch='A';
    System.out.println(ch);
    int x=+'A';
   System.out.println(x);
  }
}
// O/P - A, 65
class Test13{
  public static void main(String argss[]){
   int x=100;
   byte y=x;
   System.out.println(y);
  }
}
//O/P - CompileTimeError 
Note- We can not store wider data type into smaller data. 

Binary arithmetic operators

Binary arithmetic operators are used to perform addition, subtraction, multiplication, and division.

  • Operator symbols : + (add), - (subtract), * (multiply), / (division), % (modulus).
  • If we apply any arithmetic operator between two variables the result type will be Max (int, data type of 1st variable, data type of 2nd variable). Example- Look into the below statement:
    1. byte + byte = int
    2. int + long = long
    3. char + int = int
    4. etc.
class Test15 {
 public static void main(String argss[]) {
   byte by1=10;
   byte by2=20;
   byte by3=by1+by2;
   System.out.println(by3);
  }
}
// O/P - incompatible types: possible lossy conversion from int to byte.
Note: result type must be Max (int, data type of 1st variable, data type of 2nd  variable).
class Test16 {
  public static void main(String argss[]) {
    byte by1=10;
    byte by2=20;
    int i=by1+by2;
    System.out.println(i);
  }
}


// O/P - 30
class Test17 {
 public static void main(String argss[]) {
   char ch1='a';
   char ch2='b';
   int i=ch1+ch2;
   System.out.println(ch1);
   System.out.println(ch2);
   System.out.println(i);
  }
} 
// O/P - a, b, 195
class Test18 {
  public static void main(String argss[]) {
    int a=1+1L  ; // 1L represents long data type value
    System.out.println(a);
 }
}
// O/P  - CompileTimeError 
         (incompatible types: possible lossy conversion 
          from long to int).	
          	  
class Test19
 {
  public static void main(String argss[])
   {
   long x=1+1L;
   System.out.println(x);
   }
}
// O/P - 2
class Test20 {
  public static void main(String argss[]) {
    long x=1+1.0;   //  1.0 represents double data type value
    System.out.println(x);
  }
}
// O/P - CompileTimeError 
       - incompatible types: possible lossy conversion from double to long.

  • If you divide any integer value by 0 then it will generate Arithmetic Exception at runtime.
  • The result of arithmetic operations performed with byte, short, int and long(integer value) can not be represented as infinity that is why we get arithmetic exception.
  • The result of arithmetic operations performed with float and double can be represented as infinity.
  • Float and Double class contain POSITIVE_INFINITY AND NEGATIVE_INFINITY constant for holding the infinity results.

  • 0/0 - undefined
    1/0 - infinity
    0/1 - 0
    
    class Test21 {
     public static void main(String argss[]) {
       int x=1/0;
       System.out.println(x);
      }
    }
    // O/P - CompileTimeError 
           - Exception in thread "main" java.lang.ArithmeticException:/ by zero
    
    
    class Test22 {
     public static void main(String argss[]){
      float x=1/0.0f;
      System.out.println(x);
      double d=-1/0.0;
      System.out.println(d);
     }
    }
    
    // O/P - infinity, 
    

    • Float and Double classes contain NaN constant for holding the undefined results.
    • (multiply), / (division), % (modulus) have same level for expression execution but from left to right.
    • + (add), - (subtract) have same level for expression execution but after * (multiply), / (division), % (modulus) operator.


    class Test23 {
     public static void main(String argss[]) {
       float x=0/0.0f;
       System.out.println(x);
       double d=-0.0/0.0;
       System.out.println(d);
     }
    }
    
    // O/P - NaN, NaN
    
    class Test24
     {
     public static void main(String argss[]) 
     {
       int a=20*20/10; 
       System.out.println(a);
     }
    }
    // O/P - 40
    
    
Footer with Map

Top