SlideShare a Scribd company logo
3
Most read
4
Most read
6
Most read
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

More Related Content

ODP
Datatype in JavaScript
PDF
Scala qq
PDF
Under the hood of scala implicits (Scala eXchange 2014)
PDF
Dynamic Type Inference for Gradual Hindley–Milner Typing
PPTX
Puzles C#
PDF
Under the hood of scala implicits (kl10tch 10.03.2015)
PDF
How to start functional programming (in Scala): Day1
PPTX
A well-typed program never goes wrong
Datatype in JavaScript
Scala qq
Under the hood of scala implicits (Scala eXchange 2014)
Dynamic Type Inference for Gradual Hindley–Milner Typing
Puzles C#
Under the hood of scala implicits (kl10tch 10.03.2015)
How to start functional programming (in Scala): Day1
A well-typed program never goes wrong

What's hot (19)

PDF
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
DOCX
Java Questioner for
PPTX
Queue - Data Structure - Notes
DOCX
Quiz test JDBC
PPTX
Sorting and Searching - Data Structure - Notes
PPTX
Java Quiz
PPTX
2.dynamic
PPTX
Stack - Data Structure - Notes
PPT
Csharp4 generics
PPSX
Stacks Implementation and Examples
PDF
Railroading into Scala
PPTX
Infix to postfix
PPTX
Lecture 3.1 bt
PDF
07. haskell Membership
PDF
Functional programming-advantages
PPTX
Type casting
PDF
Unambiguous functions in logarithmic space - CiE 2009
PDF
Applications of stack
DOC
Data structures question paper anna university
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
Java Questioner for
Queue - Data Structure - Notes
Quiz test JDBC
Sorting and Searching - Data Structure - Notes
Java Quiz
2.dynamic
Stack - Data Structure - Notes
Csharp4 generics
Stacks Implementation and Examples
Railroading into Scala
Infix to postfix
Lecture 3.1 bt
07. haskell Membership
Functional programming-advantages
Type casting
Unambiguous functions in logarithmic space - CiE 2009
Applications of stack
Data structures question paper anna university
Ad

Similar to Java Type Casting (20)

PPT
8 polymorphism
PPTX
Java Datatypes
PPTX
chap4 : Converting and Casting (scjp/ocjp)
PPTX
L03 Software Design
PPTX
Data types in java
PPT
java tutorial 3
PDF
Java & OOP Core Concept
PPTX
Type casting
PPTX
L02 Software Design
PPT
06 InheritanceAndPolymorphism.ppt
PPT
InheritanceAndPolymorphismprein Java.ppt
PPT
Chapter 8 Polymorphism
PPT
Java Programming - Polymorphism
PPT
JAVA Polymorphism
PDF
Core Java Programming Language (JSE) : Chapter VI - Class Design
PPTX
Type cast operator
PPTX
Type casting in java
PPTX
A Case Study on Java. Java Presentation
PPT
Java Tutorials
PPT
3. Data types and Variables
8 polymorphism
Java Datatypes
chap4 : Converting and Casting (scjp/ocjp)
L03 Software Design
Data types in java
java tutorial 3
Java & OOP Core Concept
Type casting
L02 Software Design
06 InheritanceAndPolymorphism.ppt
InheritanceAndPolymorphismprein Java.ppt
Chapter 8 Polymorphism
Java Programming - Polymorphism
JAVA Polymorphism
Core Java Programming Language (JSE) : Chapter VI - Class Design
Type cast operator
Type casting in java
A Case Study on Java. Java Presentation
Java Tutorials
3. Data types and Variables
Ad

More from AathikaJava (17)

PPTX
Java While Loop
PPTX
Java Webservices
PPTX
Spring Web MVC
PPTX
Java Session
PPTX
Java Servlet Lifecycle
PPTX
Java Rest
PPTX
Java Request Dispatcher
PPTX
Java Polymorphism Part 2
PPTX
Java MVC
PPTX
Java Polymorphism
PPTX
Java Spring
PPTX
Mapping Classes with Relational Databases
PPTX
Introduction to Java
PPTX
Java Encapsulation and Inheritance
PPT
Hibernate basics
PPTX
Java Filters
PPTX
Encapsulation
Java While Loop
Java Webservices
Spring Web MVC
Java Session
Java Servlet Lifecycle
Java Rest
Java Request Dispatcher
Java Polymorphism Part 2
Java MVC
Java Polymorphism
Java Spring
Mapping Classes with Relational Databases
Introduction to Java
Java Encapsulation and Inheritance
Hibernate basics
Java Filters
Encapsulation

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PPTX
MYSQL Presentation for SQL database connectivity
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
cuic standard and advanced reporting.pdf
Approach and Philosophy of On baking technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
A Presentation on Artificial Intelligence
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
MYSQL Presentation for SQL database connectivity
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
Spectroscopy.pptx food analysis technology
Big Data Technologies - Introduction.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
A comparative analysis of optical character recognition models for extracting...
cuic standard and advanced reporting.pdf

Java Type Casting

  • 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 – 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
  • 5. • 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
  • 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 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)
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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)
  • 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(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
  • 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 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(); }
  • 16. 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..?/] ????
  • 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 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.!!!!
  • 19. 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