Data Types in Java

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:-

  1. Primitive data type
  2. 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.

1. Boolean:-

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;

2. char:-

data type is used to store a single character in the single quote.

Ex:-   char c=ā€™aā€™;

3. short:-  

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;

4. byte:- 

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;

5. int:- 

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;

6. float:- 

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;

7. long:-

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;

8. double:-

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;

To store the floating point value we should use float or double?
Float data type indicate that only six or seven decimal digits can have after the decimal point while double data type indicate that 15 digits can have after the decimal digits, so float data type is safer to use as compare to double data type.

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:-

  1. Classes
  2. Interfaces
  3. Arrays

we will discuss them in details furthur.

What are the Wrapper classes available for the primitive data type?
boolean:- java.lang.Boolean
byte:- java.lang.Byte;
char:- java.lang.Character;
int:- java.lang.Integer;
float:- java.lang.Float;
long:- java.lang.Long;
short:- java.lang.Short;

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 !

Leave a Comment

Your email address will not be published. Required fields are marked *