Primitives
Primitive types are used for representing the simplest pieces of data. They're built-in to the Java language. There is a fixed set of them, and you cannot define your own. Table Java Primitive Data Types below lists them and what you use them for.
| Type name | Purpose | Size in bytes |
|---|---|---|
| byte | For representing whole numbers from −128 up to 127. | 1 |
| short | For representing whole numbers from −32,768 to 32,767. | 2 |
| int | For representing whole numbers from −231 to 231 − 1. | 4 |
| long | For representing whole numbers from −263 to 263 − 1. | 8 |
| float | For approximating real numbers ranging in size from 1.4 × 10−45 to 3.4 × 10+38. | 4 |
| double | For approximating real numbers ranging in size from 4.9 × 10−324 to 1.8 × 10+308. | 8 |
| boolean | For representing the values true and false. | Undefined |
| char | For representing characters that make up text. Java uses 16-bit Unicode, enabling a char to represent one of 65,536 characters. | 2 |
The number of bytes used to represent values of a primitive type explain the type's range. One byte has eight bits, allowing 28, or 256, possible values. For the byte type, these are divided between 128 negative values and 127 positive values, plus 0. The short type uses two bytes, which allows 216, or 65,536, possible values. These are divided between 32,768 negative values and 32,767 positive values, plus 0.
You may wonder why there are four different types for whole numbers. byte values take up one byte of memory; short values take up two bytes; int values take up four bytes; long values take up eight bytes of memory. If your program uses very large numbers of numbers—like millions or billions—you should use the smallest type your data will fit into. Normally your programs won't use so much data. The int type is a good default choice.
The char type, for characters, uses two bytes (16 bits) of memory, which defines how many characters it can represent. When Java was defined, the Unicode standard used 16 bits. It has since been extended, but Java's char type remains at 16 bits.
Floating-point types
The float and double types are for holding numbers that represent measurements and calculations with measurements. They use floating-point representation, which means that some of the bits that make up a float or double value store the value's exponent, and the remaining bits store the value's significand and sign bit.
The float type requires four bytes of memory and the double type requires twice that many, eight. double values can be larger than float values, but more importantly they can be much more precise. A float value has about 7 decimal digits of precision, and a double value has about 16 decimal digits of precision†.
Even though the float type is precise enough for most purposes, the double type is a good default choice because calculations with double values lose less precision than calculations with float values.
Because float and double have a finite number of bits, not every value in the range above is representable. See Figure Floating-point representation is finite for an illustration of this. It shows that values of floating-point types can represent only a finite number of real values. As a result, some caution in where to use floating-point types is warranted. Figure Advice on appropriate use of floating-point types. shows some advice from javapractices.com:†


Although a number like 99.99 would seem to be a natural fit for float or double, because of the decimal point, if it in fact represents $99.99, then an integer type like int or long should be used. $99.99 is the same as 9999¢. The danger of using a floating-point type is that if the number becomes large, then because floating-point is not exact, monetary value can actually be lost—or improperly gained.