Saturday, May 30, 2015

Casting

package package1;

public class Class1 {

public static void main(String[] args) {

int i = 5 ;
long j = 6;

// When assigning the value to the long we do not use CAST
//cause long has 2 power 64 and int have 2 power 32
//So In this case we are assigning the value to the long so we do not to CAST the variable

j = i;
System.out.println(j);

// When assigning the value to the int we have to use CAST
//cause long has 2 power 64 and int have 2 power 32
//So In this case we are assigning the value to the int so we have to CAST the variable
// cause int is smaller power than long

i = (int) j;
System.out.println(i);

// IN THE SAME WAY LET US SEE THE USE OF OBJECT same way as above

System.out.println("\n-----------First both object have the valus as : ----------");
ClassA cA = new ClassA();
System.out.println(cA);
ClassB cB = new ClassB();
System.out.println(cB+ "\n");

cA = cB ;
System.out.println("-------------After assigning value:--------------\n " + cA);

System.out.println("\n-------------Different Type with Child Class Object--------------");

ClassA a = new ClassB();
System.out.println(a);
ClassB b = new ClassB();
System.out.println(b + "\n");

a = b;
System.out.println(a);

b = (ClassB) a;
System.out.println(b);
}
}

----------------------------------------------------------------------------------------------------------

package package1;

public class ClassA {

}

-----------------------------------------------------------------------------------------------------------

package package1;

public class ClassB extends ClassA{

}
-----------------------------------------------------------------------------------------------------------



No comments:

Post a Comment