Now that you learnt that a variable can store a value of a particular type, in this section, you will learn what the various types are.
You have already used the int keyword to store integers. In the following video, you will learn more about the int data type.
So you remember that for declaring or updating any new variables, first we had to tell the computer, we had to tell the program beforehand what kind of information we are going to store inside those variables. So you remember statements such as these int x equals to say three. So now here I told you that there is this keyword int which is used in Java. So this keyword int stands for what we call as integer. So whenever we are telling the program that this is the kind of information you're going to store in the variable, we also have to specify things like int or things like string. So these are what we call as the different data types in Java. So whenever I'm talking about data type, it basically signifies what kind of information I'm going to store. If my data type is an integer, then I denote it by int. Or if my data type is say, something else, a character, then I denote it by some other keyword. So now we are going to take a look at the different data types in Java. So one of the most commonly used and one of the most fundamental data types in Java is the integer data type. This is denoted by the keyword int. I say int x equals to three. And you remember why I was actually telling the computer beforehand that I'm going to use int? Well, so that the program knows how much space it has to allocate for storing this particular variable. So going on another level, we say that I say int x equals to three. So the program assigns a certain amount of computer memory for storing this variable. Specifically, Integer data type is made up of 32 bits. So you can imagine bits as being the smallest unit of storing information inside a computer. So this is a single bit. So whenever I am declaring a new int, it will consist of 32 such bit values or 32 such small boxes. The bit is the smallest unit of storing any information in the computer and a bit can only contain zero or one. So now Integer has 32 bits inside it. So even when I have to store a value, say three inside it, it will allocate 32 small boxes or 32 bits for storing this particular number. So this is how you can store any given number into the Integer data type. And till what values can I store inside int? That is, what is the range of this int? Well, you can store any number between minus two to the power 31 till two to the power 31 minus one. So how is this range coming about? Well, you can read more about this range in the link which is given on this particular particular section.
In Java, when declaring or updating variables, you must specify the type of information that will be stored inside.
Data types in Java are used to signify the kind of information being stored, such as int for integers and string for text.
The int data type is one of the most commonly used and fundamental data types in Java.
The keyword "int" is used to denote the integer data type and is followed by the variable name and value, such as int x = 3.
The computer assigns a certain amount of memory to store the variable, with the int data type consisting of 32 bits or 32 small boxes.
The smallest unit of storing information in a computer is a bit, which can only contain a value of zero or one.
The range of the int data type is from -2 to the power of 31 to 2 to the power of 31 minus one.
More information about the range of the int data type can be found in the provided link.
The integer data type can be used to store integers between - to -1.
If you wish to understand how this range is calculated, you can go here.
Since the int data type can only store numbers in a particular range, what will you do if you wish to store a larger number? Or if you want to store a really small number, would you want the computer to allocate a large chunk of memory which int occupies? Clearly, for storing larger numbers and optimal memory usage, we need different “containers” or data types.
In the following video, you will learn about two more data types which can store integers within different ranges.
Through the video above, you learned about two more data types:
long- This can store integer data within the range - to -1. (Note that “long” and “Long” are two different data types in Java. You will understand more about the difference much later in this course.)
short- This can store integer data within the range - to -1.
Till now, you have learned about data types which can only store whole numbers and integers. There certainly has to be a way to store other rational numbers.
In the following video, you will learn about data types called float and double which can store decimal values.
You learned about the following two data types:
float: This data type can store numbers with 7 digits after the decimal.
double: This data type can store numbers with 16 digits after the decimal
Note: Remember to append “f” to the number before storing it in a float variable to make sure that it is not interpreted as a double.
Till now you have only seen how numbers can be stored in variables and data types to store different kinds of numbers. In the next segment, you will learn how to store some other kind of data too like binary values and characters.