Learn how to convert variables of one type into another.
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.
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:
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 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:
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:
When evaluating mathematical equations, casting takes precedence over multiplication and division, but follows parentheses in the order of operations.
For example, the equation:
will yield a different result than the initial equation:
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.
Solve the following mathematical equation - What is the final value of myValue
?
The answer is in the following example.
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:
As a result, any double value myDouble
can be rounded to its nearest integer value by using the formula:
In practice, the addition of .5 makes sure that the value of myDouble
will be closest to the correct integer.
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!
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
.
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
.