Java Primitive Types Reference Chart

There are 10 types of people in the world: those who understand binary numbers and those who don't.

Data in computer programs are stored in variables.  A variable's name corresponds to some location in computer memory where the value of that variable is stored.  Suppose you could “see” the binary value stored in some memory location.  For example say the variable “foo” corresponds to the memory address 1280, and when you “looked” at that part of memory you saw the following:

0 1 0 0 0 1 1 0  1 1 0 1 1 1 1 0  ...

There is no way for you (or a computer) to know what it means.  It could be an integer, a string of characters, a floating point number, or something else (part of a GIF graphic, for example).  This is one reason why all Java variables have a declared type.  The type of a variable tells the computer how to interpret the data.

Java provides many different built-in (or primitive) types you can use for your variables.  As a programmer you must be able to pick an appropriate type for the kind of data you expect to store in any variable.  In general prefer integral (“fixed point”) types to “floating point” types, and prefer smaller types to larger ones.  You do need to ensure you chose a type that is large enough to hold the full range of values expected (which should be documented as part of the requirements), and will be accurate enough.

Float and double are useful with scientific and engineering computation, and with statistics.  Outside of these areas they should mostly be avoided.  Float is preferred to double when memory is short, or when you need a very large number of values and can tolerate the lower precision.  The performance advantage of floats to doubles is modest on modern hardware, and should not be a consideration.

For most applications you can chose int, even when a smaller integral type has an appropriate range.  As with floats, the smaller types are useful when you need a lot of them and/or when memory use must be minimized.  Note most financial calculations can be calculated in fixed point, that is using integers (calculate in pennies or even thousandths of a cent).

Java Primitive Types Reference Chart
Type Size Format Range Literals Comments
boolean ≤1 byte   —   —  false, true Use for on/off (checkbox) settings
byte 1 byte signed integer ±127 −3, 0, 10, 124 Use to store lots of small numbers (e.g., temperatures, years of service, test scores)
short 2 bytes signed integer ±32,767 −130, −3, 0, 10, 124, 25000 Use to store lots of small numbers too big for a byte
char 2 bytes unsigned integer 0 to +65,535 0, 124, 44000, 'A', '\u00A9' Use for Unicode character codes, including '\\', '\n', '\r', '\t', '\'', and '\udddd'
int 4 bytes signed integer ±2,147,483,647 −10, 0, 12, 120000, 1_200_000, 017 (octal), 0x2F (hex), 0b100 (binary) Use to store or compute with integers; the most commonly used type
long 8 bytes signed integer ±9,223,372,036,854,775,807 −10L, −3L, 0L, 10L, 250_000_000L Use to store compute with integers too large for an int
float 4 bytes floating point ±1038 −6F, 0.F, 0.0F, 3.14F, 6F, 6.02E23F, 1E2F, −1E2F, 1E−2F Use to store or compute with rational numbers (numbers with decimal points or in scientific notation); has 6 decimal digits of accuracy.
double 8 bytes floating point ±10308 −10.0, 0.0, 0.0D, 3.14, 12., 120000D Similar to float but with greater range and 14 decimal digits of accuracy.

Additional Notes

three bit two's complment number chart
Decimal Binary    Decimal Binary
+30 1 1    −11 1 1
+20 1 0    −21 1 0
+10 0 1    −31 0 1
0 0 0 0    −41 0 0

3 bit two's complement signed integers