The datatypes represents type of data that we store into memory. The datatypes in java language are classified into three types
1) Primitive datatypes
The primitive datatypes are predefined and designed to store a single value.
Ex: int, char etc.
2) Derived datatypes
The derived data types are predefined and designed to store multiple values.
Ex: array
3) User defined datatypes
If a datatype is created by the user or the programmer, then it is called as user defined datatype. Using the user defined datatype we can store any number of values and any type of values, according to the application requirement.
Ex: class
Primitive datatypes
The primitive datatypes are predefined and designed to store a single value and they are used to store the basic inputs required for a program. The primitive data type are also called as fundamental data types.
The java language provides 8 primitive data types and they are classified into 4 categories.
- Integer
- Floating-point
- Character
- Boolean
1) Integer Category
This category can be used to storing number, either positive or negative without a decimal point. Under the integer we have 4 primitive data types and they are
a) byte
b) short
c) int
d) long
All the 4 primitive data types are used for storing same kind of data, but their sizes and ranges are different so that the memory can be utilized efficiently without any wastage.
Data Types | Size | Range |
Byte | 1 | -128 to 127 |
Short | 2 | 032768 to 32767 |
Int | 4 | -2147483648 to 214743647 |
Long | 8 | -9223372036854775808 to 9223372036854775807 |
MSB | ||||||||
Sign Bit 0 +ve 1 -ve | Value Bits |
2) Floating- Point Category
The category used for storing numbers either positive or negative with decimal point. Under the floating category we have two primitive data types and they are
a) float
b) double
Both the primitive data types under floating-point category are used for storing same kind of data, but their sizes and ranges are different to that the memory can be utilized efficiency without wastage.
Data Types | Size | Range | Number of decimal digits |
Float | 4 | 1.4E-46 to 3.4E38 | 7 |
double | 8 | 4.9E-324 to 1.79E308 | 15 |
3) Character Category:
This category can be used for storing a single character. A character can be represented by either one alphabet one special symbol. Under the character there is only on primitive data type and it is char.
Data Types | Size | Range |
Char | 2 | 0 to 65535 |
Qns: Why is the size of char 1 byte in C language and 2 bytes in Java?
Ans) When an application is developed in C language, it uses the characters of only english language. To store the English language characters 1 byte of memory is sufficient. The C language uses ASCII character set (0-255).
When an application is developed in java language, it uses the character of all the foreign languages. To store the characters of all languages 1 byte of memory is not sufficient, therefore the size is increased to 2 bytes of Java language. The Java language use UNICODE character set (0-65535).
4) Boolean Category
This category is used for storing either true or false. Under the boolean category we have only primitive type i.e. boolean
Data Types | Size | Range |
Boolean | JVM Dependent | True/false |