Topic Overview

Greetings! Are you ready to explore additional realms of Java? Our current mission delves into the core language concept: Primitive Data Type Conversion. Often, we find ourselves transforming one data type to another, akin to adjusting a spaceship's asteroid-floating-point measurements to fit the integer-based radar. We will study automatic and explicit conversions and highlight potential pitfalls. Let's ignite our knowledge thrusters!

Automatic (Implicit) Conversions

Data type conversion or type casting involves transforming values between different data types. Imagine a transformation from a human (a representation of an integer type) to a spaceship (symbolizing a double type): the integer fits into a double with ease (widening). However, fitting a double into an int (a process of narrowing), necessitates explicit conversion and may result in data loss.

When types are compatible and the target type is larger (similar to a spaceship's spacious cockpit), Java performs the automatic conversion. Here's an example of automatic (implicit) conversion from an int to a double:

int i = 10;  // an integer 
double d = i;  // automatic conversion to double

System.out.println("The value of d: " + d);   // Output: The value of d: 10.0
Manual (Explicit) Conversions

Just like squeezing a spaceship into a human, there are times when a larger value must fit into a smaller type. This situation necessitates explicit casting. Take a look at how we cast a double to an int:

double d = 10.25; // a double number
int i = (int) d;  // casting the double to int

System.out.println("The value of i: " + i);  // Output: The value of i: 10

Notice how the decimal part of 10.25 is truncated during the casting process, leaving just 10 as a result.

Converting to and from Strings

A common type of conversion in Java is converting to and from String values. We often need to convert numbers to strings for output or parse strings as numbers for computation.

int ten = 10; // an integer with value 10
String tenString = Integer.toString(ten); // A string with value "10"
System.out.println("The value of tenString: " + tenString);    // Output: The value of tenString: 10

String twentyFiveString = "25";
int twentyFive = Integer.parseInt(twentyFiveString);
System.out.println("The value of twentyFive: " + twentyFive);    // Output: The value of twentyFive: 25

String invalidNumber = "25abc";
int number = Integer.parseInt(invalidNumber); // Oops! This will throw an error, "25abc" is not a number!

In the conversion to String, we use the Integer.toString(n) method for int type. Other types have similar methods - Float.toString(f), Double.toString(d), etc. For the conversion from String to a number, we use the Integer.parseInt(s) method. Similarly, Float.parseFloat(s) and Double.parseDouble(s) are used to obtain float and double numbers from the string, respectively.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal