SrcForge

Lesson 6

Type Casting

Learn implicit and explicit casting, safety tips, and examples for Java beginners.

Lesson content

Type Casting in Java

Type casting converts values between Java data types. Implicit casting is automatic; explicit casting is manual and can lose precision.

Type casting is important when mixing types, converting numeric codes to characters, or calling APIs that require specific types.

Definitions

  • Type casting: Converting a value from one data type to another in Java.
  • Implicit casting (automatic): Compiler-performed widening conversion from smaller to larger types (e.g., int to double).
  • Explicit casting (manual): Programmer-forced narrowing conversion from larger to smaller types (e.g., double to int).
  • Widening: Moving to a type with equal or larger range (safe).
  • Narrowing: Moving to a type with smaller range (may lose data).
  • ASCII: Numeric codes that represent characters; useful when casting between char and numeric types.

Implicit casting (automatic)

Pattern: byte -> short -> char -> int -> long -> float -> double. The compiler widens smaller types into larger ones automatically when safe.

Explicit casting (manual)

Pattern: double -> float -> long -> int -> char -> short -> byte. Use parentheses to force a conversion; be aware of rounding or truncation.

Code Examples

public class TypeCastingExample {
    public static void main(String[] args) {
        
        int myInt = 9;
        double myDouble = myInt; // Automatic casting: int to double

        System.out.println(myInt);      // Outputs 9
        System.out.println(myDouble);   // Outputs 9.0


        double anotherDouble = 9.78;
        int anotherInt = (int) anotherDouble; // Manual casting: double to int

        System.out.println(anotherDouble);   // Outputs 9.78
        System.out.println(anotherInt);      // Outputs 9
    }
}
/*            ASCII  -  American Standard Code For Information Interchange

 
            Computers can only understand numbers,
            so an ASCII code is the numerical representation of a character such as 'a' or '@' etc.
 
            The first 32 characters in the ASCII-table
            are unprintable control codes and are used to control peripherals such as printers.
 
            Codes 32-127 are common for all the different variations of the ASCII table, they are
            called printable characters, represent letters, digits, punctuation marks,
            and a few miscellaneous symbols.
 
            65-90  A-Z
            97-122 a-z
            48-57  0-9
            Space  32
        */


import java.util.Scanner; //Package


public class ascii {
    public static void main(String args[])


    {
        
        
        Scanner in = new Scanner(System.in); //Input Class
        System.out.println(" Enter a number : ");
        int alpha = in.nextInt();
        
        System.out.println(alpha+ " is the numeric value of the alphabet"
                + "/character " +(char)alpha); //explicit conversion
        
    
    }
}
 
//Type Casting in Java


import java.lang.*;
 
class casting
{
    public static void main(String args[])
    {
        int a=10;
        double b=a,// Widening casting 
                d=25.5385;

        int c=(int)d; //Narrowing casting
        System.out.println("Int : "+a);
        System.out.println("Double : "+b);
        System.out.println("Double : "+d);
        System.out.println("Int : "+c);
 

    }
}

Q1: What is the difference between implicit and explicit casting?

Implicit casting is automatic and safe; explicit casting is manual and can lose data.

Q2: When should I use explicit casting?

Use explicit casting when converting larger numeric types to smaller ones or when converting numeric codes to char, and you accept truncation.

Q3: Does casting double to int round the number?

No — casting truncates the fractional part. Use Math.round() if you need rounding.