Type Conversion and Casting
Type Conversion
Sometimes you need to convert between different primitive types. For example, you may wish to store integers in floating-point types so they can handle decimal values.
In many cases, you can assign the values of one variable type to another variable type:
int i = 5;
double d = i;
System.out.println(d);
This will print out:
5.0
This is because d
will be assigned the 5 and will store it as a decimal value.
Java will automatically convert types as long as the types are compatible and the following condition is met:
- The type being assigned (on the left) is larger than the type being copied (on the right). For example, a
double
andlong
are larger than anint
. This is why thed
can accept the value ofi
above without issue.
Casting
What if you want to assign a double
or long
to an int
? Java will not automatically convert it, since an int
cannot hold all their possible values. To convert a larger type to a smaller type, you need to explicitly "cast" it. This is a way of telling Java "I understand its going to a smaller type, don't worry". The cast has this general syntax:
newValue = (newValueType) originalValue;
To cast a number to an int
you would use (int)
. In the code below two numbers are cast to ints:
double di = 3.0;
long lo = 10L;
int i1 = (int) di;
int i2 = (int) lo;
i1
and i2
will now equal 3
and 10
respectively. This cast
was simple since the 'larger' numbers could actually fit in the int
, so you just had to tell Java not to worry. But what happens when they don't fit? In such a case, Java will automatically convert the larger number into smaller form:
- When a real number is converted to an integer, the decimal values will be cut off. This can be useful when you no longer need the decimal values.
double d = 3.6;
int i = (int) d;
System.out.println(i);
This will print 3.
- When a large value is converted to a smaller one, Java will reduce the number modulo the smaller value's full range. This isn't usually useful and can cause some confusing bugs.
long doubleMaxInt = 2147483648L * 2;
int num = (int) doubleMaxInt;
System.out.println("long is: " + doubleMaxInt);
System.out.println("int is: "+ num);
This will print:
long is: 4294967296 int is: 0
Java may do this without any explicit cast, as in the following example:
int max = 2147483647;
int sum = max + 1;
System.out.println(sum);
This will print -2147483648
, which may not be the result you were expecting. To avoid these errors, use longs when dealing with such long numbers.
Note:
Using a long
above for the variable still wouldn't have helped:
int max = 2147483647;
long sum = max + 1;
System.out.println(sum);
The output would be the same as before. This is because when Java added max
to 1
, it was dealing with integers, which overflowed. That result was then assigned to a long, but it was too late. To avoid that issue, make sure to explicitly cast integers to longs:
int max = 2147483647;
long sum = (long) max + 1;
System.out.println(sum);
This will print the expected result of 2147483648
.
Challenge
A rocket ship traveled for Y years at M miles per hour. How many miles did the rocket ship travel? Please print the answer.
You will be given two numbers as input, Y and M (as a
and b
respectively).
Note:
There are 365 days in every year and 24 hours in every day.
Y can be from 0 to 1000.
M can be from 0 to 670 million.
(You do not need to worry about relativity in this problem!)
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
Here's the beginning of some code:
int flint = 5;
long kong = 1200L;
double trouble = 12345.67 ;
float boat = 12.05f;
Given the above code, which of the following assignment statements will work?
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Lukas Dancak
Nov 9, 11:51 AMeasy :) System.out.print((long)a*(long)b*(long)365*(long)24);
thales
Jul 10, 6:28 AMlast long(s) no nescesarry.
long afstand= (long)a *(long)b*8760;
System.out.println(afstand);