Java type casting
›Short versionOpen if you only have a minute.
- Widening, such as int to double, is automatic. The value fits, so no cast is written.
- Narrowing, such as double to int, needs a cast. The cast tells the compiler you accept the loss.
- A cast to an integer type truncates toward zero. (int) 7.9 is 7, not 8.
- (double) total / count divides in floating point. (double) (total / count) divides as ints first, and the fraction is already gone.
- A cast of a value that does not fit wraps. (byte) 200 is -56. parseInt of text that is not an integer throws.
A conversion copies a value into another type. is the direction Java will do for you. is the direction you have to ask for, with a , because the result can be a different number from the one you started with.
int myInt = 9;
double myDouble = myInt;
double price = 9.78;
int truncated = (int) price;myDouble is 9.0. truncated is 9. The variable price is still 9.78. The cast built a new value for that one expression.
What a narrowing cast actually drops
From a floating-point type to an integer type, the loss is . The fraction is cut toward zero. It is not rounded.
double price = 19.99;
int dropped = (int) price;
int nearest = (int) Math.round(price);
System.out.println(dropped);
System.out.println(nearest);The first print is 19. The second is 20, because Math.round picked the nearest integer and the cast only converted that long down to an int. Math.floor and Math.ceil are the other deliberate choices. A cast is none of them.
Between integer types, a value that does not fit is . The low bits are kept and read in the narrower type.
int largeNumber = 200;
byte smallByte = (byte) largeNumber;
System.out.println(smallByte);Widening the other way is the quiet success. byte to int, int to long, and int to double need no cast. char letter = 'A'; then int code = letter; stores 65, the code unit, not the digit 1.
Where you put the cast changes the division
Two ints divide as ints. The fraction is discarded before a wider variable ever sees it. The cast has to cover an operand, not the finished quotient.
int total = 47;
int count = 5;
int avg1 = total / count;
double avg2 = (double) total / count;
double avg3 = (double) (total / count);
System.out.println(avg1);
System.out.println(avg2);
System.out.println(avg3);The prints are 9, then 9.4, then 9.0. avg3 looks like a careful cast and still loses the fraction, because total / count runs first. That order is the whole decision. Integer division itself is on Java operators.
Text is a different conversion. A method reads digits. A cast will not.
String str = "123";
int num = Integer.parseInt(str);
String dStr = "19.99";
double d = Double.parseDouble(dStr);
int back = 123;
String text = String.valueOf(back);Integer.parseInt("abc") compiles and throws NumberFormatException when it runs. Integer.parseInt("19.99") throws as well. The decimal point is legal for Double.parseDouble and illegal for an int. String.valueOf, or Integer.toString, is the other direction, and it cannot fail for lack of digits.
double d = 7.9; int i = (int) d; What is i? What would Math.round have produced instead?
i is 7. The cast cuts toward zero and drops .9. Math.round(7.9) returns the long 8, because 7.9 is nearer to 8 than to 7. Rounding and truncating are different operations. The cast only truncates.