0% found this document useful (0 votes)
32 views

Type Casting Notes

Typecasting involves converting data from one type to another. There are two types of typecasting in Java: primitive typecasting and derived typecasting. Primitive typecasting converts primitive types and can be either automatic widening to a larger type or explicit narrowing using cast operators. Derived typecasting converts objects between subclasses and superclasses, which can be automatic upcasting or requires explicit downcasting and risks runtime exceptions if the object types do not match.

Uploaded by

AbhilashRaju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Type Casting Notes

Typecasting involves converting data from one type to another. There are two types of typecasting in Java: primitive typecasting and derived typecasting. Primitive typecasting converts primitive types and can be either automatic widening to a larger type or explicit narrowing using cast operators. Derived typecasting converts objects between subclasses and superclasses, which can be automatic upcasting or requires explicit downcasting and risks runtime exceptions if the object types do not match.

Uploaded by

AbhilashRaju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Typecasting:

Converting one type to another type is called type


casting

Java supports two types of type casting


1. Primitive type casting
2. Derived type casting

Primitive type casting: Converting one primitive type to


another primitive type is known as primitive type casting.

1. auto widening
Converting a smaller primitive type to the any of the
bigger primitive type is known as widening, since widening
is done automatically by compiler it is also called as
auto widening.

2.explicit narrowing
Converting the bigger primitive type to the smaller
primitive type is known as explicit narrowing. It should
be explicitly specified in the program using ()cast
operator by the use.

Derived casting: Converting an object to behave like


another possible type is known as derived casting. In
order to convert an object to another type the class
should have IS Arelationship

Up casting:
Converting an object of sub class to behave like any of
the super class type .
It is automatically done by compiler hence called auto up
cast.

Down casting:
Converting an upcasted object to behave like subclass
type.
It should be explicitly specified in the program by the
user.Down casting cannot be directly done .only an
upcasted object can be down casted.

A explicit downcast of a super class object to any of the


sub class type compiles but throws runtime exception-
class cast exception

You might also like