We have seen a variable is used to hold a value. Be it a number or a name or even a sentence. But how will java come to know, if the value is a number or a name?
And thus Data Types comes into picture.
Data Types tells the variable that what kind of values it can contain.
In the above statement
'x' is a variable
and
'int' is the Data Type. Which is saying 'x' can only hold numbers. If you want to assign anything other than a number, it will end up with an error.
Java provides us with a set of Data Types used to hold all type of values.
There are two types of Data Types in Java :
byte data type is of 8-bit.
It can hold values from -128 to 127.
short data type is of 16-bit.
It can hold values from -32,768 to 32,767.
int data type is of 32-bit.
It can hold values from -2,147,483,648 to 2,147,483,647.
long data type is of 64-bit.
It can hold values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
float data type is of 32-bit.
double data type is of 64-bit.
boolean data type only contains true or false
If we do not specify anything the default value is false.
char data type is of 16-bit.
Char data type can hold a letter/character.
Although we have seen how to hold a letter (Say the letter 'a') using 'char' data type. But we have not yet seen, how can we store a name? No worries, we will be looking at it next.