Card 04/ 09

ConceptDifficulty: Beginner1 min

The Eight Types Everything Else Is Built From

You need to store someone's age, their account balance, and whether they have confirmed their email address. Three quantities, three different sizes, and Java makes you say which before it will store any of them.

There are exactly eight types that hold a value directly. Everything else in Java is an object built on top of them.

The eight primitive types, their size, and their value before you assign one
TypeHoldsSizeDefault in a field
booleantrue or falseone bit of meaningfalse
byte−128 to 1278 bits0
shortabout ±32 thousand16 bits0
intabout ±2.1 billion32 bits0
longabout ±9.2 quintillion64 bits0L
floatdecimals, roughly 7 digits32 bits0.0f
doubledecimals, roughly 15 digits64 bits0.0
charone character16 bits'\u0000'

Two of those columns matter more than the others. Size decides what fits: an int cannot hold the number of milliseconds since 1970, which is why timestamps are long. Default decides what a field holds before anyone assigns it — a field is always initialised, always to the value above.

So int for whole numbers, long when they get large, double for decimals and boolean for a yes or no covers almost everything you will write. The other four are for when you know exactly why you want them.