-
Notifications
You must be signed in to change notification settings - Fork 0
Data Types
In Java, data types can be divided in to three categories:
- Primitives
- Arrays
- Objects
There are eight primitive data types in Java.
| Primitive | Size | Description |
|---|---|---|
| byte | 1 byte | A whole number in the range -128 to 127 |
| short | 2 bytes | A whole number in the range -32,768 to 32,767 |
| int | 4 bytes | A whole number in the range -2,147,483,648 to 2,147,483,647 |
| long | 8 bytes | A whole number in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| boolean | 1 byte | A boolean representing true or false
|
| char | 2 bytes | A character |
| float | 4 bytes | A single-precision (IEEE 754) floating point number |
| double | 8 bytes | A double-precision (IEEE 754) floating point number |
An array is a special type of object. It is a container for fixed number of elements of a specific type.
- An array can be of any primitive type or Object type
- The type of the array elements cannot be changed
- The length of an array cannot be changed
- The length of an array is a positive integer (in theory up to 2.1 billion, in practice limited to available JVM memory)
- An array can have up to 255 dimensions (although more than a single dimension is uncommon!)
Note that for streams, only int, long and double primitives are supported
| Description | Code |
|---|---|
| Computes a hashcode for an array | int Arrays.hashCode(*array) |
| Computes equality of two arrays | boolean Arrays.equals(*array1, *array2) |
| Returns a string representation of an array | String Arrays.toString(*array) |
| Compares two arrays | int Arrays.compare(*array1, *array2) |
| Sort an array | void Arrays.parallelSort(*array) |
| Returns a stream over an Object array | <E> Stream<E> Arrays.stream(E[] array) |
| Returns a stream over an int array | IntStream Arrays.stream(int[] array) |
| Returns a stream over an long array | LongStream Arrays.stream(long[] array) |
| Returns a stream over an double array | DoubleStream Arrays.stream(double[] array) |
*array = any type of array, primitive or Object.
An object is a data structure with a fixed set of fields. The object definition can be found in its Class. There are three types of Object:
| Type | Description |
|---|---|
| Object | General purpose super type of all Objects |
| Enum | Special subtype of object to represent immutable enumerations |
| Record | Special subtype of object to represent immutable records |
How can you calculate the size of a Java object?
Any Java book will include the relative sizes of each of the basic types in one of its early chapters.
/** The number of bytes used to represent a byte. */
SIZE_OF_BYTE = 1;
/** The number of bytes used to represent a short. */
SIZE_OF_SHORT = 2;
/** The number of bytes used to represent an integer. */
SIZE_OF_INT = 4;
/** The number of bytes used to represent a long. */
SIZE_OF_LONG = 8;
/** The number of bytes used to represent a boolean. */
SIZE_OF_BOOLEAN = 1;
/** The number of bytes used to represent a char. */
SIZE_OF_CHAR = 2;
/** The number of bytes used to represent a float. */
SIZE_OF_FLOAT = 4;
/** The number of bytes used to represent a double. */
SIZE_OF_DOUBLE = 8;Knowing the size of primitives is essential to understanding the size of any instance of an object. There are also two other key sizes associated with objects, the base size of any object (including arrays), and the size of a null pointer (field or object array element):
/** The number of bytes used to represent a pointer field (unassigned/null). */
SIZE_OF_NULL_POINTER = 4;
/** The number of bytes used to represent an object (with no fields). */
SIZE_OF_OBJECT_INSTANCE = 8;Object Rules:
- The base size of any object is 8 bytes.
- The total size of any object is always a multiple of 8 bytes (rounded up).
Field Rules:
- Each primitive field occupies the size of the primitive value it contains (1, 2, 4 or 8 bytes).
- Each object field occupies 4 bytes (if the field value is not null, it should be calculated as a separate object).
Array Rules:
- A primitive array occupies the base of 8 bytes, + 4 bytes for the length of the array, + the length multiplied by the size of the primitive.
- An object array occupies the base of 8 bytes, + 4 bytes for the length of the array, + the length multiplied by 4 bytes (the size of the null pointer).
It is helpful to note that from 32-bit to 64-bit, object references (for fields and arrays) double from 4 to 8 bytes, and the base object size doubles from 8 to 16 bytes. What is interesting however is how 64-bit compressed pointers reduce object references back to 4 bytes. They also reduce the base object size from 16 to 12 bytes. Overall compressed pointers provide a very significant reduction in memory usage compared to full 64-bit pointers.
| Object | 32-bit | 64-bit** | 64-bit |
|---|---|---|---|
| Object Field | 4 bytes | 4 bytes | 8 bytes |
| Object Array Element | 4 bytes | 4 bytes | 8 bytes |
| Object | 8 bytes | 16 bytes | 16 bytes |
| Boolean | 16 bytes | 16 bytes | 24 bytes |
| Byte | 16 bytes | 16 bytes | 24 bytes |
| Short | 16 bytes | 16 bytes | 24 bytes |
| Integer | 16 bytes | 16 bytes | 24 bytes |
| Long | 16 bytes | 24 bytes | 24 bytes |
| Float | 16 bytes | 16 bytes | 24 bytes |
| Double | 16 bytes | 24 bytes | 24 bytes |
| Currency | 16 bytes | ||
| Date | > 24 bytes | ||
| BigInteger | > 56 bytes | ||
| BigDecimal | > 144 bytes | ||
| Thread | > 176 bytes | ||
| GregorianCalendar | > 460 bytes | ||
| Object | 32-bit | 64-bit** | 64-bit |
|---|---|---|---|
| ArrayList | 40(+4) bytes | 40(+4) bytes | 64 (+8) bytes |
| LinkedList | 48 (+24) bytes | 48 (+24) bytes | 80(+40) bytes |
| HashMap | 120 (+32) bytes | ||
| HashSet | 136 (+30) bytes | ||
| LinkedHashMap | 160 (+32) bytes | ||
| LinkedHashSet | 176 (+30) bytes | ||
| TreeMap | 40 (+32) bytes | ||
| TreeSet | 64 (+32) bytes | ||
| ConcurrentHashMap | 1272 (+33) bytes | ||
| ConcurrentSkipListMap | 104 (+36) bytes | ||
| CopyOnWriteArrayList | 72 (+4) bytes | ||
| CopyOnWriteArraySet | 88 (+4) bytes | ||
| ArrayBlockingQueue | 144 (+4) bytes | ||
| LinkedBlockingQueue | 200 (+216) bytes | ||