Java variables, types, and casting
Declare typed variables and understand integer division, overflow, and conversions.
class Main {
public static void main(String[] args) {
int count = 7;
long big = 3_000_000_000L;
double ratio = count / 2;
double exact = count / 2.0;
int overflow = Integer.MAX_VALUE + 1;
char letter = 'A';
letter += 2;
boolean flag = count > 5 && big > count;
System.out.println(ratio + " " + exact);
System.out.println(overflow);
System.out.println(letter + " " + (int) letter);
System.out.println(flag);
System.out.println((int) 3.99 + " " + Math.round(3.99));
}
}3.0 3.5 -2147483648 C 67 true 3 4
Explanation
Java is statically typed: every variable has a type that is fixed when it is declared. The primitive types include int (32-bit), long (64-bit), double, boolean, and char. A larger literal needs the right suffix, such as the L in 3_000_000_000L, and underscores can make long numbers readable.
Arithmetic follows the types of the operands. Dividing two ints performs integer division and discards the fraction, even if the result is stored in a double. Making one operand a double gives a real division. Integer arithmetic does not report overflow: adding 1 to Integer.MAX_VALUE wraps around to the smallest int. A narrowing conversion needs an explicit cast and truncates toward zero, while Math.round rounds to the nearest whole number.
Explore the dry run
class Main { public static void main(String[] args) { int count = 7; long big = 3_000_000_000L; double ratio = count / 2; double exact = count / 2.0; int overflow = Integer.MAX_VALUE + 1; char letter = 'A'; letter += 2; boolean flag = count > 5 && big > count; System.out.println(ratio + " " + exact); System.out.println(overflow); System.out.println(letter + " " + (int) letter); System.out.println(flag); System.out.println((int) 3.99 + " " + Math.round(3.99)); }}Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| 7 / 2 is integer division, which gives 3. The 3 is then widened to 3.0. | ratio = 3.0 | — | |
| One double operand makes it a real division. | exact = 3.5 | — | |
| 2147483647 + 1 wraps around silently. | overflow = -2147483648 | — | |
| A char is a small number: 'A' is 65, so adding 2 gives 'C'. | letter = 'C' | — | |
| Printing the two divisions. | letter = 'C' | 3.0 3.5 | |
| The wrapped value is printed. | letter = 'C' | 3.0 3.5 -2147483648 | |
| The char and its numeric code. | letter = 'C' | 3.0 3.5 -2147483648 C 67 | |
| 7 > 5 and 3000000000 > 7 are both true. | letter = 'C' | 3.0 3.5 -2147483648 C 67 true | |
| The cast truncates to 3; Math.round gives 4. | letter = 'C' | 3.0 3.5 -2147483648 C 67 true 3 4 |
Cost and common mistakes
Each statement is O(1).
- Integer overflow does not throw. Use Math.addExact when an overflow should be an error.
- A cast to a smaller type can lose information. (int) 3.99 is 3, not 4.
- Comparing doubles with == is unreliable because of rounding. Compare with a small tolerance.
Try it yourself
What does 10 / 4 print, and what does 10 / 4.0 print?
Reveal answer and reasoning
10 / 4 prints 2 (integer division) and 10 / 4.0 prints 2.5.