Java — Variable Declaration & Literal Errors
In Java, strict type checking and literal rules catch many common mistakes at compile time before your code ever runs. Below are the 7 most frequent variable declaration and literal errors in Java, why the compiler rejects them, and how to fix each one.
1. Integer literal overflow
If an int literal is larger than the type's range (-2,147,483,648 to 2,147,483,647), Java throws a compile error.
int x = 2147483648;
// error: integer number too large
int y = -2147483649;
// error: integer number too large
Fix: Use long with an L suffix when storing numbers outside the 32-bit int range:
long x = 2147483648L; // Valid
2. int ↔ boolean are not interchangeable
Unlike C/C++, Java treats int and boolean as completely separate types — neither converts to the other, even implicitly.
int x = true;
// error: incompatible types: boolean cannot be converted to int
boolean flag = 10;
// error: incompatible types: int cannot be converted to boolean
Note: if (x) and while (x) where x is an int are valid in C and C++ (any non-zero value is "truthy"), but this is a compile error in Java — the condition must be a genuine boolean.
3. Lossy numeric conversions
long x = 10.88;
// error: incompatible types: possible lossy conversion from double to long
int z = 10l; // 10L is a long literal
// error: incompatible types: possible lossy conversion from long to int
4. Boolean literals are case-sensitive
true / false are keywords in Java — True (capital T) is not recognized and is treated as an undeclared identifier.
boolean x = True;
// error: cannot find symbol
// symbol: variable True
5. char size in Java
char is 2 bytes in Java because Java is Unicode-based, whereas C and C++ use char as a 1-byte, ASCII-based type.
System.out.println(Character.BYTES); // 2 bytes
6. float needs a decimal point and an f / F suffix
A literal with a decimal point (e.g. 233.0) is treated as double by default, so assigning it directly to a float is a lossy conversion.
float a = 233.0;
// error: incompatible types: possible lossy conversion from double to float
float b = 33.0333;
// error: incompatible types: possible lossy conversion from double to float
Fix: suffix the literal with f or F:
float a = 233.0f;
float b = 33.0333f;
For double, no suffix is required (22.333333D works, but the D is optional since double is already the default for decimal literals):
double d = 22.333333;
7. double only applies to decimal literals — not octal or hex
double (with a decimal point) is valid only for base-10 numbers. It does not work with octal or hexadecimal literals.
double x = 0234.34555;
System.out.println(x); // Output: 234.34555
Leading zero + a decimal point → the value is read as plain decimal, not octal — the leading zero is simply ignored.
Quick Cheat Sheet
| Literal Type | Default Type | Required Suffix | Example |
|---|---|---|---|
| Whole number | int |
L / l (for long) |
long id = 9876543210L; |
| Decimal number | double |
f / F (for float) |
float rate = 12.5f; |
| Boolean | boolean |
None (strictly lowercase) | boolean active = true; |
| Character | char (2 bytes) |
Single quotes | char grade = 'A'; |
1 Comments