Java/J2ee Programming Training Java Type Casting
Page 2Classification: Restricted Agenda • Conversion of one data type to another. • Implicit ( lower data type to higher data type ) • Explicit ( higher data type to lower data type ) .
Implicit conversion • Implicit – byte b = 2; – int i= a ; 0000000 0000000 0000000 00000010 Byte 4 Byte 3 byte2 byte1 00000010 byte1
• Explicit – Int I = 258; (0000_0000 0000_0000 0000_0001 0000_0010 ) – byte b =(byte) I ; 0000_0000 0000_0000 0000_0001 0000_0010 Byte 4 Byte 3 byte2 byte1 0000_0010 byte1 Explicit conversion
• Explicit – Int I = 258; (0000_0000 0000_0000 0000_0001 0000_0010 ) – byte b = (int)i; 0000_0000 0000_0000 0000_0001 0000_0010 Byte 4 Byte 3 byte2 byte1 0000_0010 byte1 Explicit conversion
• Rectangle IS- A Shape – Rectangle has all properties of shape • Rectangle has dim1, dim2 • Rectangle has area method 1. Rectangle rect= new Rectangle (10,20); 2. Shape s = null; 3. s = rect; 4. s.area(); 1. Rectangle rect= new Rectangle (); 2. Shape s; 4. s.area() area() Shape area() Rectangle area() Triangle Int d1, d2; rect=00xx d1 = 20 d2 = 30 s= NULL 00xx s= 00xx JVM will invoke the method based on the type of object reference variable is referring to @ runtime.  “s” refers to Rectangle object at run time.  JVM invokes area method of Rectangle class s =rect Typecasting—Implicit( child -> parent)
Implicit • Triangle is a Shape – Triangle has all properties of shape • Triangle has dim1, dim2 • Triangle has area method 1. Triangle tri= new Triangle(20, 30 ); 2. Shape s = null; 3. s = tri; 4. s.area(); 1. Triangle tri= new Triangle(); 2. Shape s; 4. s.area() area() Shape area() Rectangle area() Triangle Int d1, d2; tri=00xx d1 = 20 d2 = 30 s= NULL 00xx s= 00xx JVM will invoke the method based on the type of object reference variable is referring to @ runtime.  “s” refers to Triangle object at run time.  JVM invokes area method of Triangle class s =tri Typecasting(reference data types)
Shape int dim1; Int dim2; void area(); Rectangle int d1 , d2; void area(){} 1. Rectangle is a shape i. Rect can be assigned to shape 2. Shape s; 3. Rectangle rect = new Rectangle(); 4. s = rect ; shape= rectangle
Shape 1. Cuboid is a Rectangle i. Has all properties of Rectangle ii. Cuboid can be assigned to Rectangle 2. Rectangle rect; 3. rect = new Cuboid(); int dim1; Int dim2; void area(); Rectangle int d1 , d2; void area(){} Cuboid int d1 , d2; void area(){} rect = cuboid shape= rectangle
Shape int dim1; Int dim2; void area(); Rectangle int d1 , d2; void area(){} Cuboid int d1 , d2; void area(){} Triangle int d1 , d2; void area(){} rect = cuboid shape= cuboid rect= triangle shape= rectangle Cannot assign classes @ same level in hierarchy
Explicit • Type casting base cls>child class – Assigning reference variable of base class to child class – Student = User; Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Typecasting(reference data types)
Typecasting(reference data types) Explicit • performOperation(User u ) accepts a parameter of type User. void performOperation(User u) { } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Hey Cindrela, performOperation(User u)can accept user and its subclass( Student, Faculty as its parameter). yup,it can handle more than one object….its polymorphic in nature
Hey Cindrela, performOperation(User u)can accept user and its subclass( Student, Faculty as its parameter). Means.. I can pass either pass User, Student, Faculty as a parameter…. yup,it can handle more than one object….its polymorphic in nature wow…That’s a gud feature in java .. I don’t have to write a separate method for each type of user..Ahhh gud void performOperations( User u) void performOperations( Student s ) void performOperations( Faculty f) lets explore further
Typecasting(reference data types) Explicit : : A faculty comes @ 9:00 to perform following operation.. void performOperation(User u) { u.login(); u.logoff(); u.takeExam(); } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Check whether the user is Faculty or User. Let the candidate takeExam if and only if he is a Student.
Compile Time Binding Explicit Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Faculty f = (Faculty)u login logoff evaluate login logoff void performOperation(User u) { u.login(); u.logoff(); u.evaluate(); } User login logoff u.login() PASS u.logoff PASS u.evaluate() NO NO void performOperation(User u) { u.login(); u.logoff(); Faculty f = ( Faculty )u; f.evaluate(); }
Typecasting Explicit : : A faculty comes @ 9:00 to perform following operation.. void performOperation(User u) { u.login(); u.logoff(); Faculty f = (Faculty )u; f.evaluate(); } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty student..?/] ????
Explicit Conversion Explicit : : A student comes @ 9:05 to perform following operation.. void performOperation(User u) { u.login(); u.logoff(); Faculty f = (Faculty )u; f.evaluate(); } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty JOHN:I am receiving classCastException error MIKE: perform operation method has received an object of type student your user is of type Student but u are trying to cast it to FACULTY. JOHN:How do I debug the error. MIKE: Uas u hav passed the object of type student… cast user to Student. Student s = ( Student )user;
Hey Charles,We are passing Student and Faculty to perfromOperation(User u) method… How did I check whether user is an object of type Student or Faculty so that I can invoke takeExam() for Student and evaluate() for Faculty Hey cindrella, u can use instance of operator to find out whether the object is type of Student or Faculty then typecast it accordingly. Thanks Charles, Can u please show the snippet for the same. if ( user instanceof Student ) { Student s = ( Student ) u; } else if ( user instanceof Faculty) { Faculty f= (Faculty ) u } Wow, that’s great. Let me try.!!!!
instanceof operator Explicit void performOperation(User u) { u.login(); u.logoff(); if( u instanceof Student ) { Student s = ( Student ) u; s.takeExam() } else if( u instanceof Faculty ) { Faculty f = (Faculty ) f; f.evaluate(); } } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty
Page 30Classification: Restricted Thank You

Java Type Casting

  • 1.
  • 2.
    Page 2Classification: Restricted Agenda •Conversion of one data type to another. • Implicit ( lower data type to higher data type ) • Explicit ( higher data type to lower data type ) .
  • 3.
    Implicit conversion • Implicit –byte b = 2; – int i= a ; 0000000 0000000 0000000 00000010 Byte 4 Byte 3 byte2 byte1 00000010 byte1
  • 4.
    • Explicit – IntI = 258; (0000_0000 0000_0000 0000_0001 0000_0010 ) – byte b =(byte) I ; 0000_0000 0000_0000 0000_0001 0000_0010 Byte 4 Byte 3 byte2 byte1 0000_0010 byte1 Explicit conversion
  • 5.
    • Explicit – IntI = 258; (0000_0000 0000_0000 0000_0001 0000_0010 ) – byte b = (int)i; 0000_0000 0000_0000 0000_0001 0000_0010 Byte 4 Byte 3 byte2 byte1 0000_0010 byte1 Explicit conversion
  • 6.
    • Rectangle IS-A Shape – Rectangle has all properties of shape • Rectangle has dim1, dim2 • Rectangle has area method 1. Rectangle rect= new Rectangle (10,20); 2. Shape s = null; 3. s = rect; 4. s.area(); 1. Rectangle rect= new Rectangle (); 2. Shape s; 4. s.area() area() Shape area() Rectangle area() Triangle Int d1, d2; rect=00xx d1 = 20 d2 = 30 s= NULL 00xx s= 00xx JVM will invoke the method based on the type of object reference variable is referring to @ runtime.  “s” refers to Rectangle object at run time.  JVM invokes area method of Rectangle class s =rect Typecasting—Implicit( child -> parent)
  • 7.
    Implicit • Triangle isa Shape – Triangle has all properties of shape • Triangle has dim1, dim2 • Triangle has area method 1. Triangle tri= new Triangle(20, 30 ); 2. Shape s = null; 3. s = tri; 4. s.area(); 1. Triangle tri= new Triangle(); 2. Shape s; 4. s.area() area() Shape area() Rectangle area() Triangle Int d1, d2; tri=00xx d1 = 20 d2 = 30 s= NULL 00xx s= 00xx JVM will invoke the method based on the type of object reference variable is referring to @ runtime.  “s” refers to Triangle object at run time.  JVM invokes area method of Triangle class s =tri Typecasting(reference data types)
  • 8.
    Shape int dim1; Int dim2; voidarea(); Rectangle int d1 , d2; void area(){} 1. Rectangle is a shape i. Rect can be assigned to shape 2. Shape s; 3. Rectangle rect = new Rectangle(); 4. s = rect ; shape= rectangle
  • 9.
    Shape 1. Cuboid isa Rectangle i. Has all properties of Rectangle ii. Cuboid can be assigned to Rectangle 2. Rectangle rect; 3. rect = new Cuboid(); int dim1; Int dim2; void area(); Rectangle int d1 , d2; void area(){} Cuboid int d1 , d2; void area(){} rect = cuboid shape= rectangle
  • 10.
    Shape int dim1; Int dim2; voidarea(); Rectangle int d1 , d2; void area(){} Cuboid int d1 , d2; void area(){} Triangle int d1 , d2; void area(){} rect = cuboid shape= cuboid rect= triangle shape= rectangle Cannot assign classes @ same level in hierarchy
  • 11.
    Explicit • Type castingbase cls>child class – Assigning reference variable of base class to child class – Student = User; Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Typecasting(reference data types)
  • 12.
    Typecasting(reference data types) Explicit •performOperation(User u ) accepts a parameter of type User. void performOperation(User u) { } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Hey Cindrela, performOperation(User u)can accept user and its subclass( Student, Faculty as its parameter). yup,it can handle more than one object….its polymorphic in nature
  • 13.
    Hey Cindrela, performOperation(Useru)can accept user and its subclass( Student, Faculty as its parameter). Means.. I can pass either pass User, Student, Faculty as a parameter…. yup,it can handle more than one object….its polymorphic in nature wow…That’s a gud feature in java .. I don’t have to write a separate method for each type of user..Ahhh gud void performOperations( User u) void performOperations( Student s ) void performOperations( Faculty f) lets explore further
  • 14.
    Typecasting(reference data types) Explicit :: A faculty comes @ 9:00 to perform following operation.. void performOperation(User u) { u.login(); u.logoff(); u.takeExam(); } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Check whether the user is Faculty or User. Let the candidate takeExam if and only if he is a Student.
  • 15.
    Compile Time Binding Explicit Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty Facultyf = (Faculty)u login logoff evaluate login logoff void performOperation(User u) { u.login(); u.logoff(); u.evaluate(); } User login logoff u.login() PASS u.logoff PASS u.evaluate() NO NO void performOperation(User u) { u.login(); u.logoff(); Faculty f = ( Faculty )u; f.evaluate(); }
  • 16.
    Typecasting Explicit : : Afaculty comes @ 9:00 to perform following operation.. void performOperation(User u) { u.login(); u.logoff(); Faculty f = (Faculty )u; f.evaluate(); } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty student..?/] ????
  • 17.
    Explicit Conversion Explicit : :A student comes @ 9:05 to perform following operation.. void performOperation(User u) { u.login(); u.logoff(); Faculty f = (Faculty )u; f.evaluate(); } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty JOHN:I am receiving classCastException error MIKE: perform operation method has received an object of type student your user is of type Student but u are trying to cast it to FACULTY. JOHN:How do I debug the error. MIKE: Uas u hav passed the object of type student… cast user to Student. Student s = ( Student )user;
  • 18.
    Hey Charles,We arepassing Student and Faculty to perfromOperation(User u) method… How did I check whether user is an object of type Student or Faculty so that I can invoke takeExam() for Student and evaluate() for Faculty Hey cindrella, u can use instance of operator to find out whether the object is type of Student or Faculty then typecast it accordingly. Thanks Charles, Can u please show the snippet for the same. if ( user instanceof Student ) { Student s = ( Student ) u; } else if ( user instanceof Faculty) { Faculty f= (Faculty ) u } Wow, that’s great. Let me try.!!!!
  • 19.
    instanceof operator Explicit void performOperation(Useru) { u.login(); u.logoff(); if( u instanceof Student ) { Student s = ( Student ) u; s.takeExam() } else if( u instanceof Faculty ) { Faculty f = (Faculty ) f; f.evaluate(); } } Login() Logoff() User Login() Logoff() takeExam() Student Login() Logoff() evaluate() Faculty
  • 20.