In the previous tutorial, I have explained hello world program in detail. The next part is to know what are the different data types in Java. In this tutorial, we will cover the following topics:
Post Content
The basic data types in Java include:
Let’s discuss datatypes one by one in detail with its variable size and range…
It represents integral arithmetic values. According to the size of data types, it is further classified into:
Keyword | Memory Size | Default | The range of Datatypes Value |
---|---|---|---|
byte | 1 byte (8 bits) | 0 | -128….127 |
short | 2 bytes (16 bits) | 0 | -32,768…..32,767 |
int | 4 bytes (32 bits) | 0 | Up to 10 digits on the positive and negative end |
long | 8 bytes (64 bits) | 0 | Up to 19 digits on the positive and negative end |
It is used to represent a single character. 16 bit Unicode character representations are used for character data. The size of a char is 2 bytes (16 bits) and the default value is ‘\u0000’
.
They are usually used to represent decimal values. They are of two types:
Keyword | Memory Size | Default | The range of Datatypes Value |
---|---|---|---|
float | 4 bytes (32 bits) | 0.0 | Up to 10 digits on the positive and negative end |
double | 8 bytes (64 bits) | 0.0 | Up to 19 digits on the positive and negative end |
In C/C++, we use
float f = 1.5;
However, this is incorrect in Java.
The correct representation requires and additional suffix ‘f’ after the number.
For e.g.:
float f = 1.5f;
Other ways to assign a value of 1.5 to a variable include:
float f = (float)1.5;
Or
double f=1.5;
Boolean variables can have only two values, true and false. No other values are permitted. It cannot be cast to other types as well (neither implicitly nor explicitly). The size of Boolean is one bit and the default value is false.
Most of the new programmers have a question…
The string is far different from all primitive datatypes. One of the major difference is its size. We can simply specify the size of the string. It can be of any length. And we can allocate as much memory space as we can for the string.
The string is a major entity and used in many programs for manipulating data. So instead of keeping it as a Data type, it is a class in Java. And there are so many methods to manipulate and to modify the String class object.
So there are two main reasons to make string as a class instead of data types in Java…
This is what makes Java beautiful.
So the data can be the primitive data type or the object of the class.
So we are almost ready to use them in our program. We will use these variables and basic data types throughout our Java programming tutorial.
All we need now is a set of rules to be followed while writing these programs. The most basic and important rules or guidelines are given below:
There are various other rules and conventions that are often followed related to the readability of program, use of comments to define the purpose of each class, nesting and its limits, consistency, naming, modifiers etc. However, we shall learn these rules as we go on programming further. It is necessary to always remember the above mentioned seven rules.
Do you have any question related to basic data types in Java or guidelines to be followed? Let me know your opinion in the comment section.