In this tutorial, we are going to learn about various data types in java programming language.
You can also refer the below video tutorial for the same.
Hope so you are excited to learn about data types in java. So let’s start with this tutorial.
Data Types in Java
Data type specify the different value and size of value that can be stored in the variable.
Example:- If a variable has int data type, it can take integer values, in the similar way a float variable will take only float variable.
Data type specify the capacity of container or variable that what type of value can be stored in the container.
In java program we have two type of data type:-
- Primitive data type
- Non-primitive data type
1. Primitive data type:-
Every programming language has a set of primitive data types that are built into the language. Primitive data type is the pre-defined data type in java. They are defined by the language grammar. Java has 8 type of pre-defined data type. For example:- integer, character, float, byte, Boolean, short, long, double.
Boolean data type is declared with Boolean keyword which has default value false. It is used to store true/false values. Boolean data types are often used to represent switches or flag in program.
EX:- Boolean b=false;
data type is used to store a single character in the single quote.
Ex:- char c=’a’;
short data type variable requires size of 2 bytes, storing values from -32,768 to 32,767. A short data type is 2 times smaller than an integer. short can be use din the place of int data type
EX:- short n = 5000;
It can also be used in place of int data type. It can store the value ranging from -128 to 127. Byte is 4 times smaller than an integer. Byte data type is used to save space in large arrays.
EX:- byte n = 100;
int data type is used to store the value ranging from – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) . we can use the int data type to represent an unsigned 32-bit integer.
EX:- int n = 10000;
float data type is used to store the floating point value. Whenever we need a number with a decimal, such as 9.99 or 3.14515, we use float data type. It can store the value ranging from 3.4e−038 to 3.4e+038. In the float data type we should end the value with f.
EX:- float f=56.7f;
long data type is used when int is not enough to store the value. It can store the value ranging from -9223372036854775808 to 9223372036854775808. In the long data type we should end the value with L.
EX:- long n=23000000L;
double data type is used to store the decimal values just like float. The double data type can store fractional numbers from 1.7e−308 to 1.7e+038. I.
EX:- double d=2.34;
2. Non-primitive data type:-
non primitive data type is the user defined data type. They are not defined by the programming language on the other hand they are created by the programmer as per the requirement. In the java programming language we can call the non-primitive data type as object because they are created not predefined. There are three type of non-primitive data type in java:-
we will discuss them in details furthur.
If you found this post useful, don’t forget to share with your friends and if you have any query feel free to comment in the comment section.
Thank you 🙂 keep learning !