Previous | Home | Next |
Java is a strongly typed language so there must be a data type associated with every variable declared. Mainly there are eight primitive data types which are used in the java. Out of the eight data types used 4 are integer data types, 2 are float data types, one is character type and last but not the least is Boolean type data type. Now we shall see all these data types in detail :
Integer data types are used for integral numbers i.e. the numbers that do not have fractional parts. For Integer types there are four kind of data types are used in java, each having different storage capacity and range and based upon that they can be categorized as under :
TYPE | STORAGE NEED (BYTES) | RANGE |
byte | 1 | -128 to 127 |
short | 2 | -32768 to 32767 |
int | 4 | -2147,483,648 to 2147,483,647 |
long | 8 | -9,223,372,036,854,775,808 to -9,223,372,036,854,775,807 |
In java the ranges of the integer data types does not depend upon the machine on which you are executing your java code, their ranges are fixed, but unlike, in C++ or C the values of the integer data types changes if there is a change in the platform. Following show the way how we can write various integer literals in java :
-
int literals are written in decimal notation as usual like 123 or -23445.
-
long literals are written in decimal notation but postfixed by an L, eg 234556L or -56467776768L.
Note: Java does not have unsigned types.
Floating-point types are the numbers that are having fractional part. Based upon the storage capacity and range they can be categorised into the following types :
TYPE | STORAGE NEED (BYTES) | RANGE |
float | 4 | approx ±3.40282237E+38 F |
double | 8 | approx ±1.79769312486231570E+308 D |
Here, double corresponds to the fact that, it has double precision than that of float types, that's why double are also referred as double precision numbers. For floating-point types, we mostly use the double types, because of the fact that float has a less precision so it may be incompatible for certain type of calculations that require more precision.
For writing float literals we just postfixes F after the number as 4343.45455F, and a number not followed by F, will be simply termed as double by default.
Java Character type are the alphabets which are written in single quotes. It is basically a 16-bit unicode character, its minimum value is \u0000(which equals to 0) and maximum is \nFFFF(which equals to 65,535). Since it is a 16-bit unicode character so it takes 2 bytes in the memory upon declaration, while in C a character literal takes 1 byte in the memory.
For eg. char 'H'; is a character.
Boolean types are usually used for checking the logical conditions. These data types have two values true and false and also you cannot use integers instead of boolean values in java as you use in C or C++.
If you will use if(z=0) statement, in C or C++ it will compile but in java it will not compile and will give compilation error.
Previous | Home | Next |