Pages

TypeCasting

Type Conversion:
A variable of one type can receive the value of another type

Widening conversion : Variable of lower capacity can be assigned to another variable of higher capacity.
Narrowing conversion :Variable of higher capacity can be assigned to another variable of lower capacity, which may lead to loss of data .

Type Casting :

Whenever a larger type is converted to a smaller type, we have to explicitly specify the type cast operator
This prevents accidental loss of data.

Among 8 primitive data type 6 are numeric that only matters here :

Byte < Short < int < long < float < double(wider) | other are char,Boolean


1)
class A
{
 public static void main(String[] args) 
 {
  int i=10;
  double d=i;//Here integer converting to double ,
                           //auto widening happening ,hence no casting required
  System.out.println(i);
  System.out.println(d);

 }
}
/*
10
10.0
*/
---------------------------------------------------------------------------
2)
class B
{
 public static void main(String[] args) 
 {
  byte b=10;
  int i=b;
  double d=i;
  float f=b;
  long l=i;
  System.out.println("done");
 }
}
/*
done
*/
---------------------------------------------------------------------------
3)
class C
{
 public static void main(String[] args) 
 {
  int i=10;
  test(i);
 }
 static void test(double d)
 {
  System.out.println("test(double)");
 }
}
/*
test(double)
*/
---------------------------------------------------------------------------
4)
class D
{
 public static void main(String[] args) 
 {
  int i=test();
  System.out.println("done");
 }
 static byte test()
 {
  return 10;
 }
}
---------------------------------------------------------------------------
5)
class E
{
 public static void main(String[] args) 
 {
  int i=10;
  double d=(double)i;//explicit conversion,not required ,compiler will do automatic
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
6)
class F
{
 static long test()
 {
  int i=10;
  return i;
 }
 public static void main(String[] args) 
 {
  double d=test();
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
7)
class G
{
 static float test1(short s)
 {
  return test2(s);
 }
 static long test2(int i)
 {
  return i;
 }
 public static void main(String[] args) 
 {
  byte b=10;
  double d=test1(b);
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
8)
class H
{
 public static void main(String[] args) 
 {
  double d=10.9;
  int i=(int)d;
  System.out.println(d);
  System.out.println(i);
 }
}
/*
10.9
10
*/
---------------------------------------------------------------------------
9)
class I
{
 public static void main(String[] args) 
 {
  long x=10;
  test((int)x);
  System.out.println("done");
 }
 static void test(int i)
 {
 }
}
/*
done
*/
---------------------------------------------------------------------------
10)
class Demo
{
 public static void main(String[] args) 
 {
  byte a=10;
  short b=20;
  int c=10;
  long d=20;
  float f=20;
  double g=20.09;
  a=b;//error or keep a=(byte)b;
  b=a;
  d=c;
  c=d;//error or keep c=(int)d;
  g=f;
  f=g;//error or keep f=(float)g;
  System.out.println("done");
 }
}
/*
Demo.java:11: possible loss of precision
found : short
required: byte
a=b;
^
Demo.java:14: possible loss of precision
found : long
required: int
c=d;
^
Demo.java:16: possible loss of precision
found : double
required: float
f=g;
*/
---------------------------------------------------------------------------
11)
class K
{
 static int test(long x)
 {
  return (int)x;
 }
 public static void main(String[] args) 
 {
  int i=10;
  byte b=(byte)test(i);
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
12)
class L 
{
 public static void main(String[] args) 
 {
  double d=20;
  int i=(byte)d;
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
13)
class M 
{
 public static void main(String[] args) 
 {
  double d=20.9;
  int i=(short)(int)(byte)(long)d;
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
//Consider below hierarchy for further programs

class A 
{
}
class B extends A
{
}
class C extends B
{
}
class D extends C
{
}
class E extends D
{
}
class F extends E
{
}
see further general example in different .java files
---------------------------------------------------------------------------
14)
class Manager1
{
 public static void main(String[] args) 
 {
  A a1=new A();
  B b1=new B();
  C c1=new C();
  D d1=new D();
  E e1=new E();
  F f1=new F();
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
15)
class Manager2
{
 public static void main(String[] args) 
 {
  A a1=new A();
  A a2=a1;
  B b1=new B();
  B b2=b1;
  C c1=null;
  C c2=c1;
  D d1,d2;
  d1=d2=new D();
  E e1,e2=new E();
  e1=e2;
  F f1,f2=null;
  f1=f2;
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
16)
/*
reference variable a1 is A type,object is B type ,right side B is subclass of A.
so automatically B is converting to upper one,auto upcasting happening.
*/
class Manager3
{
 public static void main(String[] args) 
 {
  A a1=new B();  //auto upcasting
  B b1=new D();
  C c1=new E();
  D d1=new F();
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
17)
class Manager4
{
 public static void main(String[] args) 
 {
  B b1=null;
  C c1=null;
  D d1=new D();
  E e1=null;
  F f1=new F();
  b1=f1;
  c1=e1; //automatic E type can be converted in C type by compiler
  e1=f1;
  b1=d1;
  d1=e1;
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
18)
class Manager5
{
 public static void main(String[] args) 
 {
  E e1=new E();
  test(e1);
 }
 static void test(C c1) //automatic E type can be converted in C type by compiler
 {
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
19)
class Manager6
{
 static D test()
 {
  F f1=new F();
  return f1; //auto upcasting into D type
 }
 public static void main(String[] args) 
 {
  B b1=test(); //D is auto up-casting into B type
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
20)
class Manager7
{
 static B test1(E e1)
 {
  return test2(e1);
 }
 static C test2(D d1)
 {
  return d1;
 }
 public static void main(String[] args) 
 {
  F f1=new F();
  A a1=test1(f1);
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
21)
class Manager8
{
 public static void main(String[] args) 
 {
  A a1=new C();
  B b1=(B)a1; //has to do explicit down-casting ,if not doing then error
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
22)
class Manager9
{
 public static void main(String[] args) 
 {
  D d1=new F();
  E e1=(E)d1;
  System.out.println("done");
 }
}
//done
---------------------------------------------------------------------------
23)
class Manager10
{
 public static void main(String[] args) 
 {
  E e1=new F();
  test((F)e1);
  System.out.println("done");
 }
 static void test(F f1)
 {
  System.out.println("test(F)");
 }
}
/*
test(F)
done
*/
---------------------------------------------------------------------------
24)
class Manager11
/*
compiler will check only assignment ,hence no error while compiling
a1 is pointing to A type object that can not be converted into B type,hence run-time error*/
{
 public static void main(String[] args) 
 {
  A a1=new A();
  B b1=(B)a1; //without up-casting if attempt down-casting give run time error.
  System.out.println("done");
 }
}
/*
compilation success but run-time exception
Exception in thread "main" java.lang.ClassCastException: A cannot be cast to B
at Manager11.main(Manager11.java:7)
*/
---------------------------------------------------------------------------
25)
class Manager12
{
 public static void main(String[] args) 
 {
  A a1=new B();
  B b1=(B)a1;
  System.out.println("done");
 }
}
/*
done
*/
---------------------------------------------------------------------------
26)
class Manager13
{
 public static void main(String[] args) 
 {
  B b1=new D();
  C c1=(C)b1;
  D d1=(D)c1;
  System.out.println("done");
 }
}
/*
done
*/
---------------------------------------------------------------------------
27)
class Manager14
{
 public static void main(String[] args) 
 {
  B b1=new E();
  C c1=(C)b1;
  D d1=(D)b1;
  E e1=(E)b1;
  //F f1=(F)b1;
  System.out.println("done");
 }
}
/*
done
*/
---------------------------------------------------------------------------
28)
class Manager15
{
 public static void main(String[] args) 
 {
  A a1=new B();
  System.out.println(a1 instanceof A);
  System.out.println(a1 instanceof B);
  System.out.println(a1 instanceof C);
  System.out.println(a1 instanceof D);
 }
}
/*
true
true
false
false
*/
---------------------------------------------------------------------------
29)
class Manager16
{
 public static void main(String[] args) 
 {
  C c1=new D();
  System.out.println(c1 instanceof A);
  System.out.println(c1 instanceof B);
  System.out.println(c1 instanceof C);
  System.out.println(c1 instanceof D);
  System.out.println(c1 instanceof E);
  System.out.println(c1 instanceof F);
 }
}
/*
true
true
true
true
false
false
*/
---------------------------------------------------------------------------
30)
class Manager17
{
 public static void main(String[] args) 
 {
  A a1=new C();
  if(a1 instanceof A)
  {
   A a2=(A)a1;
  System.out.println(1);
  }
  if(a1 instanceof B)
  {
   B b1=(B)a1;
  System.out.println(2);
  }
  if(a1 instanceof C)
  {
   C c1=(C)a1;
  System.out.println(3);
  }
  if(a1 instanceof D)
  {
   D d1=(D)a1;
  System.out.println(4);
  }
  if(a1 instanceof E)
  {
   E e1=(E)a1;
  System.out.println(5);
  }
  if(a1 instanceof F)
  {
   F f1=(F)a1;
  System.out.println(6);
  }
  System.out.println("done");
 }
}
/*
1
2
3
done
*/
---------------------------------------------------------------------------
31)
class A
{
 int i;
 void test1()
 {
  System.out.println("A-test1");
 }
}
class B extends A
{
 int j;
 void test2()
 {
  System.out.println("B-test2");
 }
}
class Manager1
{
 public static void main(String[] args) 
 {
  A a1=new A();
  a1.test1();
  a1.i=10;
  System.out.println(a1.i);
  B b1=new B();
  b1.test1();
  b1.i=10;
  b1.test2();
  b1.j=20;
  System.out.println(b1.i);
  System.out.println(b1.j);
 }
}
/*
A-test1
10
A-test1
B-test2
10
20
*/
---------------------------------------------------------------------------
32)
/*
If ref variable a1 is pointing to B object,then we can't use member of 
B class using ref variable a1 ,for that we have to down cast
WHENEVER auto upcasting is there ,we can not access member object class 
for that we have to do downcast explicitly
*/
class A
{
 int i;
 void test1()
 {
  System.out.println("A-test1");
 }
}
class B extends A
{
 int j;
 void test2()
 {
  System.out.println("B-test2");
 }
}
class Manager2
{
 public static void main(String[] args) 
 {
  A a1=new B();
  a1.i=10;
  a1.test1();
  //a1.j=20;
  //a1.test2();
  System.out.println("done");
 }
}
/*
A-test1
done
*/
---------------------------------------------------------------------------
33)
//down-casting ref var a1 to B type ,now member of class B can be access
class A
{
 int i;
 void test1()
 {
  System.out.println("A-test1");
 }
}
class B extends A
{
 int j;
 void test2()
 {
  System.out.println("B-test2");
 }
}
class Manager3
{
 public static void main(String[] args) 
 {
  A a1=new B();
  a1.i=10;
  a1.test1();
  B b1=(B)a1;//down-casting ref var a1 to B type ,now member of class B can be access
  b1.j=20;
  b1.test2();
  b1.i=30;
  b1.test1();
  System.out.println("done");
 }
}
/*
A-test1
B-test2
A-test1
done
*/
---------------------------------------------------------------------------
34)
Example of polymorphism
/*
while calling 2nd time obj2 is D type pointing to D object,while calling method auto upcasting happening.
if polymorphism is not occurring then method overriding will not happen.
if super class method is static then while overriding in sub class have to use static,otherwise it will give compiler error
if using static even-though up-casting is there ,method will not override
static method are not overriding,because static method are not inheriting to subclass
 */
class C
{
 //void test1()
 static void test1()
 {
  System.out.println("c-test1");
 }
}  
class D extends C
{
 //void test1()
 static void test1()
 {
  System.out.println("D-test1");
 }
}
class Manager4
{
 static void method(C c1)
 {
  c1.test1();//showing polymorphism
 }
 public static void main(String[] args) 
 {
  C obj1=new C();
  D obj2=new D();
  method(obj1);
  method(obj2);
 }
}
/*
c-test1
D-test1
*/
---------------------------------------------------------------------------
35)
class Manager4
{
 static
 {
  System.out.println("Manager4-SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println("Manager4-main");
 }
}
class Manager5 extends Manager4
{
 static
 {
  System.out.println("Manager5-SIB");
 }

}
class Manager6
{
 static
 {
  System.out.println("Manager6-SIB");
 }
 public static void main(String[] args) 
 {
 System.out.println("Manager6-main-b");
 Manager4.main(args);
 System.out.println("Manager6-main-e");
 }
}
class Manager7
{
 static
 {
  System.out.println("Manager7-SIB");
 }
 public static void main(String[] args) 
 {
  System.out.println("Manager7-main-b");
         Manager5.main(args);
  System.out.println("--------");
  Manager4.main(args);
  System.out.println("Manager7-main-e");
 }
}
/*
Manager4:-
Manager4-SIB
Manager4-main
==============
MANAGER5:--
Manager4-SIB
Manager5-SIB
Manager4-main
==============
MANAGER6:-
Manager6-SIB
Manager6-main-b
Manager4-SIB
Manager4-main
Manager6-main-e
================
MANAGER7:-
Manager7-SIB
Manager7-main-b
Manager4-SIB
Manager4-main
--------
Manager4-main
Manager7-main-e
*/


No comments:

Post a Comment