As we all know that Java lies in the category of Statically-typed languages, that means the languages where each variable and expression-type is already known at the compile time, hence, once a variable is declared of a certain Data-type it cannot hold another type of value. Previously, we learnt about declaring variables and studied in a brief details about some of the commonly used Data Types. But now, let's Master this complete topic about Data Types.
What is a Data-type?
Data Types define the values that a particular variable can hold. They also specify the size of a variable, depending upon the type of Data it holds, like the int, float, char, boolean, String all have different properties and sizes. These all Data types are pre-defined as a part of the Programming Language-Java.
Categories of Data-Types in Java
There are 2 categories of Data-Types in Java:
- Primitive Data-types : A primitive Data-type is pre-defined by the Language and its name is preserved by a keyword. Java supports 8 primitive Data-types namely - byte, int, long, float, double, char, short, boolean. These 8 primitive Data-types are the building blocks of Data-Manipulation.
* The Developers of Java made these primitive data-types such that their size and properties do not change from one System to another. Hence, maintaining the portability of Java.
- Non-primitive Data-Types : These are the Data-types which donot store the values like the Primitive ones, but store the reference to the value, or we can say they store the addresses of the locations where the values are stored. Thatswhy, they are also called as Reference Data-types. These Data-types include arrays, Strings, classes, interfaces etc.
1) Primitive Data-types
byte
It is a 8-bit signed two's compliment integer, meaning it can hold numbers between -128 and +127. These are mostly used in case of memory management, due to their small size of only 8 Bits(1 Byte). These are also used in large arrays for saving memory. The default value of a byte variable is 0.
Example:
byte n = 126;
System.out.println(n);
OUTPUT
126
* If we try to enter a value beyond its range then an error will occur of type mismatch, saying the value is out of range. For example: byte i = 200; , it will generate a type mismatch error.
int
It is a 32-bit signed two's compliment integer. It can hold value ranging from -2,147,483,648 to +2,147,483,648 , including these also. It is the most commonly used Data-type for integer values if there is no space issue. It has a default value of 0 and default size of 4 bytes(32 bits = 4 bytes).
Example: int n = 234;
short
Stores Integer values but only for a small range. It is actually a 16 bit signed two's compliment integer having value range of -32,768 to +32,768(inclusive). It also has default value of 0 and size of 2 bytes.
Example: short n = 245655;
long
It also stores Integer values as int and short but It is used when the value to be stored is even out of int's value-range, thatswhy it is a 64-bit two's compliment integer. Having a very large value-range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Having size of 8 bytes.
Example: long a = 356000L;
* We have written a 'L' at the end to of the value to specify it is a long integer value.
float
It stores floating point values like decimals. It holds upto 6 decimal digits. It has no value range, any floating point value can be held by a float variable. Size = 4 bytes. Default value = 0.0F.
Example: float f = 23.3232F;
* 'F' is to denote it is a floating value, it also optional in some cases.
double
It also stores floating point values like float, but having more decimal places (upto 16 decimal places) than float. Used to storing precise values. Size = 8 bytes . Default value = 0.0
Example: double d = 12.3232334545;
char
char is a 16 bit Unicode character. It stores character type values like 'a', 'B', '+' etc. These values should be in single qoutes while assigning to variable. Size = 2 bytes. Default value = '\u0000'.
Example: char a = 'A';
* The size of char in C/C++ is 1 byte but here it is 2 bytes because, here it represents Unicode character, which requires 16 bits. Unlike ASCII in C/C++ which uses only 8 bits.
boolean
It stores only 2 possible values or states: true or false. It is used in cases where we have to track whether case is true/false. Size = 1 byte. Default value = false.
Example: boolean b = true;