Primitive Data Types

Man working on computer
AMV Photo/Digital Vision/Getty Images

In almost every Java program you will find primitive data types being used. They provide a way to store the simple values the program is dealing with. For example, consider a calculator program that allows the user to perform mathematical calculations. In order for the program to achieve its goal it has to be capable of storing the values the user enters. This can be done using variables. A variable is a container for a specific kind of value that is know as a data type.

Primitive Data Types

Java comes with eight primitive data types to handle simple data values. They can be split into four categories by the kind of value they hold:

  • Integers: these are positive and negative whole numbers.
  • Floating Point Numbers: any number that has a fractional part.
  • Characters: a single character.
  • Truth Values: either true or false.


Integers hold number values that cannot have a fractional part. There are four different types:

  • byte: uses one byte to store values from -128 to -127
  • short: uses two bytes to store values from -32,768 to 32,767
  • int: uses four bytes to store values from -2,147,483,648 to 2,147,483,647
  • long: uses eight bytes to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

As you can see from above the only difference between the types are the range of values they can hold. Their ranges directly correlate to the amount of space the data type needs to store its values.

In most cases when you want to represent a whole number use the int data type. Its ability to hold numbers from just under -2 billion to a little over 2 billion will be suitable for most integer values. However, if for some reason you need to write a program that uses as little memory as possible, consider the values you need to represent and see if the byte or short is a better choice.

Likewise, if you know the numbers you need to store are higher than 2 billion then use the long data type.

Floating Point Numbers

Unlike integers, floating point numbers like fractional parts. There are two different types:

  • float: uses four bytes to store values from -3.4028235E+38 to 3.4028235E+38
  • double: uses eight bytes to store values from -1.7976931348623157E+308 to 1.7976931348623157E+308

The difference between the two is simply the range of fractional numbers they can hold. Like integers the range directly correlates to the amount of space they need to store the number. Unless you have memory concerns it's best to use the double data type in your programs. It will handle fractional numbers to the precision needed in most applications. The main exception will be in financial software where rounding errors cannot be tolerated.


There is only one primitive data type that deals with individual characters – the char. The char can hold the value of one character and is based on 16-bit Unicode encoding. The character might be a letter, digit, punctuation, a symbol or a control character (e.g., a character value that represents a newline or a tab).

Truth Values

As Java programs deal in logic there needs to be a way to determine when a condition is true and when it is false.

The boolean data type can hold those two values; it can only be true or false.