Data Types in Java
In Java, data types define the type of value a variable can store. Every variable in Java must have a specific data type. Data types help Java allocate memory efficiently and perform operations safely.
For example, if you store a person's age, Java uses an integer data type. If you store a decimal number like salary or price, Java uses floating-point data types.
Why Data Types Are Important
- Help Java manage memory efficiently
- Improve program performance
- Prevent invalid data assignments
- Make code easier to understand
- Provide type safety during compilation
Categories of Data Types in Java
Java data types are mainly divided into two categories.
- Primitive Data Types
- Non-Primitive Data Types
Primitive Data Types in Java
Primitive data types are predefined by Java. They store simple values directly in memory and are faster than non-primitive types.
List of Primitive Data Types
Primitive Data Type Examples
byte age = 25;
short year = 2026;
int salary = 50000;
long population = 8000000000L;
float temperature = 36.5f;
double price = 45999.99;
char grade = 'A';
boolean isLoggedIn = true;Real-World Examples of Primitive Types
- byte → Student age
- short → Year value
- int → Employee ID
- long → World population
- float → Temperature readings
- double → Product price or bank balance
- char → Grade letter
- boolean → Login status
Explanation of Each Primitive Data Type
1. byte
The byte data type is used to save memory when storing small numbers. Its range is from -128 to 127.
byte marks = 95;2. short
The short data type is larger than byte and is used for medium-range integer values.
short distance = 15000;3. int
The int data type is the most commonly used integer type in Java.
int totalUsers = 120000;4. long
The long data type is used when int is not large enough. It must end with the letter L.
long mobileNumber = 9876543210L;5. float
The float data type stores decimal numbers with single precision. It must end with the letter f.
float percentage = 89.5f;6. double
The double data type stores decimal numbers with higher precision than float.
double accountBalance = 105000.75;7. char
The char data type stores a single Unicode character.
char gender = 'M';8. boolean
The boolean data type stores only two values: true or false.
boolean paymentSuccess = true;Non-Primitive Data Types in Java
Non-primitive data types are also called reference types because they store references to objects instead of actual values.
Examples of Non-Primitive Data Types
- String
- Array
- Class
- Interface
- Object
- Collection
- Enum
Non-Primitive Data Type Examples
String name = "Rahul";
int[] numbers = {10, 20, 30};
class Student {
int id;
String studentName;
}Features of Non-Primitive Data Types
- Can store multiple values
- Can call methods
- Can be null
- Memory size is not fixed
- Created by programmers or Java libraries
Difference Between Primitive and Non-Primitive Data Types
Default Values of Primitive Data Types
Type Conversion in Java
Type conversion means changing one data type into another. Java supports automatic conversion and manual conversion.
Automatic Type Conversion
int num = 100;
double data = num;Manual Type Casting
double price = 99.99;
int finalPrice = (int) price;Memory Allocation in Java
Primitive variables are generally stored in stack memory, while objects and arrays are stored in heap memory. Reference variables store memory addresses pointing to heap objects.
Common Mistakes Beginners Make
- Forgetting to add L after long values
- Forgetting to add f after float values
- Using double quotes instead of single quotes for char
- Confusing String with char
- Using int for very large numbers
Best Practices
- Use int for normal integer calculations
- Use double for decimal calculations
- Use boolean for conditions and flags
- Use meaningful variable names
- Choose smaller data types only when memory optimization is required
Interview Questions on Java Data Types
- What are primitive data types in Java?
- What is the difference between float and double?
- Why is String a non-primitive data type?
- What is type casting in Java?
- What are default values in Java?
- What is the size of int and double?
- Difference between char and String?
- Can primitive data types store null values?
- Why is boolean size JVM dependent?
- What is automatic type conversion?
Conclusion
Data types are one of the most important concepts in Java programming. Understanding primitive and non-primitive data types helps developers write efficient, secure, and optimized code. Choosing the correct data type improves performance, reduces memory usage, and makes applications easier to maintain.