What are the data types in Java?
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
- Primitive data types
- Non-primitive data types
1) Primitive data types : There are eight primitive data types in Java
Data Type | Size | Range | Default Values |
---|---|---|---|
boolean | 1 bit | true or flase | flase |
char | 2 bytes | one character/letter or 0 to 65,536 (unsigned) , ASCII values | \u0000 |
byte | 1 byte | 128 to 127 | 0 |
short | 2 bytes | 32,768 to 32,767 | 0 |
int | 4 bytes | 2,147,483,648 to 2,147,483,647 | 0 |
long | 8 bytes | 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 |
float | 4 bytes | 6 to 7 significant decimal digits | 0.0 |
double | 8 bytes | 15 significant decimal digits | 0.0 |
2) Non-Primitive Data Types : Non-primitive data types are created by the programmer, and are not defined by Java language.
Examples : Strings, Arrays, Classes, Interface etc.
String myStrVar = “Hello, It’s String Variable”;
Difference between primitive data types and non primitive data types
Primitive Data Type | Non-primitive Data type |
---|---|
This are predefined in Java | This are created by the programmer and is not defined by Java |
A primitive type always has a value | Non-primitive types can be null. |
Primitive type starts with a lowercase letter. | Non-primitive types start with an uppercase letter. |