Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
double price = 9.99;
int whole = (int) price;
System.out.println(whole);
}
}9
- Expecting
10: a cast tointdoes not round to the nearest number, it drops the fraction, so(int) 9.99is9. - Expecting
9.0: after the cast the value is anint, and anintprints with no decimal point.
- What does
(int) -9.99give, and why? - Why does
int whole = price;not compile without the cast?