Type Casting
Type Conversion In Java
Type conversion in java is the technique of converting one type (data-type) into
other type.
• Type conversion in java can be done between compatible types and
incompatible types. Compatible types are those which can be implicitly
converted.
• For example : Conversion from byte to int is compatible, since int can hold a
byte easily.
• Incompatible types are those which need to be explicitly converted by casting.
• For example : Conversion from int to byte is incompatible, since a byte is
smaller than int and cannot hold it.
• Hence there are two types of type conversion in java :
a. Implicit Type Conversion(Widening)
b. Explicit Type Conversion (Narrowing)
Types of Type Casting
a. Implicit Type Conversion(Widening)
• Widening Casting (automatically) - converting a smaller type to a
larger type size
• byte -> short -> char -> int -> long -> float -> double
b. Explicit Type Conversion (manually)
• Narrowing Casting (manually) - converting a larger type to a smaller
size type
• double -> float -> long -> int -> char -> short -> byte