Data Types and Their Ranges in Java
Java has two main types of data types: Primitive and Non-Primitive (Reference) data types.
Primitive Data Types
**byte**
- Size: 8 bits
- Range: -128 to 127
**short**
- Size: 16 bits
- Range: -32,768 to 32,767
**int**
- Size: 32 bits
- Range: -2^31 to 2^31 - 1 (-2,147,483,648 to 2,147,483,647)
**long**
- Size: 64 bits
- Range: -2^63 to 2^63 - 1 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
**float**
- Size: 32 bits
- Range: Approximately ±3.40282347E+38F (6-7 significant decimal digits)
**double**
- Size: 64 bits
- Range: Approximately ±1.79769313486231570E+308 (15 significant decimal digits)
**char**
- Size: 16 bits (1 character/2 bytes)
- Range: '\u0000' (or 0) to '\uffff' (or 65,535)
**boolean**
- Size: Depends on the JVM
- Range: true or false
Non-Primitive Data Types
Non-primitive data types (also called reference types) are defined by classes and include:
- **Strings**
- **Arrays**
- **Classes**
- **Interfaces**
Non-primitive types have all the same size (refer to their memory address) and do not store
the actual value directly, instead, they store references (pointers) to the memory location
where the actual data is stored.
Examples
public class DataTypesExample {
public static void main(String[] args) {
byte byteVar = 100;
short shortVar = 10000;
int intVar = 100000;
long longVar = 100000L;
float floatVar = 234.5f;
double doubleVar = 123.4;
char charVar = 'A';
boolean boolVar = true;
System.out.println("byte: " + byteVar);
System.out.println("short: " + shortVar);
System.out.println("int: " + intVar);
System.out.println("long: " + longVar);
System.out.println("float: " + floatVar);
System.out.println("double: " + doubleVar);
System.out.println("char: " + charVar);
System.out.println("boolean: " + boolVar);
}
}
Understanding these data types and their ranges is fundamental to effective programming
in Java.