SlideShare a Scribd company logo
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

What's hot (19)

[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015][Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
Mumbai B.Sc.IT Study
 
Java Questioner for
Java Questioner for Java Questioner for
Java Questioner for
Abhay Korat
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Quiz test JDBC
Quiz test JDBCQuiz test JDBC
Quiz test JDBC
vacbalolenvadi90
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
Omprakash Chauhan
 
Java Quiz
Java QuizJava Quiz
Java Quiz
Dharmraj Sharma
 
2.dynamic
2.dynamic2.dynamic
2.dynamic
bashcode
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
Csharp4 generics
Csharp4 genericsCsharp4 generics
Csharp4 generics
Abed Bukhari
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
Nehal Shah
 
Infix to postfix
Infix to postfixInfix to postfix
Infix to postfix
Saeed Farooqi
 
Lecture 3.1 bt
Lecture 3.1 btLecture 3.1 bt
Lecture 3.1 bt
btmathematics
 
07. haskell Membership
07. haskell Membership07. haskell Membership
07. haskell Membership
Sebastian Rettig
 
Functional programming-advantages
Functional programming-advantagesFunctional programming-advantages
Functional programming-advantages
Sergei Winitzki
 
Type casting
Type castingType casting
Type casting
simarsimmygrewal
 
Unambiguous functions in logarithmic space - CiE 2009
Unambiguous functions in logarithmic space - CiE 2009Unambiguous functions in logarithmic space - CiE 2009
Unambiguous functions in logarithmic space - CiE 2009
Michael Soltys
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
sangeethajames07
 
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015][Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2015]
Mumbai B.Sc.IT Study
 
Java Questioner for
Java Questioner for Java Questioner for
Java Questioner for
Abhay Korat
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
Omprakash Chauhan
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
Nehal Shah
 
Functional programming-advantages
Functional programming-advantagesFunctional programming-advantages
Functional programming-advantages
Sergei Winitzki
 
Unambiguous functions in logarithmic space - CiE 2009
Unambiguous functions in logarithmic space - CiE 2009Unambiguous functions in logarithmic space - CiE 2009
Unambiguous functions in logarithmic space - CiE 2009
Michael Soltys
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
sangeethajames07
 

Similar to Java Type Casting (20)

8 polymorphism
8 polymorphism8 polymorphism
8 polymorphism
Abhijit Gaikwad
 
Java Datatypes
Java DatatypesJava Datatypes
Java Datatypes
Mayank Aggarwal
 
chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)
It Academy
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
Ólafur Andri Ragnarsson
 
Data types in java
Data types in javaData types in java
Data types in java
HarshitaAshwani
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
Tushar Desarda
 
Java & OOP Core Concept
Java & OOP Core ConceptJava & OOP Core Concept
Java & OOP Core Concept
Pin-Lun Huang
 
Type casting
Type castingType casting
Type casting
Ruchika Sinha
 
L02 Software Design
L02 Software DesignL02 Software Design
L02 Software Design
Ólafur Andri Ragnarsson
 
InheritanceAndPolymorphismprein Java.ppt
InheritanceAndPolymorphismprein Java.pptInheritanceAndPolymorphismprein Java.ppt
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
Chapter 8 Polymorphism
Chapter 8 PolymorphismChapter 8 Polymorphism
Chapter 8 Polymorphism
OUM SAOKOSAL
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - Polymorphism
Oum Saokosal
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
Mahi Mca
 
Core Java Programming Language (JSE) : Chapter VI - Class Design
Core Java Programming Language (JSE) : Chapter VI - Class DesignCore Java Programming Language (JSE) : Chapter VI - Class Design
Core Java Programming Language (JSE) : Chapter VI - Class Design
WebStackAcademy
 
Type cast operator
Type cast operatorType cast operator
Type cast operator
ASHUTOSH TRIVEDI
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
Ayush Gupta
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
Woxa Technologies
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
Nilesh Dalvi
 
chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)
It Academy
 
Java & OOP Core Concept
Java & OOP Core ConceptJava & OOP Core Concept
Java & OOP Core Concept
Pin-Lun Huang
 
InheritanceAndPolymorphismprein Java.ppt
InheritanceAndPolymorphismprein Java.pptInheritanceAndPolymorphismprein Java.ppt
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
Chapter 8 Polymorphism
Chapter 8 PolymorphismChapter 8 Polymorphism
Chapter 8 Polymorphism
OUM SAOKOSAL
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - Polymorphism
Oum Saokosal
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
Mahi Mca
 
Core Java Programming Language (JSE) : Chapter VI - Class Design
Core Java Programming Language (JSE) : Chapter VI - Class DesignCore Java Programming Language (JSE) : Chapter VI - Class Design
Core Java Programming Language (JSE) : Chapter VI - Class Design
WebStackAcademy
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
Ayush Gupta
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
Nilesh Dalvi
 
Ad

More from AathikaJava (17)

Java While Loop
Java While LoopJava While Loop
Java While Loop
AathikaJava
 
Java Webservices
Java WebservicesJava Webservices
Java Webservices
AathikaJava
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
AathikaJava
 
Java Session
Java SessionJava Session
Java Session
AathikaJava
 
Java Servlet Lifecycle
Java Servlet LifecycleJava Servlet Lifecycle
Java Servlet Lifecycle
AathikaJava
 
Java Rest
Java Rest Java Rest
Java Rest
AathikaJava
 
Java Request Dispatcher
Java Request DispatcherJava Request Dispatcher
Java Request Dispatcher
AathikaJava
 
Java Polymorphism Part 2
Java Polymorphism Part 2Java Polymorphism Part 2
Java Polymorphism Part 2
AathikaJava
 
Java MVC
Java MVCJava MVC
Java MVC
AathikaJava
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java Polymorphism
AathikaJava
 
Java Spring
Java SpringJava Spring
Java Spring
AathikaJava
 
Mapping Classes with Relational Databases
Mapping Classes with Relational DatabasesMapping Classes with Relational Databases
Mapping Classes with Relational Databases
AathikaJava
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
AathikaJava
 
Java Encapsulation and Inheritance
Java Encapsulation and Inheritance Java Encapsulation and Inheritance
Java Encapsulation and Inheritance
AathikaJava
 
Hibernate basics
Hibernate basicsHibernate basics
Hibernate basics
AathikaJava
 
Java Filters
Java FiltersJava Filters
Java Filters
AathikaJava
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
AathikaJava
 
Java Webservices
Java WebservicesJava Webservices
Java Webservices
AathikaJava
 
Java Servlet Lifecycle
Java Servlet LifecycleJava Servlet Lifecycle
Java Servlet Lifecycle
AathikaJava
 
Java Request Dispatcher
Java Request DispatcherJava Request Dispatcher
Java Request Dispatcher
AathikaJava
 
Java Polymorphism Part 2
Java Polymorphism Part 2Java Polymorphism Part 2
Java Polymorphism Part 2
AathikaJava
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java Polymorphism
AathikaJava
 
Mapping Classes with Relational Databases
Mapping Classes with Relational DatabasesMapping Classes with Relational Databases
Mapping Classes with Relational Databases
AathikaJava
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
AathikaJava
 
Java Encapsulation and Inheritance
Java Encapsulation and Inheritance Java Encapsulation and Inheritance
Java Encapsulation and Inheritance
AathikaJava
 
Hibernate basics
Hibernate basicsHibernate basics
Hibernate basics
AathikaJava
 
Ad

Recently uploaded (20)

TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 

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