Operaters - Java Example


Explaining java operators with Precedence and some small examples.
Datatypes defines how to store variables value in stack memory.

Java has these operators .....

1. Arithmetic Operators :-

Arithmetic Operators are used to mathmetical calculations.

 

Operator Description
+ Add Values
Subtract Values
* Multiply Values
/ Divide Values
% Take Modulus of Values
package com.java_example.tutorial;

public class Operators {
	public static void main(String[] args) {
		
        int i=1; 
        int j=2; 
        int k=0;
        
          // Addition +
          k = i + j;
          System.out.println("Addition = "+k);
          
         // Subtraction -
          k = i - j;
          System.out.println("Subtraction = "+k);
          
          // Multiplication *
          k = i * j;
          System.out.println("Multiplication = "+k);
          
         // Division /
          k = i / j;
          System.out.println("Division = "+k);
          
          // Modulus %
          k = i % j;
          System.out.println("Modulus = "+k);
                   
    }
 }


OUTPUT :

    Addition = 3
    Subtraction = -1
    Multiplication = 2
    Division = 0
    Modulus = 1
    Incriment = 3
    Decrement = -1

 

2. Relational Operators :

Arithmetic Operators are used to Compare two values.

Operator Description
== If Both expression/values are equal it will return true otherwise return False
!= If Both expression/values are not equal it will return true otherwise return False
> If First expression/values is Greater than Second it will return true otherwise return False
< If First expression/values is Less than Second it will return true otherwise return Fals
>= If First expression/values is Greater than or equal to Second it will return true otherwise return False
<= If First expression/values is Less than or equal to Second it will return true otherwise return False
package com.java_example.tutorial;

public class Relational_Operators {
	public static void main(String[] args) {
		
        int i=1; 
        int j=2; 
       
          // ==
          System.out.println("== = "+( i == j ));
          
          // != 
          System.out.println("!= = "+( i != j ));
          
          // >
          System.out.println("> = "+( i > j ));
          
          // < 
          System.out.println("< = "+( i < j ));
          
          // >=
          System.out.println(">= = "+( i >= j ));
          
          // <=
          System.out.println("<= = "+( i <= j ));
          
         
    }
 }

OUTPUT :

    == = false
    != = true
    > = false
    < = true
    >= = false
    <= = true

 

Logical Operators :

Used to compare two values.

Operator Description
&& And operator will show common part of two expressions.
Ex. if both expressions values is True, then result will True. If either expression value is False, result is False.
|| Or operator will show uncommon part of twoexpressions.
Ex. if any one or both expressions value is True, result is True.
! Not operator will show negation of value. Ex. if expressions value is True, result is False and if expressions value is False, result is True.
   
package com.java_example.tutorial;

public class Logical_Operators {
	public static void main(String[] args) {
		
        int i=1; 
        int j=2; 
        boolean result;
        
        
       // &&
        
          result = ( i < j ) && ( j > i );
          System.out.println("&& = "+ result);
        
          result = ( i > j ) && ( j > i );
          System.out.println("&& = "+ result);
          
      // || 
          
          result = ( i > j ) || ( i > j );
          System.out.println("|| = "+ result);
        
          result = ( i > j ) || ( i < j );
          System.out.println("|| = "+ result);
          
      // !
          System.out.println("! = "+( i != j ));
                    
         
    }
 }

OUTPUT :

    && = true
    && = false
    || = false
    || = true
    ! = true

 

Bitwise Operators :

Used to handle binary values.

Operator Description
>> Binary Right Shift Operator
<< Binary Left Shift Operator
~ Binary Ones Complement Operator
>>> Shift right zero fill operator
& Binary AND Operator
| Binary OR Operator
^ Binary XOR Operator

Assignment Operators :

Operator Description
= Assign Second Variable Value to First.
+= Increments Second Variable and Then Assign Value to First Variable.
-= Decrements Second Variable and Then Assign Value to First Variable.
*= Multiplies First Variable Value With Second Variable and Then Assign Value to First Variable.
/= Divides First Variable Value With Second Variable and Then Assign Value to First Variable.
%= Modulus First Variable Value With Second Variable and Then Assign Value to First Variable.
<<= Left shift First Variable Value With Second Variable and Then Assign Value to First Variable.
>>= Right shift First Variable Value With Second Variable and Then Assign Value to First Variable.
&= Bitwise First Variable Value With Second Variable and Then Assign Value to First Variable.
   
package com.java_example.tutorial;

public class Assignment_Operators {
	public static void main(String[] args) {
		
        int i=1; 
        int j=2; 
        
          
         // Incriment ++
          i += j;
         // i = i + j;  Same output as up line
          System.out.println("Incriment = "+i);
          
          i=1;
          
         // Decrement ++
          i -= j;
         // i = i - j;  Same output as up line
          System.out.println("Decrement = "+i);
          
          // Like these operators we can use %=,*=,/=,!= ...etc
    }
 }

OUTPUT :

    Incriment = 3
    Decrement = -1

 

Conditional or Ternary Operator :

result = value > conditionValue ? result1 : result2

package com.java_example.tutorial;

public class Ternary_Operators {
	public static void main(String[] args) {
		
        int i=1; 
        int j=2; 
        String result;
          
          // When condition (i < j) is true
          result = (i < j) ? "Yes" : "No";
          System.out.println("(i < j) = "+result);
          
         // When condition (i > j) is false
          result = (i > j) ? "Yes" : "No";
          System.out.println("(i > j) = "+result);
        
    }
 }


OUTPUT :

      (i < j) = Yes
      (i > j) = No

 

Operator Precedence :

result = value > conditionValue ? result1 : result2

Operator Precedence Associativity Operator Precedence
(), [], postfix ++, postfix -- left Highest
unary +, unary -, prefix ++, prefix --, ~, ! right  
(type), new left  
*, /, % left  
+, - left  
<<, >>, >>> left  
< ,<= , >, >=, instanceof    
==, !=    
& left  
^ left  
| left  
&& left  
|| left  
?: left  
=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^= right lowest

 

 

Share this: