Skip to content

Type Casting

public class TypeCasting {
    public static void main(String[] args) {
        int num1 = 4;
        double num2 = 5.56789;
        char ch = 'l';

        // Casting a double to an integer
        // This will truncate the decimal part
        int castedNum2 = (int) num2;
        System.out.println("The integer value of num2 is " + castedNum2);

        // Casting a character to an integer
        // It will give the ASCII value of the character
        int castedCh = (int) ch;
        System.out.println("The integer value of ch is " + castedCh);
    }
}

Oh hi there 👋 It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.