In the previous chapter, we learnt about Java variables. In this chapter, let us dive deeper into various Data types available in Java programming language.
Java supports 2 types of Data types:
There are 8 primitive data types in Java. They are:
Let us explore them in detail...
i. byte
This data type is used to store whole numbers between -128 to 127. The size of this data type is 1 byte (or 8 bits).
E.g.
Program:
byte smallNum = -100;
System.out.println(smallNum);
Output:
-100
ii. short
This data type is used to store whole numbers between -32,768 to 32,767. This data type is the larger version of “byte” data type. The size of this data type is 2 byte (or 16 bits).
E.g.
Program:
short smallNum = 3300;
System.out.println(smallNum);
Output:
3300
iii. int
This data type is used to store whole numbers between 2,147,483,648 to 2,147,483,647. The size of this data types is 4 byte (i.e. 32 bits).
E.g.
Program:
int num = 330000;
System.out.println(num);
Output:
330000
iv. long
This data type stores whole numbers between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This data type is the larger version of “int” data type. The size of this data type is 8 bytes (i.e. 64 bits). While declaring it in Java, we need to explicitly add “L” at the end to treat the literal as “long”.
E.g.
Program:
long num = 3147483647L; // Add 'L' to treat the literal as a long System.out.println(num);
Output:
3147483647
v. float
This data type is used to store numbers with decimals. The size of this data type is 4 byte (i.e. 32 bits). While declaring this variable, remember to append the value with “f”.
E.g.
Program:
float decimalNum = 4.56f;
System.out.println(decimalNum);
Output:
4.56
vi. double
This data type is also used to store numbers with decimals. It is the larger version of “float” data type. The size of this data type is 8 bytes (i.e. 64 bits). Unlike the “f” character we used to append in float data type, we don’t need to mandatorily append any suffix for this data type. However, still the character “d” can be used to append for the same purpose, though it is redundant and not followed much in practice.
E.g.
Program:
double decimalNum = 433434343343243243242342342343223432433434343434.5623423423434;
System.out.println(decimalNum);
Output:
4.334343433432433E47
vii. boolean
This data type is used to store 2 possible values i.e. “true” or “false”. The size of this data type is 1 bit.
E.g.
Program:
boolean isAvailable = true;
System.out.println(isAvailable);
Output:
true
viii. char
This data type is used to store single characters. The size of this data type is 2 bytes (i.e. 16 bits).
E.g.
Program:
char letter = 'M';
System.out.println(letter);
Output:
M
If you find difficultly in remembering these data types, then consider checking out this article – Easy way to remember Java data types.
Java also has a set of other data types which are Objects. These are referred to as Non-primitive datatypes or Reference datatypes. Some of the examples include String, Integer, Arrays, etc. We shall explore these data types in detail in further tutorials.
I hope this tutorial gave you a fair understanding of Java Data types. See you soon in the next tutorial.
Fully customizable CRM Software for Freelancers and Small Businesses