Course Content
Core Java
About Lesson

Type Casting is a method or process that converts a data type into another data type. The conversion is either done by the compiler (automatically) or by developer (explicitly/manually).

Significance

  1. Data Conversion: It is helpful to perform operations involving variables of different data types. eg, converting int to double during division for floating point precision.
  2. Compatibility: Some APIs or methods expect parameters of specific data types. Using type cast we can convert our variables to the required ones.

Types of Cast Typing

  1. Widening Type Casting
  2. Narrowing Type Casting

Widening (Implicit) Type Casting

This type of casting is done automatically by the Java Compiler. In this type casting, we assign a value of the smaller data type to a variable of larger data type. In this smaller data type is converted into larger type. It is widening casting because it widens the range of values that can be represented.

Conversion does not lose information about the magnitude of the numeric value.

Allowed Widening Type Cast

  1. byte to short, int, long, float or double
  2. short to int, long, float or double
  3. char to int, long, float or double
  4. int to long, float or double
  5. long to float or double
  6. float to double
        byte b = 127;
        short s = 198;
        char c = '$';
        int i = 1999;
        long l = 9899;
        float f = 567.23f;
        double d = 879797.585;

        int i1 = b;
        short s1 = b;

        int i2 = c;
        double d2 = c;

        double d3 = f;

Narrowing (Explicit) Type Casting

It is done manually by the Java programmer/developer. It occurs when a value of larger data type is assigned to a variable of smaller data type. It can result in loss of data if the value being cast exceeds the range of the target data type.

Narrowing Type Cast

  1. short to byte or char
  2. char to byte or short
  3. int to byte, short or char
  4. long to byte, short, char or int
  5. float to byte, short, char, int or long
  6. double to byte, short, char, int, long or float
        byte b = 127;
        short s = 198;
        char c = '$';
        int i = 1999;
        long l = 9899;
        float f = 567.23f;
        double d = 879797.585;

        byte b1 = (byte) s;
        char c1 = (char) s;
        
        char c2 = (char) i;
        char c3 = (char) l;
        int i3 = (int) b;

        long l1 = (long) f;

Default Values

In Java, when you declare a variable but don’t initialize it, the variable is assigned a default value based on its data type by the Java Compiler.

Values for

  1. Primitive Data Types
    1. byte, short, int, long: 0
    2. float double: 0.0
    3. char: '\u0000' (null character)
    4. boolean: false
  2. Reference Data Types: Default values for all reference types (objects) is null.
  3. Arrays: Default value of uninitialized array is null.
  4. Local Variables: They must be initialized before use else compiler will throw error.

Scroll to Top