The Java Tutorial: Primitive Data Types

Java supports 8 primitive data types. A primitive data type is predefined by Java and named by a reserved keyword. This means you can use literals to initialize primitive data types without using the new keyword. In this tutorial, we'll explain Java's 8 primitive data types in detail including examples and best use cases for each.

Java Primitive Data Types

byte

The byte data type is an 8-bit signed two's complement integer. A byte has a minimum value of -128 and a maximum value of 127.

What is two's compliment?

Two's compliment is a mathematical operation on binary numbers. It's commonly used in computing to represent signed integers. You can easily reverse the sign of a two's compliments number by reversing the bits (0s become 1s and 1s become 0s) and adding 1.

When should I use byte?

The byte is 4X smaller than the int data type making it useful for saving memory.

Example

int myByte = 120;

byte[] list1 = new byte[10];
//this array takes 10 bytes of memory

int[] list2 = new int[10];
//this array takes 40 bytes of memory

In the example above, we create a variable myByte and initialize it with a value of 120. We then create two arrays list1 and list2. Notice that list1 is a byte array and will occupy 4X less space than list2.

short

The short data type is a 16-bit signed two's compliment integer. A short has a minimum value of -32,768 and a maximum value of 32,767.

When should I use short?

A short is bigger version of a byte. It's still 2X smaller than an int data type but provides a larger range of values than byte. Use short when you need to represent numbers outside the -128 to 127 range but also need to save memory.

Example

short myShort = 2000;

short[] list1 = new short[10];
//this array takes 20 bytes of memory

int[] list2 = new int[10];
//this array takes 40 bytes of memory

int

The int data type is a 32-bit signed two's compliment integer. An int has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647.

When should I use int?

The int data type is the default data type for integral values in Java. Unless you are concerned about memory, use int to represent integral values.

Example

int myInt = 5000000;

long

The long data type is a 64-bit signed two's complement integer. It's minimum value is -9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807.

When should I use long?

Use long when a wider range than int is needed.

Example

long myLong = 3000000000L;

In the example above, we assign a long literal to the variable myLong. Notice the trailing L? This identifies the literal as a long value. In Java, an integer literal is considered type long if it ends in L or l. Otherwise, it's considered an integer type int. Without the L, we would get an error since 3000000000 is outside the range of int.

float

The float data type is a single-precision 32-bit IEEE 754 floating point.

When should I use float?

Use float if you want to represent floating point numbers and saving memory is important.

Example

float myFloat = 144.5F;

Just like our long example, notice how we specify the float literal with a trailing F. Without this, Java will assume 144.5 to be of integer type double and error out.

double

The double data type is a 64-bit IEEE 754 floating point.

When should I use double?

The double data type is the default type for handling decimal values. Use double when you want to represent floating point values with a higher level of precision.

Example

float myFloat = 1.123456789F;
//1.1234568

double myDouble = 1.123456789;
//1.123456789

Remember that, for floating point integrals, the default is double. This means we don't need to include the optional D for our myDouble variable since it's already the default. If we wanted to specify this as a float, we would need to use a trailing F like we do with myFloat.

Notice how with the same value for myFloat and myDouble we get different levels of precision...

The problem with floats and doubles
Be careful using float and double. as they use IEEE-754 arithmetic. While this offers some precision, it will give you problems if you try to represent something like currency with the float type. This is because IEEE-754 integers use a binary mantissa which is outside the scope of this tutorial. For these reasons, it's better to use the BigDecimal class for representing currency values in Java.

boolean

The boolean data type is represented as one bit of information. A boolean has two possible values: either true or false.

When should I use boolean?

You can use the boolean data type for basic flags to track true/false conditions.

Example

boolean myBool = true;

char

The char data type is a single 16-bit Unicode character.

When should I use char?

You can use the char data type to store any character.

Example

char myChar = 'C';

Conclusion

In this tutorial, we've introduced Java primitive data types. We've provided examples of each along with practical explanations for when to use which and why. For integral values (byte, short, int, long) remember that the main differences are range limitations and size. For floating point values (float, double) remember the key difference is range of values and precision.

Your thoughts?