Please enable JavaScript to use CodeHS

Casting in Java

Learn how to convert variables of one type into another.

By Evelyn Hunter

The int and double primitive data types both store numerical data. int data is stored as integer, or whole number values, while double data is stored in a floating point number, or a number that includes a decimal. While these differences seem minute, they make an enormous difference when attempting to use both data types in mathematical equations.

For example, a programmer has a program that stores the number of people that they have coming to their party, and how much money they spend on food.

int people = 11;
int money = 200;
Plain text

These values are stored as int values because they are whole numbers - there will never be a case where the number of people at a party is 11.5!

If this programmer wanted to figure out how much money the party goers owe, the simple solution would be to divide the total amount of money spent by the number of people that showed up:

int moneyOwed = money / people;
Plain text

However, this calculation doesn’t give us the correct amount. Because int values only store integer values, the value stored in moneyOwed will be $18, instead of the correct amount $18.18.

Even if this calculation was stored in a double, such as double moneyOwed = money / people the results wouldn’t be correct either. Because the calculation money / people is an int divided by an int, the resulting value will still be 18. When the value is then stored in the double moneyOwed, it would be converted to 18.0, not the correct amount $18.18.

In order to get this program to work correctly, one of the int values will have to be cast to a double.

Casting

Casting is the process of converting the value of one type into the value of another. It can be used to convert int type integers into double values, and double values into int.

To convert an int to a double, the keyword (double) must be placed to the left of the variable that is to be converted:

double moneyOwed = (double) money / people;
Plain text

Now, the value of money is converted to a double. When divided by an int, the resulting value will be a double, and the correct value $18.18 will be stored in moneyOwed. The following example shows all of this in action:

Order of Operations

When evaluating mathematical equations, casting takes precedence over multiplication and division, but follows parentheses in the order of operations.

For example, the equation:

double moneyOwed = (double) (money / people);
Plain text

will yield a different result than the initial equation:

double moneyOwed = (double) money / people;
Plain text

This is due to the fact that the two int values will divide as ints before one of the values is casted to a double, resulting in another incorrect value.

Practice

Solve the following mathematical equation - What is the final value of myValue?

myValue = (int)((double)3/2) * (4.0);
Plain text

The answer is in the following example.

Rounding with Casting

Casting is often used when trying to round a double value to its closest integer value. When a double is cast to an int, the decimal portion of the value is removed from the value, leaving only the integer value:

int castToInt = (int) 5.46; //Result is 5
Plain text

As a result, any double value myDouble can be rounded to its nearest integer value by using the formula:

(int)(myDouble + 0.5)
Plain text

In practice, the addition of .5 makes sure that the value of myDouble will be closest to the correct integer.

Implicit Casting

Implicit casting is when Java automatically casts the value correctly without the programmer needing to do so. Java will automatically cast an int to a double if an int value is stored in a double.

An example of this was already introduced earlier!

double moneyOwed2 = money / people;
Plain text

The int resulting from money / people was automatically converted to a double when stored in the variable moneyOwed2. It’s important to note that implicit casting will not convert a double automatically into an int.

Casting with Strings

String values can also be converted to numerical values for use in mathematical equations.

The method Integer.parseInt(String value) will convert a String value to an int value and Double.parseDouble(String value) will convert a String value to a double value, assuming that the String can be converted to a valid numerical value.

int and double values can also be converted to type String. The method String.valueOf() will convert a double or an int to a String. Notice how each method belongs to the String, Integer and Double classes. Integer and Double are examples of wrapper classes, a term that is discussed in the wrapper class tutorial. Visit that page to learn more about Integer and Double.