Data Types in Java

Java Data Types

Well, at a very basic level, we are dealing with three basic things, which include giving input some data, processing some data, and then having some output. We can relate this thing with several examples, but let’s have an example of an ATM. What is the procedure for withdrawing money from an ATM? If I ask you this question then you’d just tell the answer so easily if you know how to withdraw cash from ATM, which I assume you know.

But the basic reason for asking this is that the procedure is simple. We first insert a card, then we enter our pin, then you enter how much cash you want to withdraw, and then if you have an appropriate balance, you get the cash(of course there are certain limits to withdraw amount, and also different ATMs may have different procedures, but the process is similar). The basic process is that you are doing some input, then that input is processed, and then you receive some output.

You can apply this basic concept to many different places. So, in a broad way, we are dealing with data, and the data can be of different types like your name is a set of characters(which we refer to here as a String, which we are going to see further), your age is a number, are you above 18 or not is true or false (here we call it as boolean). So, in short, we are going to deal with different types of data, and for this, we have different Data types in Java. So now let’s explore more about Data types in Java, and also let’s see what data is.

So, The Data types in Java is just a representation of the type of data we are using in our program. There are different types of data to deal with, but even before that, let’s consider what data is. As discussed a little bit about this earlier, Data is some kind of information.

It can be anything like your name, age, salary, email address, etc. So, in short, there are different types of data available. It can be an integer number, a fractional number, a string (again, the string is just a set of characters), true or false, etc. This way, we have different Data types in Java available, to deal with different types of data, which we are going to see now, and use later.

So, data types can be divided into two types – primitive and non-primitive data types.

Primitive data types are some basic Data types in Java-like a simple numbers, characters, etc. So, let’s have a look at the different primitive data types.

Also, there is a formula for calculating the range of a primitive datatype which is as given below-

2(n-1) to 2(n-1) – 1

Note: In the above formula, the value of n is a number of bits. For example, for byte, the value of n is 8. or for int, n is 4*8 = 32, because the int takes 4 bytes of storage and 1 byte is 8 bits.

By calculating the range of a Data types in Java in Java, I mean that we are going to calculate what numbers can behold by a variable of that particular data type, so we can use those data types accordingly.

Let me give you an example. If you want to store a number that you know never goes beyond 100, or let’s say 127, then it is better to use a byte instead of an int since this will require less memory.

Let’s have a look at the different primitive data types that we have with us –

data-types Size Range
boolean 1 bit true or false
byte 1 byte -128 to 127
short 2 bytes -32768 to 32767
int 4 bytes -2 billion to 2 billion
long 8 bytes -263to 263-1
float 4 bytes Can store fractional numbers with the efficiency of 6 – 7 digits after the decimal
double 8 bytes Can store fractional numbers with the efficiency of 12 – 15 digits after the decimal
char 2 bytes Can store one character(Unicode)

There are some non-primitive data types as well, like Strings, Arrays, etc, which we are going to see later in the course. So, for now, let’s stick with primitive data types.

In a very general way, while we do some kind of program, we generally give some input data, then the data is processed, and then we receive some result (output) from the program. Let’s consider one simpler example for the same concept just as we discussed a little bit before. Like if you want to write some program to add two numbers, the general thing would be that you would give two numbers as input for addition, then the addition is done, which can be referred to as processing of the data, and then we receive the output as the addition of two numbers. So, generally, in our programs, we are going to deal with some kind of data at some point. So, at times we are going to make use of these data types.

But now the question is that where are we going to store the data? Or what if we need to store some data? How are we going to do so? The answer to this is using variables.