Data Types and its Categories in Java
Data Types and its Categories in Java
Generally, data types are used to create variables where variables will hold values, as defined in the range of values of a datatype.
Java supports two categories of data types:
→ Primitive data types and
→ Non Primitive Data Types
Non Primitive Data Types
Non primitive data types are also called as reference data types
A Non Primitive Data Type is used to refer to an object.
In java variables of type class , arrays , enums and interface are represented as objects.
We will discuss this in later chapters like Arrays and Classes and Objects
Primitive Data Types
There are totally eight primitive data types in Java. They can be categorized as given below:
↪ Integer types (Does not allow decimal places)
(a) byte
(b) short
(c) int
(d) long
↪ Rational Numbers(Numbers with decimal places)
(a) float
(b) double
↪ Characters
char
↪ Conditional
boolean
→ Integer Type
Type | Size(in Bytes) | Range |
byte | 1 | -128 to 127 |
short | 2 | -32768 to 32767 |
int | 4 | -2147483648 to 2147483648 |
long | 8 | -9223,372,036,854,775,808 to 9223,372,036,854,775,807 |
→ Rational Numbers
Type | Size(in Bytes) | Range |
float | 4 | -3.4 * 10^38 to 3.4 * 10^38(6 significant decimal digits) |
double | 8 | -1.7*10^308 to 1.7*10^308(15 significant decimal digits) |
→ Characters
Type | Size(in Bytes) | Range |
char | 2 | 0 to 65535 |
Why Java uses 2 bytes for characters ?
→ In Java almost 18 international languages are supported .
→ Now, the characters and symbols of these languages cannot be accommodated in 1 byte space in memory ,so java takes 2 byte for characters.
→ Java supports UNICODE but C language supports ASCII code. In ASCII code we can represent characters of English language are present, so for storing all English latter and symbols 1 byte is sufficient.
→ But UNICODE character set is superset of ASCII in which all the characters which are available in 18 international languages are supported and it contains 65536 characters ranging from 0 to 65535
→ To assign unicode values we have two options:
Use the numeric value
OR
Use the format ‘\uxxxx’ where xxxx is hexadecimal form of the value
For example:
char ch=65;
OR ⇰Both means we are assigning letter A
char ch=‘\u0041’;
→ Conditional
Type | Size | Range |
boolean | 1 bits | true or false |