Type Conversions in Java

In this Java tutorial, you will learn about type conversion in Java, converting a value from one datatype to another, including implicit and explicit conversion methods, with examples.

What is a Type Conversion

Java Type Conversion – Type conversion in Java refers to the process of converting a variable from one data type to another. Java supports both implicit and explicit conversions, allowing for flexibility and control over data manipulation.

Implicit Type Conversion (Widening)

In Java, an implicit type conversion (or widening conversion) happens when a smaller data type is converted into a larger one, such as from int to long. This type of conversion is performed automatically by the compiler.

Example of Implicit Conversion

</>
Copy
public class TypeConversionDemo {

    public static void main(String[] args) {
        int intVal = 100;
        double doubleVal = intVal; // Implicit conversion from int to double
        System.out.println("Integer value: " + intVal);
        System.out.println("Double value: " + doubleVal);
    }
}

Output

Integer value: 100
Double value: 100.0

Here, the integer intVal is automatically converted to a double. Since a double can hold larger numbers, this conversion is safe and handled by Java.

Explicit Type Conversion (Narrowing)

Explicit type conversion (or narrowing) is needed when a larger data type needs to be converted to a smaller one, such as from double to int. This conversion is done manually by the programmer using casting syntax.

Example of Explicit Conversion

</>
Copy
public class TypeConversionDemo {

    public static void main(String[] args) {
        double doubleVal = 9.78;
        int intVal = (int) doubleVal; // Explicit conversion from double to int
        System.out.println("Double value: " + doubleVal);
        System.out.println("Integer value after casting: " + intVal);
    }
}

Output

Double value: 9.78
Integer value after casting: 9

Here, the double doubleVal is explicitly cast to an integer, truncating the decimal part and retaining only the integer portion.

Conversion Between Wrapper Types

Java provides wrapper classes (e.g., Integer, Double) to handle conversions between different types. These classes contain methods that allow easy conversion, especially from strings to numeric types.

Example of String to Integer Conversion

</>
Copy
public class TypeConversionDemo {

    public static void main(String[] args) {
        String strVal = "123";
        int intVal = Integer.parseInt(strVal); // Convert String to int
        System.out.println("String value: " + strVal);
        System.out.println("Integer value: " + intVal);
    }
}

Output

String value: 123
Integer value: 123

The method Integer.parseInt() takes a string containing a numeric value and converts it to an integer. Similarly, methods exist for other types such as Double.parseDouble() and Long.parseLong().

Type Conversion Tutorials

In the following list, we covered tutorials on how to convert from one data type to another, with detailed explanation and examples.