Which are all the data types supported by java?

Data Types in Java

We know how to write the first program and get input from users as a String or number. But which are all the data types supported by java? We will see it in this section.

Data types in java are divided into 2 types:
1.Primitive data types: boolean, char, byte, short, int, long, float, and double.
2.Non-Primitive data types: Classes, Interfaces, and Arrays.


Section 1: Primitive Data Types

Primitive Data types are further divided into different types as shown below.

Data Types in Java
boolean:

Inside boolean we can save either true or false values. These are case-sensitive. Instead of true, if we tried to store True, we will get a compile-time error: cannot find symbol. Similarly, if we tried to save string value “True” or int value 1, we will get a compile-time error: incompatible types. The default value for boolean is false.

char:

We can store char value in java. The range for char is 0 to 65535 and the size required to store char is 2 bytes.
If we tried to store value > 65535 we will get a compile-time error: incompatible types: possible lossy conversion from int to char.
The default value for char is ‘u0000’.

byte:

The byte data type requires 1byte (8bits)storage space in java. The range for byte is –128 TO 127.
If we try to store value > 127 or < -128 we will get a compile-time error: incompatible types: possible lossy conversion from int to byte

short:

This type requires 2bytes memory space ranging from –32768 to 32767. Same as byte if we try to save value other than the range we will get a compile-time error.

int:

To store integer values in java, 4bytes memory space is required. The range for integer value is –2147483648 to 2147483647.

long:

If we want to store a value that is large integer numbers that can not be stored as int data type we can store it into long. Memory space occupied by long data type is java is 8bytes. The range for long is -9223372036854775808 to 9223372036854775807.

float:

If we want to store decimal values in java we go for float or double data type. The memory space required for float is 4bytes and the range is 1.4E-⁴⁵to 3.4028235E³⁸.

double:

If we want to store langer decimal values then we can store them in double. The memory space required for a double data type in java is 8bytes and the range is 4.9E-³²⁴ to 1.7976931348623157E³⁰⁸.

-A blog by Shwetali Khambe

Related Posts