Tuesday, August 20, 2013

Operators in java





Operators in java

There is a list of all operators used in java.

Operator
Description

Rank
.
Member selection
1
( )
Function

[ ]
Array element reference

-
Unary minus
2
++
Increment

--
Decrement

!
Logical negation

~
Once compliment

*
Multiplication
3
/
Division

%
Modulus

+
Addition
4
-
Subtraction

<< 
Left shift

>> 
Right shift

>>> 
Right shift with zero fill

< 
Less than
6
<=
Less than or equal to

> 
Greater than

>=
Greater than or equal to

Instance of
Type comparison

==
Equality
7
!=
Inequality

&
Bitwise AND
8
^
Bitwise XOR
9
|
Bitwise OR
10
&&
Logical AND
11
| |
Logical OR
12
? :
Conditional operator
13
=
Assignment operator
14
Op =
Shorthand assignment


You can practice these operators using below simple programs.


Arithmetic operators


public class Operators1 {

     public static void main(String[] args) {
         
          float a = 523f;
          float b = 10f;
         
          System.out.println("a = " + a );
          System.out.println("b = " + 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("a * b = " + (a*b));
          System.out.println("a % b = " + (a%b));
         
     }
}


Compound assignment operator


class Apple{
    
public static void  main(String args[]){

     double x= 5,y= 100;

     x+=y;     //x=x+y
     System.out.println(x);

     x-=y;     //x=x-y
     System.out.println(x);

     x*=y;     //x=x*y
     System.out.println(x);

     x/=y;     //x=x/y
     System.out.println(x);

     x%=y;     //x=x%y
     System.out.println(x);
     //in this situation the value of x is changing line by line.

}
}

Increment and decrement operators


public class Operators2 {

     public static void main(String[] args) {
         
          int a = 5;
         
          System.out.println("a = " + a );
          System.out.println("++a = " + ++a);
          System.out.println("a++ = " + a++);
          System.out.println("a = " + a );
     }
}


Relational operators


public class RelationalOperatorDemo {

 public static void main(String[] args) {

  int a = 10;
  int b = 5;

  System.out.println("a : " + a);
  System.out.println("b : " + b);
  System.out.println("a=b : " + (a == b));
  System.out.println("a>b : " + (a > b));
  System.out.println("a!=b : " + (a != b));

 }
}


Logical operators


public class LogicalOperatorDemo {

 public static void main(String[] args) {

  int a = 10;
  int b = 5;
  int c = 1;

  System.out.println("a : " + a);
  System.out.println("b : " + b);
  System.out.println("c : " + c);
  System.out.println(a > b && c > a);
  System.out.println(a > b && !(c > a));
  System.out.println(a > b || c > a);

 }
}


Conditional operator


public class Operators5 {

     public static void main(String[] args) {
         
          int a = 10;
          int b = 5;
         
          System.out.println("a : " + a );
          System.out.println("b : " + b );
          System.out.println("a > b : " + ((a>b)? true :false ));
     }
}


This operator is like if else statement. You can do this one as follows example also.


public class Operators5 {

     public static void main(String[] args) {
         
          int a = 10;
          int b = 5;
         
          System.out.println("a : " + a );
          System.out.println("b : " + b );
         
          if(a>b){
              System.out.println(true);
          }
          else{
              System.out.println(false);
          }   
     }
}

Bitwise compliment


class Apple{
    
     public static void  main(String args[]){
    
          int x = 10;
          int y = ~x;
    
          System.out.println(y);
     }
}



Try these examples to get an experience of operators.


class Apple{

     public static void main(String args[]){
         
     System.out.println( 10+20+ " Add" );
     System.out.println( "Add " + 10+20 );
     System.out.println( "Add " + (10+20) );

     }
}

/*In the second print line 10+20 is got as a string*/