in , ,

Java Type Casting

Java Type Casting
Java Type Casting

In this tutorial, we will find out about Java Type Casting and its sorts with the help of examples.

Before you find out about Java Type Casting, ensure you think about Java Data Types.


In this article, you will learn-

Type Casting

The process of converting the value of one data type (int, float, double, and so on) to another data type is known as typecasting.

In Java, there are 13 sorts of type transformation. However, in this instructional exercise, we will just zero in on the significant 2 sorts.

  1. Widening Type Casting
  2. Narrow Type Casting

To learn about other types of type conversion, visit Java Type Conversion (official Java documentation).


Widening Type Casting

In Widening Type Casting, Java automatically converts one data type to another data type.

Example: Converting int to double

class Main {
  public static void main(String[] args) {
    // create int type variable
    int num = 10;
    System.out.println("The integer value: " + num);

    // convert into double type
    double data = num;
    System.out.println("The double value: " + data);
  }
}

Output

The integer value: 10
The double value: 10.0

In the above example, we are allotting the int type variable named num to a double sort variable named data.

Here, Java first converts the int type data into the double type. And afterward, relegate it to the double variable.

On account of Widening Type Casting, the lower data type (having a smaller size) is changed over into the higher data type (having a larger size). Thus there is no misfortune in the data. This is the reason this sort of change happens automatically.

Note: This is otherwise called Implicit Type Casting.


Narrowing Type Casting

In Narrowing Type Casting, we manually convert one data type into another using the parenthesis.

Example: Converting double into an int

class Main {
  public static void main(String[] args) {
    // create double type variable
    double num = 10.99;
    System.out.println("The double value: " + num);

    // convert into int type
    int data = (int)num;
    System.out.println("The integer value: " + data);
  }
}

Output

The double value: 10.99
The integer value: 10

In the above example, we are assigning the double type variable named num to an int type variable named data.

Notice the line,

int data = (int)num;

Here, the int keyword inside the parenthesis demonstrates that that the num variable is changed over into the int type.

On account of Narrowing Type Casting, the higher data types (having larger size) are changed over into lower data types (having smaller size). Consequently, there is a deficiency of information. This is the reason this sort of transformation doesn’t occur automatically.

Note: This is also known as Explicit Type Casting.


Let’s see some of the examples of other type conversions in Java.

Example 1: Type conversion from int to String

class Main {
  public static void main(String[] args) {
    // create int type variable
    int num = 10;
    System.out.println("The integer value is: " + num);

    // converts int to string type
    String data = String.valueOf(num);
    System.out.println("The string value is: " + data);
  }
}

Output

The integer value is: 10
The string value is: 10

In the above program, notice the line

String data = String.valueOf(num);

Here, we have used the valueOf() method of the Java String class to convert the int type variable into a string.


Example 2: Type conversion from String to int

class Main {
  public static void main(String[] args) {
    // create string type variable
    String data = "10";
    System.out.println("The string value is: " + data);

    // convert string variable to int
    int num = Integer.parseInt(data);
    System.out.println("The integer value is: " + num);
  }
}

Output

The string value is: 10
The integer value is: 10

In the above example, notice the line

int num = Integer.parseInt(data);

Here, we have used the parseInt() technique for the Java Integer class to change over a string type variable into an int variable.

Note: If the string variable can’t be changed over into the whole number variable then an exemption named NumberFormatException happens.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Scanner Class

Java Scanner Class

Java autoboxing and unboxing

Java autoboxing and unboxing