● 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.
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

Type casting

  • 1.
    ● Type castingis 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  Typesof 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 PRIMITIVEDATA 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 DataTypes  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 { voidshow1() { System.out.println (“I am class one“); } } class Two extends One { void show1() { System.out.println(“I am class Two”); } }
  • 9.
    Widening in referenceddata 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().