Introduction
When we talk about programming languages, we can’t skip an intangible part of how data is stored and accessed by these languages. However, sometimes the type of data stored is more important than the data itself. As it defines how we access, modify and use that data.
This is where datatypes come into picture.
A data type is essentially an attribute provided to a variable which tells how we intend to use the variable. This also defines the types of operations that can be performed on the variable, the properties they possess, and what can be deduced from the type of data stored in the variable.
On the basis of the above characteristics the datatypes in java are divided into two categories:
- Primitive Datatypes
- Non-Primitive Datatypes
We will learn more about what distinguishes each of them apart further in the blog.
Also see, Duck Number in Java and Hashcode Method in Java.
Primitive Datatypes
Java has some predefined datatypes that have their size, type of values, properties, and other characteristics defined for them by default. These are known as the Primitive datatypes. There are 8 primitive datatypes in Java:
- Byte
- Short
- Int
- Float
- Double
- Char
- Boolean
Primitive datatypes are stored in the stack when a variable is declared. Thus, when we copy a variable it creates another variable completely irrespective of the original variable.
Of the 8 primitive datatypes, they are divided into 4 aspects that define the basic input of each data type, namely: integer, float, character, boolean.
Boolean
The boolean datatype comprises of a bit that decides whether the variable stores a true or false value. This datatype can be used as flags that can be used in conditional statements.
Example
class booleanData{
public static void main(String args[]){
boolean test = true;
System.out. println(test); //Output will be true
}
}
Output
Byte
Byte is a type of integer datatype. This is an 8-bit signed two’s complement integer. Thus, is able to store integers within -128 to 127. When working with values that can not exceed this range, this can be helpful for saving memory.
Example
class byteData{
public static void main(String args[]){
byte test1, test2;
test1 = 15;
test2 = 150;
System.out. println(test1); //Prints 15
System.out. println(test2); //throws error as out of range
}
}
Output
Char
Char stores a single character in the variable. The character must always be enclosed within single quotes. We can also mention ASCII values as the character.
Example
class charData{
public static void main(String args[]){
char test1, test2;
test1 = 'a';
test2 = 65;
System.out. println(test1); //Prints a
System.out. println(test2); //Displays A
}
}
Output
Short
Short is a integer type data that stores values in the range -32,768 to 32767. By default short takes 2 bytes of space.
Example
class shortData{
public static void main(String args[]){
short test1, test2;
test1 = 5116;
test2 = 150000;
System.out. println(test1); //Prints 5116
System.out. println(test2); //throws error as out of range
}
}
Output
Int
The most used integer type datatype that stores values in the range -2147483648 to 2147483647.
Example
class intData{
public static void main(String args[]){
int test;
test = 151655;
System.out. println(test); //Prints 151655
}
}
Output
Long
It is an integer type datatype that has a very high range due to the fact that it can store 64-bit two’s complement integers i.e. from -263 to 263-1.
Example
class longData{
public static void main(String args[]){
long test;
test = 1500000001;
System.out. println(test); //Prints 1500000001
}
}
Output
Float
When it comes to storing decimal values, float is one of the most used datatype. It can be used to store fractional values from 1.2e-038 to 3.4e+038.
Example
class floatData{
public static void main(String args[]){
float test;
test = 35.45;
System.out. println(test); //Prints 35.45
}
}
Output
Double
When using fractional values that exceed the limit of float datatype, we need to use double data type. The range for this type of datatype is from 1.7e-308 to 1.7e+308.
Example
class doubleData{
public static void main(String args[]){
double test;
test = 554.554d;
System.out. println(test); //Prints 554.554
}
}
Output
Also see, Swap Function in Java