Thursday, September 26, 2013

Operators [Arithmetic, Relational, Ternary]


Arithmetic Operators

Arithmetic operators perform the Arithmetic operations. It takes two or more operands and returns the result of specific mathematical calculation.


Operator
Description
+
add two or more numbers and also concatenate two Strings
-
subtract a number from another.
*
multiply two or more numbers.
/
divide a number by another.
%
give remainder of a division.

Example

    public class Airthmatic_operator   
        {
            public static void main(String args[])
                {
                    int a=10,b=5;
                    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));
                }
   
        }


Relational Operator

A relational operator tests some kind of relation between two identifiers. Relational operator compares between two values and determines the relationship between the same. Relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand.


Operator
Description
operand1 > Operand2
Check if Operand1 is greater than Operand2
Operand1 >= Operand2 
Check if  Operand1 is greater than or equal to Operand2
Operand1 < Operand2 
Check if  Operand1 is less than to Operand2
Operand1 <= Operand2 
Check if  Operand1 is less than or equal to Operand2
Operand1 == Operand2 
Check if Operand1 and Operand2 are equal
Operand1 != Operand2 
Check if Operand1 and Operand2 are not equal



Example

     public class Relational_Operator
       {
            public static void main(String args[])
                {
                    int a=10,b=5;
                    System.out.println("Value of a = "+a+" b= "+b);
                    System.out.println("a=b   "+(a==b));
                    System.out.println("a   "+(a
                    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));
                }
       }
   



Ternary Operator

Conditional operator also known as ternary operator or an inline if. The conditional operator gives a shorter syntax for if-else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned. The second and third operand is separated by ? and :.

Example-1

    public class Ternary_Operator
        {
            public static void main(String args[])
                {
                    int a=10,b=20;
                    System.out.println("The largest between a and b ("+a+","+b+") = "+(a>b?a:b));
                }
            }



Example-2

    public class Ternary_Operator_1
        {
            public static void main(String args[])
                {
                    int a=10,b=20,c=5;
                    System.out.println("The largest between a and b ("+a+","+b+","+c+") = "+(a>b?(a>c? a : c):(b>c?b:c)));
                }
            }




Thursday, September 19, 2013

Operators – [Assignment Operators]



Like keywords, variable, and methods - operators too are integral part of all programming languages and one cannot move an inch without it. Java have the following types of operator.

Assignment Operators / Compound Assignment Operators  
Arithmetic Operators
Relational Operators  
Logical Operators     
Bit wise Operator     
Conditional Operator


Assignment operator(=) used to assign a value to a variable.

int a = 10;

If the varible has an existing value then it will be overwritten by the current one.

Examples :

a = 7; // assigning a litteral
a = a + b; // assigning an an expression

Explicity type casting also used while assigning.

double a=12.566;
int b=(int)a;

Compound Assignment Operators

These operators used while modifying a variable values, that mean the variable should already addigned before. Java have total eleven Compound Assignment Operators.

a=10;
a+=10; // equivalent a=a+10, assign the value of an addition
a-=10;
a*=10;
a/=10;
a%=10;

Following Compound Assignment Operators  used as logical AND, OR, XOR, bitwise left shift, bitwise right shift and  unsigned bitwise right shift :
&=, |= , ^= , <<= , >>=       , >>>=




Examples

public class OperatorDemo_1
{ 
 
    public static void main(String args[])
    { 
       
        int a=10;
        int b=20;
        System.out.println("Assigned values in Variable a and b "+a+","+b); 
        //Assigning String and Character 
        char ch ='x';
        String name="Sachin";
          //Assigning an expression
        int c = a+b; 
        System.out.println("* * Primitive int Assignment  "+ a); 
        //Implicit Casting of variable x and y 
        double d = a; 
        System.out.println("* * Implicit Type Casting int to double "+ d); 
        d=12.45;
        int x=(int)d;
        System.out.println("* * Explicit Type Casting from double to int "+ x); 
        //Compound Assignment Operators 
        x=25;
        System.out.println("\t**Value of x= "+x);
        x+=10;
        System.out.println("\t\tAfter x+=10  "+x);
        x-=10;
        System.out.println("\t\tAfter x-=10  "+x);
        x*=5;
        System.out.println("\t\tAfter x*=5  "+x);
        x/=2;
        System.out.println("\t\tAfter x/=2  "+x);
        x%=2;
        System.out.println("\t\tAfter x%=2  "+x);
       
        x=25;
        System.out.println("\n\t* Value of x= "+x);
        x&=15;
        System.out.println("\t\tAfter bitwise AND x&=15  "+x);
        x|=2;
         System.out.println("\t\tAfter Bitwise OR x|=2  "+x);
        x^=2;
         System.out.println("\t\tAfter Bitwise XOR x^=2  "+x);
        x<<=2;
         System.out.println("\t\tAfter LEFTSHIFT x<<=2  "+x);
        x>>=2;
         System.out.println("\t\tAfter RIGHTSHIFT x>>=2  "+x);
         x=-10;
         System.out.println("\n\t* Value of x= "+x);
        x>>>=2;
        System.out.println("\t\tAfter RIGHTSHIFT x>>=2  "+x);
     
    } 
}



Output

Assigned values in Variable a and b 10,20
* * Primitive int Assignment  10
* * Implicit Type Casting int to double 10.0
* * Explicit Type Casting from double to int 12
          **Value of x= 25
                   After x+=10  35
                   After x-=10  25
                   After x*=5  125
                   After x/=2  62
                   After x%=2  0

          * Value of x= 25
                   After bitwise AND x&=15  9
                   After Bitwise OR x|=2  11
                   After Bitwise XOR x^=2  9
                   After LEFTSHIFT x<<=2  36
                   After RIGHTSHIFT x>>=2  9

          * Value of x= -10
                   After RIGHTSHIFT x>>=2  1073741821

Thursday, September 12, 2013

Type Casting



When a value of one type is assigned to another is known as type casting.  Primitive data types occupy different sizes in the memory. Such as, an integer is 32 bit and long is 64 but. For passing the values between different data types we need type casting.   

There are two type of casting

1. Implicit or  Upcasting
2. Explicit or Downcasting

Implicit type casting possible when both types are compatible and the destination type is larger than the source.



In Explicit conversion never take place automatically. Converting a double or long to an int is not possible because it requires more storage space. For a conversion we use an explicit conversion.



Example-1 :

//between primitive data type

    public class TypeCast
        {
            public static void main(String args[])
                {
                    //Implicit Type Casting or Downcasting
                    int a=10;
                    long b=a;
                    double d=a;
                    System.out.println("\nInteger a ="+a+" Long b= "+b+" Double d= "+d);
                    //Explicit Type Casting or Upcasting
                    d=12.55;
                    b=1234;
                    a=(int)b;
                    System.out.println("\nLong b= "+b+ " Integer a ="+a);
                    a=(int)d;
                    System.out.println("\nDouble d= "+d+ " Integer a ="+a);
               }
      }



Example-2 :

//between Objects

//Object Type Casting
    class Object
        {
             void main()
                {
                    //Object Type Casting
                    System.out.println("Test");
                }
        }

    public class Object1 extends Object
       {
                public static void main(String args[])
                {
                    Object O1=new Object();
                    Object1 O2=new Object1();
                    O1=O2;
                   
                }
       }




Thursday, September 5, 2013

PSVM – The main Method




Most of the procedural languages, the execution take place in top to bottom. As for Java is an Object Oriented Programming language and it's work around the objects the execution neither take place from bottom to top or top to bottom. Therefore, it is for Java Virtual Machine[JVM] to know the entry point for the execution of a program and it is decided upon that the execution would start from a main() function.

The main() function restricted to a strict system defined prototype.

public static void main(String args[])

public: The "main" method should declare with access spcifier public, so the method can be accessed from anywhere. It will make this main method invokable without creating an instance of this class.

static: Would allows to call the methos without creating any instance.

void: This keyword indicated that function does not return anything.

main: 'main', is the name of the method and is indicate the entry point of a program execution.

String args[]: This allows users can pass some command line parameter  while executing the program but it is not compulsory.


However, as we know that main() method declared as static and that indicate that the method belongs to the class itself and not to the objects created by the class. So, it is not require to to create an object to call an static method or access a static Data Member. But, while accessing other non-static data member or method from a static member function we need an object.


Example – 1



    public class Static_1
        {
            int a,b;
            static int c;
                public static void main(String args[])
                    {
                        Static_1 S1 = new Static_1();
                        S1.a=40; //Non Static Data Member
                        S1.b=60;
                        c=S1.a+S1.b; //c is static data member
                        System.out.println("Sum = "+c);
                       
                   }
         }


Example – 2



//Static VS Non-Static
    public class Static_2
        {
                public static void main(String args[])
                    {
                        Static_2 S2 = new Static_2();
                        S2.Sum(); //calling a static method
                        product(); //calling a static method
                   }

                public static void product()
                    {
                        int a=10,b=30;
                        System.out.println("Product = "+(a*b));
                    }
                public void Sum()
                    {
                        int a=10,b=30;
                        System.out.println("Sum = "+(a+b));
                    }
        }