What are Data Types in Java?
Data types in Java specify the size and type of values a variable can hold. They help the compiler allocate appropriate memory and enforce type safety during operations. Java data types are divided into two main categories: Primitive Data Types and Non-Primitive Data Types.
Primitive Data Types in Java
Primitive data types are the most basic data types available in Java. They are predefined by the language and named by a reserved keyword.
| Data Type | Size | Range / Values | Default Value |
|---|---|---|---|
| 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 | 0L |
| float | 4 bytes | approximately ±3.40282347E+38F (6-7 significant digits) | 0.0f |
| double | 8 bytes | approximately ±1.79769313486231570E+308 (15 significant digits) | 0.0d |
| char | 2 bytes | 0 to 65,535 (Unicode characters) | 'u0000' |
| boolean | 1 bit | true or false | false |
Categories of Data Types
Java divides data types into two main categories:
Primitive Data Types
- byte → 1 byte, stores -128 to 127 (whole numbers)
- short → 2 bytes, stores -32,768 to 32,767
- int → 4 bytes, stores whole numbers (most commonly used)
- long → 8 bytes, stores large whole numbers (suffix L/l required)
- float → 4 bytes, stores fractional numbers (suffix F/f required)
- double → 8 bytes, stores double-precision fractional numbers
- char → 2 bytes, stores a single character/letter or ASCII values
- boolean → 1 bit, stores true or false values
Non-Primitive Data Types
- String → Stores sequence of characters (text)
- Arrays → Collection of similar data types
- Classes → User-defined data types (objects)
- Interfaces → Contract for classes to implement
- Collections → ArrayList, HashSet, HashMap etc.
Data Types Example Program
public class DataTypesExample {
public static void main(String[] args) {
// Primitive Data Types
byte myByte = 100;
short myShort = 10000;
int myInt = 100000;
long myLong = 10000000000L; // L suffix required
float myFloat = 3.14f; // f suffix required
double myDouble = 3.14159265359;
char myChar = 'A';
boolean isJavaFun = true;
// Non-Primitive Data Types
String myText = "Hello, Java!";
int[] myArray = {1, 2, 3, 4, 5};
System.out.println("byte: " + myByte);
System.out.println("short: " + myShort);
System.out.println("int: " + myInt);
System.out.println("long: " + myLong);
System.out.println("float: " + myFloat);
System.out.println("double: " + myDouble);
System.out.println("char: " + myChar);
System.out.println("boolean: " + isJavaFun);
System.out.println("String: " + myText);
System.out.print("Array: ");
for (int num : myArray) {
System.out.print(num + " ");
}
}
}
byte: 100 short: 10000 int: 100000 long: 10000000000 float: 3.14 double: 3.14159265359 char: A boolean: true String: Hello, Java! Array: 1 2 3 4 5
Primitive vs Non-Primitive Data Types
🔢 Primitive Data Types
- Predefined by Java language
- Stored in stack memory
- Cannot be null
- Have default values
- Fast performance
📦 Non-Primitive Data Types
- Created by programmers (except String)
- Stored in heap memory
- Can be null
- No default values (null reference)
- Slightly slower due to object overhead
Best Practices for Using Data Types
Use int for small numbers and long for large numbers
Use double for decimals unless precision is critical
Use float only when memory constraints matter
Always add suffix L for long and F for float values
Use String instead of char arrays for text manipulation
Avoid unnecessary type conversions to reduce performance overhead