Java Data Types
Data type specifies type of data stored by a variable. In other words, data type tells to the compiler what type of data a variable can store during program execution.
To deal with different types of data, Java provides 8 types of predifined data types that are also known as primitive data types. Primitive data values are not objects and each primitive type has certain range to store values.
- char
- boolean
- byte
- short
- int
- long
- float
- double
We can broadly catageries these into integral type (char, byte, short, int, long), floating-point type (float, double) and boolean type.
Java char Type
Char data type is used to store a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Data Type | Size (bits) | Minimum Value | Maximum Value |
---|---|---|---|
char | 16 | \u0000 (or 0) | \uffff (or 65,535 inclusive) |
Java char Example
Output:
Java boolean Type
boolean data type is used to store a logical value either True or False. It used to set simple flag for the conditional programming context.
Data Type | Size (bits) | True value | False value |
---|---|---|---|
boolean | not defined | true | false (default) |
Java boolean Example
Output:
Java byte Type
byte data type is used to store numeric value of limited range. It is an 8-bit signed two's complement integer and can be helpful to save memory. Please refer the below table for its minimum, maximum value and size as well.
Data Type | Size (bits) | Minimum Value | Maximum Value |
---|---|---|---|
byte | 8 | -27 (-128) | 27-1 (+127) |
Java byte Example
Output:
Java short Type
short data type is used to store numeric value of limited range but larger than byte size. It is an 16-bit signed two's complement integer and can be helpful to save memory. Its size is twice the size of byte. Please refer the below table for its minimum, maximum value and size as well.
Data Type | Size (bits) | Minimum Value | Maximum Value |
---|---|---|---|
short | 16 | -215 (-32768) | 215-1 (+32767) |
Java short Example
Output:
Java int Type
int data type is used to store numeric value and can store larger than byte and short. It is a 32-bit signed two's complement integer. Its size is twice the size of short data type. Please refer the below table for its minimum, maximum value and size as well.
Data Type | Size (bits) | Minimum Value | Maximum Value |
---|---|---|---|
int | 32 | -231 | 231-1 |
Note: In Java SE 8 and later versions, we can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.
Java int Example
Output:
Java long Type
long data type is used to store numeric value and can store larger than byte, short and int data types. It is a 64-bit signed two's complement integer. Its size is twice the size of int data type. Please refer the below table for its minimum, maximum value and size as well.
Data Type | Size (bits) | Minimum Value | Maximum Value |
---|---|---|---|
long | 64 | -263 | 263-1 |
Note: In Java SE 8 and later versions, we can use long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.
Java long Example
Output:
Java float Type
float data type is used to store floating-point value. It is a single-precision 32-bit IEEE 754 floating point. We can use it to store short range of floating-point values to save memory. Please refer the below table for its minimum, maximum value and size as well.
Data Type | Size (bits) | Minimum +ve Value | Maximum +ve Value |
---|---|---|---|
float | 32 | 1.401e-45f | 3.402e+38f |
Java float Example
Output:
Java double Type
double data type is used to store floating-point value. It is a double-precision 64-bit IEEE 754 floating point. We can use it to store large range of floating-point values which are out of the float range. Please refer the below table for its minimum, maximum value and size as well.
Data Type | Size (bits) | Minimum +ve Value | Maximum +ve Value |
---|---|---|---|
double | 64 | 4.94e-324 | 1.79e+308 |
Java double Example
Output:
Java Data Types Default Values and Corresponding Wrapper Classes
All the data types we discussed here are primitive type that means these are not object. But Java provides wrapper classe for each primitive type to convert into object. Here, in the below table we have mentioned default value of each primitive type and corresponding wrapper class.
Data Type | Default Value | Wrapper Class |
---|---|---|
byte | 0 | Byte |
short | 0 | Short |
char | '\u0000' | Character |
int | 0 | Integer |
long | 0L | Long |
float | 0.0f | Float |
double | 0.0d | Double |
boolean | false | Boolean |