
Data Types in Java represents the nature of data to be stored in memory. In other words, it specifies size and value that can be stored in the variable.
For example, a variable var can store an integer value like 100.
int var = 10;
Table of Contents
Data Type group
In group, there are two types of data are available which are as follows:
- Primitive type
- Non-primitive type
Primitive Data Types in Java
A reserved keyword of the language specifies the primitive type. Primitive values do not share state with other primitive values and there is no additional method for this.
There are following categories of primitive type:
- Numeric Data Types (byte, short, int, long, float, double)
- Char Data Types (char)
- Boolean Data Types (boolean)
byte type
This type stores whole numbers from -128 to 127. It is an 8-bit signed two’s complement integer. The byte data type can be useful when the memory savings actually matters.
Default value of this data type is 0.
public class Example { // Class Name
public static void main(String[] args) {
byte byteVar = 10;
}
}
sort type
This data type can store whole numbers from -32,768 and a maximum value of 32,767 (inclusive). Default value is 0. It is suitable if you use 16 bit processors like 8086.
short shortVar = 500;
int type
This data type can store whole numbers between -231 and 231-1. Default value is 0. Programmers mostly prefer this data type for numeric value in Java.
int intVar = 100;
long type
long data type is used when int is not enough to hold big value. It can store whole numbers between -263 and 263-1. Default value is 0L. You should use L at the end of value.
long longVar = 1000L;
float type
It can store floating point numbers. Default value is 0f. You should use F or f at the end of value. It is mostly used when 5 to 6 digits of accuracy required.
float floatVar = 10.06F;
double type
It can store floating point numbers. Default value is 0d. You should use D or d at the end of value. It is mostly used when 14 to 15 digits of accuracy required.
double doubleVar = 10.06F;
char type
The char data type is a single 16-bit Unicode character and has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive). The character must be surrounded by single quotes.
char charVar = 'x';
boolean type
The boolean data type has only two possible values: true and false. Most importantly, size is not applicable for boolean data type.
boolean booleanVar = true;
Summary of Primitive Data Types and Java Data Type size
# | Data Type | Size | Range | Wrapper Classes | Default Value |
---|---|---|---|---|---|
1 | byte | 1-byte | 2-7 to 27-1 | Byte | 0 |
2 | short | 2-byte | 2-15 to 215-1 | Short | 0 |
3 | int | 4-byte | 2-31 to 231-1 | Integer | 0 |
4 | long | 8-byte | 2-63 to 263-1 | Long | 0L |
5 | float | 4-byte | 3.4e-038 to 3.4e+038 | Float | 0F |
6 | double | 8-byte | 1.7e-308 to 1.7e+308 | Double | 0D |
7 | char | 2-byte | 0 to 65535 | Character | ‘\u0000’ |
8 | boolean | N/A | true or false | Boolean | false |
Non primitive data types in Java
The non primitive data types include Classes, Interfaces, and Arrays.
What is the range of short data type?
The range of short data type in Java is 2-15 to 215-1.