Skip to content
Tutorials Center
My learning

Topics

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.

Java
int count = 3;
double ratio = 0.75;
boolean done = false;

Try it

Java Ranges and defaults

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.

Java Literals and promotion

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

Java
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_VALUE of float and double is the smallest positive value (1.4E-45, 4.9E-324), not the most negative one; use -MAX_VALUE for that. For the integer types and char it is the lowest value.
  • Defaults apply only to fields and array elements. Local variables must be assigned before use.
  • Default choices: int for whole numbers, double for fractional numbers, long for sizes and timestamps that may exceed 2 billion. Use byte/short mostly for large arrays and binary data.
  • Promotion. Arithmetic on byte, short and char yields int; mixing with long, float or double widens to the largest type involved.
  • Narrowing needs a cast, except for compound assignment and constant expressions that fit (byte b = 10;).
  • float and double are binary fractions, so 0.1 + 0.2 != 0.3. Use BigDecimal for money.
  • Literal suffixes: L for long, f for float, d optional for double. A leading 0 makes an octal literal, a common surprise.
  • char is a UTF-16 code unit; characters outside the BMP take two chars. 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.