Java Reference ยท Types and Literals
Java primitive types
Reference for Java's eight primitive types: size, range, default values, literals, wrapper classes, promotion rules and when to choose each type.
Java has exactly eight primitive types. They hold plain values directly rather than references to objects, they cannot be null, and their sizes are fixed by the language, so they behave identically on every platform. Six are numeric (four integer types and two floating-point types), plus char for UTF-16 code units and boolean for truth values.
Every primitive has a wrapper class (Integer, Double, ...) for use in generics and collections, and the compiler converts between them automatically (autoboxing). Everything that is not a primitive is a reference type.
int count = 3; double ratio = 0.75; boolean done = false;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
type name = value; // e.g. long total = 0L;
| Type | Size | Range | Default | Literal example |
|---|---|---|---|---|
byte |
8 bits | -128 to 127 | 0 |
(byte) 0x7F |
short |
16 bits | -32,768 to 32,767 | 0 |
(short) 1000 |
int |
32 bits | -2^31 to 2^31-1 (about ยฑ2.1 billion) | 0 |
42, 0xFF, 0b101 |
long |
64 bits | -2^63 to 2^63-1 | 0L |
42L |
float |
32 bits, IEEE 754 | about ยฑ3.4e38, 6-7 digits | 0.0f |
1.5f |
double |
64 bits, IEEE 754 | about ยฑ1.8e308, 15-16 digits | 0.0 |
1.5, 1e-3 |
char |
16 bits, unsigned | '\u0000' to '๏ฟฟ' |
'\u0000' |
'A', '\n' |
boolean |
not specified | true, false |
false |
true |
Usage notes
MIN_VALUEoffloatanddoubleis the smallest positive value (1.4E-45, 4.9E-324), not the most negative one; use-MAX_VALUEfor that. For the integer types andcharit is the lowest value.- Defaults apply only to fields and array elements. Local variables must be assigned before use.
- Default choices:
intfor whole numbers,doublefor fractional numbers,longfor sizes and timestamps that may exceed 2 billion. Usebyte/shortmostly for large arrays and binary data. - Promotion. Arithmetic on
byte,shortandcharyieldsint; mixing withlong,floatordoublewidens to the largest type involved. - Narrowing needs a cast, except for compound assignment and constant expressions that fit (
byte b = 10;). floatanddoubleare binary fractions, so0.1 + 0.2 != 0.3. UseBigDecimalfor money.- Literal suffixes:
Lforlong,fforfloat,doptional fordouble. A leading0makes an octal literal, a common surprise. charis a UTF-16 code unit; characters outside the BMP take twochars. Use code points for those.- Primitives are not objects: no methods, no
null. Use wrapper types when a value may be absent. - Project Valhalla may change how primitives and value classes relate in future releases; the eight types themselves remain.
Related
- int, long, float and double, char, boolean, Wrapper classes
- Lessons: Primitive types, Variables, Type casting