Skip to main content

Java Fundamentals

Type conversion

In Java, we work with different types of data. We have:

  • int for numbers without decimal points, like 5 or -6.
  • double for numbers with decimal points, like -5.6 or 7.84.
  • char for single characters, like 'a' or '1'.

Sometimes, we need to change data from one type to another, like from double to int or from int to double. This process is called type conversion.

In this lesson, we'll learn how to do this type conversion in Java. Let's dive in!

Implicit Type Conversion

Sometimes in Java, one type of data is automatically changed to another type. Let's look at an example: 

class Main { 
  public static void main(String[] args) { 
      // create a double type variable 
      double num; 
      // assign int value to double 
      num = 15; 
      System.out.println(num); 
  } 
}

Output-

15.0

In this program, we created a variable called num, which is of type double. However, we assigned an integer value 15 to it.

Java automatically converted the integer value 9 to a double value 9.0. That's why the output is 15.0

This automatic type conversion is known as Implicit Type Conversion. It's like when you have a toy car and you place it on a toy truck, and it automatically adjusts its size to fit on the truck.

What happens when we try to assign a Double value to Int Variable?

Let’s try to understand this scenario with the help of an example - 

class Main { 
   public static void main(String[] args) {
      // create an int type variable 
      int num; 
      // try to assign a double value to int 
      num = 30.78;
      System.out.println(num); 
   } 
}

When you run this code, you'll encounter an error message:

error: incompatible types: possible lossy conversion from double to int number = 30.78;

Here, we are attempting to store the decimal value 30.78 into the integer variable 'num'. However, in Java, an integer variable can't hold values with decimal points. So, converting a double (which can have decimals) to an int can cause a loss of data.

To prevent the loss of data, Java doesn't automatically convert double values to int. It's like trying to fit a big watermelon into a small basket — it just doesn't work!

Explicit Type Conversion

In situations where we need to convert a double to an int, despite the potential data loss, we can use explicit type conversion. This type of conversion forces Java to convert from one type to another, even if it means losing some data. 

Here's an example:

class Main { 
   public static void main(String[] args) { 
     // create an int type variable 
     int number; 
     // convert double value to int 
     number = (int) 30.78; 
     System.out.println(number); 
   }  
}

Output -

30

In this code, the statement (int) 30.78 explicitly converts the double value 30.78 to an integer. Therefore, the double value is converted to the integer value 30. As a result, the output is 30. It's like rounding a number to the nearest whole number, even if some of the details are lost.