SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
● Type casting is used to convert an object
or variable of one type into another.
●Assigning a value of one type to a variable
of another type is known as type casting.
Syntax :
dataType variableName = (dataType)
variableToConvert ;
Type Casting
 Types of Data Types
◦ Primitive or Fundamental Data Types
◦ Referenced or Advanced Data Types
• Casting Primitive Data Type
▫ Widening - Converting lower data type into higher
data type is called widening.
▫ Narrowing – Converting a higher data type into
lower data type is called narrowing.
Type casting
WIDENING IN PRIMITIVE DATA TYPES
 Char ch = ‘A’ ;
int num = ( int ) ch ;
int x = 9500 ;
float sal = ( float ) x ;
Widening is safe because there will not be
any loss of data. That is why, compiler does
the casting internally and hence this is called
“Implicit Casting”.
 int x = 66 ;
char ch = ( char ) x ;
double d = 12.6879 ;
int n = ( int ) d ;
Narrowing is not safe because there will be
loss of data. That is why, compiler forces the
programmer to use cast operator and hence
this is called “Explicit Casting”.
Casting Referenced Data Types
 A class is referenced data type. Converting a class type
into another class type is also possible through casting.
But the classes should have the same relationship
between them by the way of inheritance.
University
↑
College
↑
Department
Type Casting
 Generalization : Generalization is a
phenomenon where a sub class is
promoted to a super class, and hence
becomes more general . It needs
widening or up-casting.
 Specialization : Specialization is a
phenomenon where a super class is
narrowed down to a sub class. It needs
narrowing or down-casting.
 Class One
{
void show1()
{
System.out.println (“I am class one“);
}
}
class Two extends One
{
void show1()
{
System.out.println(“I am class Two”);
}
}
Widening in referenced data
types
 ClassTest
{
public static void main{String args[]}
{
One o;
o = (One) newTwo();
o.show1();
}
}
Note : we are able to access show1() method of the super
class. But in this case, it is not possible to call show2().
Type casting

More Related Content

PPTX
Agents in Artificial intelligence
PPTX
Typecasting in c
PPT
Ob stephen robbins
PDF
Understanding value education
PPT
Prokaryotes
PDF
Data Visualization With Tableau | Edureka
PPTX
Internet Service Provider-ISP
PPTX
Classification and Regression
Agents in Artificial intelligence
Typecasting in c
Ob stephen robbins
Understanding value education
Prokaryotes
Data Visualization With Tableau | Edureka
Internet Service Provider-ISP
Classification and Regression

What's hot (20)

PPTX
Type casting in java
PPTX
Binary Tree Traversal
PPTX
Functions in c++
PPSX
Data Types & Variables in JAVA
PPT
Data Structures- Part5 recursion
PPT
File handling in c
PPTX
class and objects
PPTX
Pointers in C Programming
PPTX
Data types in c++
PPTX
java interface and packages
PPTX
PPTX
classes and objects in C++
PPTX
PPTX
Structure in C language
PPTX
Types of Constructor in C++
PPTX
Static Data Members and Member Functions
PPTX
Data types in C
PPTX
File in C language
PDF
Arrays in Java
PPT
Structure of a C program
Type casting in java
Binary Tree Traversal
Functions in c++
Data Types & Variables in JAVA
Data Structures- Part5 recursion
File handling in c
class and objects
Pointers in C Programming
Data types in c++
java interface and packages
classes and objects in C++
Structure in C language
Types of Constructor in C++
Static Data Members and Member Functions
Data types in C
File in C language
Arrays in Java
Structure of a C program
Ad

Similar to Type casting (20)

PPTX
Type casting
PPTX
Java-Typecasting-A-Deep-Dive-Exploration.pptx
PPTX
Data types in java
PDF
Structures in c++
PDF
CP Handout#3
PDF
9.0 Typecasting in C Language inc language
PDF
Computer science_xii_2016
PPTX
Machine Learning - Dataset Preparation
PPTX
DOC-20250124-WA0004._20250430_233059_0000.pptx
PPTX
DOC-20250124-WA0004._20250430_233059_0000.pptx
PDF
vb.net.pdf
PDF
Structures in c++
PPT
3. Data types and Variables
PPTX
Presentation on c programing satcture
PPTX
Presentation on c structures
PPTX
unit 1 (1).pptx
PPTX
Type Casting in java programming language.pptx
PPTX
Data Structures and Algorithms_Updated.pptx
PDF
Learn C# Programming - Data Types & Type Conversion
PPT
implementing oop_concept
Type casting
Java-Typecasting-A-Deep-Dive-Exploration.pptx
Data types in java
Structures in c++
CP Handout#3
9.0 Typecasting in C Language inc language
Computer science_xii_2016
Machine Learning - Dataset Preparation
DOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptx
vb.net.pdf
Structures in c++
3. Data types and Variables
Presentation on c programing satcture
Presentation on c structures
unit 1 (1).pptx
Type Casting in java programming language.pptx
Data Structures and Algorithms_Updated.pptx
Learn C# Programming - Data Types & Type Conversion
implementing oop_concept
Ad

More from Ruchika Sinha (20)

PPT
Python Programming Introduction - Loops & Boolean
PPTX
Difference Between Normal & Smart/Automated Home
PPT
Greedy Algorithms WITH Activity Selection Problem.ppt
PPT
Greedy with Task Scheduling Algorithm.ppt
PPT
Greedy Algorithms Huffman Coding.ppt
PPT
DynProg_Knapsack.ppt
PPT
Dijkstra.ppt
PPT
Greedy with Task Scheduling Algorithm.ppt
PPT
Clipping
PPT
Lec22 intel
PPTX
Pc assembly
PPT
PPTX
Installation of motherboard
PPT
Shortest path
PPT
Bellman ford algorithm
PPTX
Python material
PPT
Python3
PPT
Optimization problems
PPT
Regular Grammar
PPTX
Software Teting
Python Programming Introduction - Loops & Boolean
Difference Between Normal & Smart/Automated Home
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy with Task Scheduling Algorithm.ppt
Greedy Algorithms Huffman Coding.ppt
DynProg_Knapsack.ppt
Dijkstra.ppt
Greedy with Task Scheduling Algorithm.ppt
Clipping
Lec22 intel
Pc assembly
Installation of motherboard
Shortest path
Bellman ford algorithm
Python material
Python3
Optimization problems
Regular Grammar
Software Teting

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Current and future trends in Computer Vision.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
web development for engineering and engineering
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Artificial Intelligence
PDF
PPT on Performance Review to get promotions
PPTX
Geodesy 1.pptx...............................................
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
Project quality management in manufacturing
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
additive manufacturing of ss316l using mig welding
R24 SURVEYING LAB MANUAL for civil enggi
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Current and future trends in Computer Vision.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
web development for engineering and engineering
Mechanical Engineering MATERIALS Selection
CH1 Production IntroductoryConcepts.pptx
Artificial Intelligence
PPT on Performance Review to get promotions
Geodesy 1.pptx...............................................
Internet of Things (IOT) - A guide to understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Project quality management in manufacturing
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Automation-in-Manufacturing-Chapter-Introduction.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx

Type casting

  • 1. ● Type casting is used to convert an object or variable of one type into another. ●Assigning a value of one type to a variable of another type is known as type casting. Syntax : dataType variableName = (dataType) variableToConvert ;
  • 2. Type Casting  Types of Data Types ◦ Primitive or Fundamental Data Types ◦ Referenced or Advanced Data Types • Casting Primitive Data Type ▫ Widening - Converting lower data type into higher data type is called widening. ▫ Narrowing – Converting a higher data type into lower data type is called narrowing.
  • 4. WIDENING IN PRIMITIVE DATA TYPES  Char ch = ‘A’ ; int num = ( int ) ch ; int x = 9500 ; float sal = ( float ) x ; Widening is safe because there will not be any loss of data. That is why, compiler does the casting internally and hence this is called “Implicit Casting”.
  • 5.  int x = 66 ; char ch = ( char ) x ; double d = 12.6879 ; int n = ( int ) d ; Narrowing is not safe because there will be loss of data. That is why, compiler forces the programmer to use cast operator and hence this is called “Explicit Casting”.
  • 6. Casting Referenced Data Types  A class is referenced data type. Converting a class type into another class type is also possible through casting. But the classes should have the same relationship between them by the way of inheritance. University ↑ College ↑ Department
  • 7. Type Casting  Generalization : Generalization is a phenomenon where a sub class is promoted to a super class, and hence becomes more general . It needs widening or up-casting.  Specialization : Specialization is a phenomenon where a super class is narrowed down to a sub class. It needs narrowing or down-casting.
  • 8.  Class One { void show1() { System.out.println (“I am class one“); } } class Two extends One { void show1() { System.out.println(“I am class Two”); } }
  • 9. Widening in referenced data types  ClassTest { public static void main{String args[]} { One o; o = (One) newTwo(); o.show1(); } } Note : we are able to access show1() method of the super class. But in this case, it is not possible to call show2().