Fallowing icon

Monday, September 24, 2012

Java Operators



The content

      * The Assignment Operators
      * The Arithmetic  Operators 
      * The Bitwise  Operators
      * The Logical  Operators 
      * The Relational Operators
      * The Operator Precedence 


      Operations are act very impotent character in Java. We can use those operations for primitive types variables with. But assignment operate only can use for both data types.

We can divide all the Java operators into the following groups


  • Assignment Operators
  • Arithmetic  Operators
  • Bitwise  Operators
  • Logical  Operators
  • Relational  Operators



The Assignment Operators

Assignment operator is a most common operator in all programming languages. It is represent by "=" symbol in java. It use for assign a value in variables to a variables. The assignment operator have left side and right side (left side=right side), It process on left side value transfer to the right side variable.


int age=23;
double salary=500.12d;
int temperature =23;



The Arithmetic  Operators  


The arithmetic operator that perform addition,subtraction multiplication,division,modulus,increment and decrement.

The table shows all compound arithmetic operators which you can use to make your code more readable and efficient.



Operator
Meaning
Example
+
Addition
1+2
-
Subtraction
9-5
*
Multiplication
12*4
/
Division
18/2
%
Modulus
8%3
++
Increment
x++,++x
--
Decrement
y--,--y
  

Example -



public class Test { 
    
    public static void main(String args[]){
        
        int age=23;                   //declare the age variable and assign value 23        
        System.out.println("Your age is    :"+age);
        
        age=age+5;                   //Now your age is 27                
        System.out.println("After addtion, Your age is    :"+age);  
        
        age=age-3;                   //Now your age is 24
        System.out.println("After subtraction, Your age is    :"+age);
        
        age=age*2;                   //Now your age is 48
        System.out.println("After multipication, Your age is    :"+age);
        
        age=age/3;                   //Now your age is 16
        System.out.println("After divition, Your age is    :"+age);
        
        age=age%3;                   //Now your age is 1
        System.out.println("After modulus, Your age is    :"+age);
        
        age++;                       //Now your age is 2
        System.out.println("After increment, Your age is    :"+age);
        
        age--;                       //Now your age is 1
        System.out.println("After decrement, Your age is    :"+age);
    }
    
}




* Download this java code, then compile it and run.


Increment and decrement are use two types in java. Some times use already in variables(eg:- ++x,--x) and some times use after variables(eg:- x++,x--). This two types have some different points. 
++x is increments the value of x and then returns x and
x++ is returns the value of x and then increments. We use this point for some times when we are coding.


The shortcut of arithmetic operate given below. 


x+=y; is the same as x=x+y;
x-=y; is the same as x=x-y;
x*=y; is the same as x=x*y;
x/=y; is the same as x=x/y;
x%=y; is the same as x=x%y;

Try to use shortcut command to coding.


Example to the Above code using shortcut commands given below. 



public class Test { 
    
    public static void main(String args[]){
        
        int age=23;                   //declare the age variable and assign value 23        
        System.out.println("Your age is    :"+age);
        
        age+=4;                  //Now your age is 27                
        System.out.println("After addtion, Your age is    :"+age);  
        
        age-=3;                  //Now your age is 24
        System.out.println("After subtraction, Your age is    :"+age);
        
        age*=2;                   //Now your age is 48
        System.out.println("After multipication, Your age is    :"+age);
        
        age/=3;                   //Now your age is 16
        System.out.println("After divition, Your age is    :"+age);
        
        age%=3;                   //Now your age is 1
        System.out.println("After modulus, Your age is    :"+age);
        
        age++;                       //Now your age is 2
        System.out.println("After increment, Your age is    :"+age);
        
        age--;                       //Now your age is 1
        System.out.println("After decrement, Your age is    :"+age);
    }    

* Download this java code, then compile it and run.



The Bitwise  Operators   


In Java bitwise operators are use to content of manipulate 
the contents of variables at a bit level. This format is process according to binary format. 


Example for the basic binary format-
We can implement positive and negative numbers using binary format. 8bit two's compliment format is used for this example implantation.

The decimal number  0 is represented as 00000000 in binary.
The decimal number  5 is represented as 00000101 in binary. 
The decimal number -5 is represented as 11111010 in binary.
The decimal number -0 is represented as 00000000 in binary.



The table shows all compound bitwise operators.

Operator
Meaning
Example
Result
&
Bitwise And
3&5
1
|
Bitwise OR
3|5
7
^
Bitwise XOR
3^5
6
>> 
Right Shift
5>>2
1
<< 
Left Shift
3<<2
12
>>> 
Zero Fill Right Shift
10>>>2
2
~
Bitwise Compliment
~3
-4

 



Example-



public class Test { 
    
    public static void main(String args[]){
        
       
       System.out.println("3 AND 5 is    :"+(3&5));    //AND operator, Output 1   
       
       System.out.println("3 OR 5 is    :"+(3|5));    //OR operator, Outpur 7
       
       System.out.println("3 XOR 5 is    :"+(3^5));    //XOR operator, Outpur 6
       
       System.out.println("5 Right Shift 2 is    :"+(5>>2));    //Right Shift operator, Outpur 1
       
       System.out.println("3 Left Shift 2 is    :"+(3<<2));    //Left Shift operator, Outpur 12
       
       System.out.println("10 Zero Fill Right Shift 2 is    :"+(10>>>2));    //Zero Fill Right Shift operator, Outpur 2
       
       System.out.println("3 is Bitwise Compliment is    :"+(~3));    //Bitwise Compliment operator, Outpur -4             
       
    }    
}





* Download this java code, then compile it and run.



The Logical  Operators


The logical operators are use for get logical results through  the conditional statements.


Description for Conditional statement 

If, Else Statement

    if(Expression1)
         statement1;

    else if(Expression2)
         statement2;
    else 
         statement3;

If expression1 true then statement1 is executed,
else if expression2 true statement2 is executed,
otherwise statement3 is executed. else is use optional.


You can use one or more statements under the expression. All the statements should be include within curly brackets. 



    if(Expression1) {
                   statement1;
                   statement2;
                    }
    else if(Expression2) {
                   statement1;
                   statement2;
                   statement3;
                          }
    else {
                   statement1;
                   statement2;
         }



         
The table shows all compound logical operators.


Operator
Operator
Meaning
Boolean Operator
Short circuit Operator

&
&&
Logical AND
|
||
Logical OR
!

Logical NOT

        
   

Logical AND

   
Expression
result
Condition a
Condition b
a & b
true
true
true
true
false
false
false
true
false
false
false
false

    
         

Logical OR



Expression
result
Condition a
Condition b
a | b
true
true
true
true
false
true
false
true
true
false
false
false




Logical NOT



Expression
Result
Condition a
Condition !a
true
false
false
true




The Relational Operators.

The relational operators determine if one operand is equal to, grater then, less than, or not equal to another operand.

Relational operators are can used for conditional statement. We discussed about the conditional statement under the  Logical operator topic.




The table shows all compound Relational operators.



Operator
Meaning
!=
not equal to
==
is equal to
< 
less than
> 
greater than
>=
greater than or equal to
<=
less than or equal to

  

Example for the relational operator.




public class Test { 
    
    public static void main(String args[]){
        
        System.out.println("Is 3 not equal 4               :"+(3!=4));        //output is true.
    
        System.out.println("Is 10 equal 10                 :"+(5==10));       //output is false.
        
        System.out.println("Is 8 less then 8               :"+(8<8));         // output is false.
        
        System.out.println("Is 100 grater then 50          :"+(100>50));      //output is true.
        
        System.out.println("Is 7 grater then or equal 7    :"+(7>=7));        //output is true.
        
        System.out.println("Is 3 less then or equal 4      :"+(3<=4));        //output is true.
    }    
}





* Download this java code, then compile it and run.


Discussion-


Operator
Disscussion
3!=4
3 is not equal 4, So operator give output for true.
5==10
5 is not equal 10, So operator give output for false.
8<8
8 is equal 8, So operator give output for false.
100>50
100 is grater then 50, So operator give output for true.
7>=7
7 is equal 7, So operator give output for true.
3<=4
3 is lessthen 4, So operator give outpur for true.




The Operator Precedence 



Operator Precedence is very important to coding.

When expressions are used in any computer language, We want to determined the order of expressions. 


Example-

x=3+7*2(5-2)

The value of will be 45 because (5-2) evaluate first, then evaluate 7*6, finally evaluate 3+42.
  

Operator Precedence is as follows-







The summary of above order operator precedence

1 :- Increment, Decrements operators
2 :- Arithmetic operators
3 :- Comparison operators
4 :- Logical Operators
5 :- Assignment operators




No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment