← Java from Scratch Lesson 6 of 88 · 10 XP

Data types

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

8 primitive types — they hold the value itself:

type size range / example
byte 1 byte −128 … 127
short 2 −32,768 … 32,767
int 4 ±2.1 billion — the default type for integers
long 8 ±9.2·10¹⁸, the literal 5_000_000_000L
float 4 ~7 digits, the literal 3.14f
double 8 ~15 digits — the default type for fractional numbers
boolean — true / false
char 2 a UTF-16 character: 'A'

Everything else (String, arrays, your own classes) is a reference type: the variable holds a reference to an object.

Unlike in C, the sizes are the same on any machine — they're set by the JVM specification.

Overflow isn't caught: Integer.MAX_VALUE + 1 gives -2147483648. Big numbers go in a long (or java.math.BigInteger).

Task. Read n (up to 20) and print n! (the factorial). For n = 20 it's 2432902008176640000 — it doesn't fit into an int, you need a long.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.