Data types in Java

Data types in Java are what determines the kind of data a variable can contain (e.g integers, decimal numbers, characters, etc), and the data type is determined at the declaration. It is important to declare the correct data type since a variable only can contain the type of data it is declared for.

WHAT IS A DATA TYPE IN JAVA?

The data type tells the computer what kind of information the data consists of. There are different types of data, where every type of data describes a specific type of data. Furthermore,  for a computer, there is a big difference between storing, for example, a text string or an integer, even if the information is similar. For example, in Java, it is a difference to save the integer 10 or the text string “10”. And of course, it would not be possible to multiply the number 5 by the text string “3”. Therefore, we always need to specify what kind of information we want to save, so that the compiler can interpret the data correctly. We do this with data types.

 

A data type:

  • It is a way to tell your computer what kind of data a particular type of information is.
  • Different data types describe different data, although the information describes the same thing, for example, the integer 7 and the text string “7”.
  • Tells the computer how to interpret the data to avoid errors, such as a multiplication between a text string and an integer.

WHY DATA TYPES?

In programming, we regularly store information in the memory to be able to use it at a later time. For example, if we do a calculation, we want to be able to save the value of the calculation for a later time. In Java, data is stored in variables of a certain data type. A data type is as it sounds – a specific type of data. For example, you store integers in Integers, and text stored in Strings. Therefore, we need to know what data types are available and what information the data type can hold. For example, you cannot store an integer in a string or vice versa.

 

In Java, data is stored in variables of a certain data type.

WHAT ARE THE DIFFERENT DATA TYPES IN JAVA?

There are two types of data types in Java, the primitive data type and the non-primitive data type. The Non-primitive data type is a reference to an instance and we will review it later. However, now we will focus on the primitive data types and there are 8 different primitive data types in Java that we will have a closer look at.

 

Each data type takes up a certain amount of memory space, a certain number of bits. You may already know that a computer only handles ones and zeros (ie, bits) represented by the binary number system (ie, ones and zeros). Moreover, we will not go into detail about how the binary number system works here, but the vital thing to take into account is that large numbers need more bits to be stored, that is, more significant space in memory. Therefore, it is worth considering an extra time what type of data you use so that you do not waste memory, or accidentally try to save too much value in a data type where it does not fit because then the program will crash.

 

The most common types of data you should be familiar with are:

  • byte, short, integers, and long – Stores integers, such as 3
  • float, double – Stores decimal numbers, for example 3.14
  • boolean – Can only store true or false
  • char – Stores characters such as ‘C’
Data types in Java

Integer in Java

Let’s start with the integers, that is, 0, 1, 2, 3,…, and so on. Additionally, the negative counterpart -1, -2, -3, …, etc. There are four data types that store integers. These are byte, short, integers, and long. Below you can see how many bits each data type needs, as well as the smallest and largest value that can be saved in each data type.

 

Data type Bit size Min-value Max-value
byte 8 -128 128
short 16 -32 768 32 768
int 32 −2147 483 648 2147 483 648
long 64 ≈ −9 × 10^18 ≈ 9 × 10^18

 

The most common data type to use is int, as it covers the most conceivable integers you may need in a program (numbers between −2147 483 648 and 2147 483 648). But long is also available if you need larger numbers than that. If you know with certainty that you will only use small numbers in your program, you can advantageously use short or bytes.

 Decimal numbers (Floating-point) in Java

Furthermore, if you want to save decimal numbers instead, you can use float or double. They work similarly to integers, where the float has 32 bits and double 64 pieces. These data types are useful, for example, when you want to save an irrational number such as pi, with any number of decimal places.

 

Data type Bit size Min-value Max-value
float 32 ≈  −3.4 × 10^38 ≈  3.4 × 10^38
double 64 ≈  −1.7 × 10^308 ≈  1.7 × 10^308

Character and Text in Java

If you want to store characters, instead, you can use the data type char. Char is perfect to use if you’re going to save a single character, for example, the letter ‘a’. However, often you want to save a whole word or an entire sentence. Then it would be best if you used a collection of chars, called String. String are certainly not a primitive data type, but it is used so often that we are already introducing it. A String is useful when you want to save a whole sentence in the memory. For example, “Programming Java is so much fun!”.

True or False (LOGIC VALUES) in Java

Finally, a logical value has the data type boolean. A boolean variable can only assume the values true or false, which makes boolean variables very useful as conditions, for example, in if statements or loops, when you want to run a program until a requirement is fulfilled. You can also think that true is equal to one (1), and false is equal to zero (0).