Data Types

Data Types in Java
Store the Right Data — Know Your Types! Data types define what kind of value a variable can store — numbers, decimals, characters, text, or true/false. Master all 8 primitive types plus non-primitive types to become a confident Java programmer!
Introduction
Every variable in Java has a data type — it tells the computer what kind of value the variable can hold. Whether you're storing a person's age (whole number), a price (decimal), a grade (single character), or a name (text) — you need the right data type.
In this article, we'll explore all 8 primitive data types and the important non-primitive types like String, Array, and Object. By the end, you'll know exactly which type to use for any data!
Real-Life Analogy
Data types are like different-sized boxes for different items. A tiny box for a ring (byte), a small box for a book (int), a large box for a TV (long). You wouldn't put a TV in a ring box or a ring in a TV box! Same in Java — choose the right "box" (data type) for each value.
What are Data Types?
Data types tell the compiler what kind of value a variable can store — numbers, characters, text, or true/false.
Key Points to Remember
Essentials
- ✅ Every variable has a data type.
- ✅ Java is strongly typed.
- ✅ Two main categories: Primitive & Non-primitive.
- ✅ Determines memory allocation.
- ✅ Affects range of values.
- ✅ Cannot be changed once declared.
Two Categories of Data Types
Primitive (Built-in)
8 built-in types: byte, short, int, long, float,
double, char, boolean
Non-Primitive (Reference)
Types built from primitives: String, Array,
Class, Object
8 Primitive Data Types
Let's explore each primitive data type in detail.
1. byte
byte
Size: 1 byte (8 bits)
Range: -128 to 127
Default: 0
Use for: Small whole numbers, memory
optimization
byte b = 100;
System.out.println("Byte value: " + b);
// Output: Byte value: 100
2. short
short
Size: 2 bytes (16 bits)
Range: -32,768 to 32,767
Default: 0
Use for: Medium whole numbers
short s = 5000;
System.out.println("Short value: " + s);
// Output: Short value: 5000
3. int ⭐ (Most Used)
int
Size: 4 bytes (32 bits)
Range: -2,147,483,648 to 2,147,483,647
Default: 0
Use for: Whole numbers (age, count,
marks)
int age = 15;
int marks = 85;
int studentCount = 40;
System.out.println("Age: " + age);
// Output: Age: 15
int is the go-to type for whole numbers in
Java!
4. long
long
Size: 8 bytes (64 bits)
Range: Very large numbers
Default: 0L
Use for: Very large whole numbers
(population, timestamps)
long population = 8000000000L; // Note the 'L'
System.out.println("Population: " + population);
// Output: Population: 8000000000
L at the end of long values!
5. float
float
Size: 4 bytes (32 bits)
Range: Decimal (6-7 digits)
Default: 0.0f
Use for: Decimals (less precise)
float pi = 3.14f; // Note the 'f'
float price = 99.99f;
System.out.println("Price: " + price);
// Output: Price: 99.99
f at the end of float values!
6. double ⭐ (Most Used for Decimals)
double
Size: 8 bytes (64 bits)
Range: Decimal (15 digits)
Default: 0.0
Use for: Decimals (more precise)
double price = 99.99;
double pi = 3.14159265358979;
double salary = 55000.50;
System.out.println("Salary: " + salary);
// Output: Salary: 55000.5
double is preferred for decimal numbers due to
its precision!
7. char
char
Size: 2 bytes (16 bits)
Range: 0 to 65,535 (Unicode)
Default: '\u0000'
Use for: Single character (letter,
digit, symbol)
char grade = 'A';
char firstLetter = 'R';
char digit = '5';
char symbol = '#';
System.out.println("Grade: " + grade);
// Output: Grade: A
'A',
not double quotes.
8. boolean
boolean
Size: 1 bit
Values: true / false
Default: false
Use for: Yes/No, True/False, On/Off
situations
boolean isPass = true;
boolean isSunday = false;
boolean hasPermission = true;
System.out.println("Passed: " + isPass);
// Output: Passed: true
Non-Primitive Data Types
Non-primitive types are built using primitive types. They store references to objects.
1. String
String
Stores sequence of characters (text).
String name = "Ravi";
String city = "Kolkata";
String message = "Hello, World!";
System.out.println("Name: " + name);
// Output: Name: Ravi
"Ravi"
2. Array
Array
Stores multiple values of the same type.
int[] nums = {1, 2, 3, 4, 5};
String[] names = {"Ravi", "Priya", "Amit"};
System.out.println("First number: " + nums[0]);
// Output: First number: 1
3. Class
Class
A template for creating objects.
class Student {
String name;
int age;
}
4. Object
Object
An object is a specific instance of a class.
Student s = new Student();
s.name = "Ravi";
s.age = 15;
Complete Example Program
public class DataTypesDemo {
public static void main(String[] args) {
int age = 15;
double price = 99.99;
char grade = 'A';
String name = "Ravi";
boolean isPass = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Price: " + price);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + isPass);
}
}
- Name: Ravi
- Age: 15
- Price: 99.99
- Grade: A
- Passed: true
Data Type Comparison Table
| Type | Size | Use For |
|---|---|---|
| byte | 1 byte | Small whole numbers |
| short | 2 bytes | Medium whole numbers |
| int | 4 bytes | Whole numbers (most used) |
| long | 8 bytes | Very large numbers |
| float | 4 bytes | Decimals (less precise) |
| double | 8 bytes | Decimals (more precise) |
| char | 2 bytes | Single character |
| boolean | 1 bit | true / false |
| String | Variable | Text (sequence) |
Real-Life Examples
Student Data
Stringname = "Ravi"intage = 15doublemarks = 85.5chargrade = 'A'booleanisPass = true
Product Data
Stringname = "Laptop"intquantity = 5doubleprice = 55000.99booleaninStock = true
Address Data
Stringstreet = "MG Road"Stringcity = "Kolkata"intpincode = 700001Stringcountry = "India"
Flight Data
StringflightNo = "AI102"intseats = 180doubleprice = 8500.00booleanisDelayed = false
Tips for Success
Best Practices
- 💡 Choose right type for your data.
- 💡 Use
intfor whole numbers. - 💡 Use
doublefor decimals. - 💡 Use
charfor single character. - 💡 Use
Stringfor text. - 💡 Use
booleanfor true/false. - 💡 Save memory with correct type.
- 💡 Use meaningful variable names.
- 💡 Follow Java naming conventions (camelCase).
- 💡 Add comments for clarity.
Common Mistakes
Watch Out for These!
- ❌ Wrong type declaration — Using int for decimals.
- ❌ Losing decimal in int — 10/3 = 3, not 3.33!
- ❌ Forgetting quotes for char (' ') — Must use single quotes.
- ❌ Forgetting quotes for String (" ") — Must use double quotes.
- ❌ Using = instead of == — = is assignment; == is comparison.
- ❌ Mixing types wrongly — Type mismatch errors.
- ❌ Not adding L for long — Value out of int range!
- ❌ Not adding f for float — Compiler treats as double.
Did You Know?
Fun Fact
Java was designed with strong typing to prevent bugs! Unlike some languages (like Python or JavaScript), you cannot mix types randomly — this makes Java safer and less prone to runtime errors!
When to Use Which Type?
| Situation | Best Type |
|---|---|
| Age of a person | int (small whole number) |
| Price of an item | double (decimal) |
| Student's grade | char (single letter) |
| Student's name | String (text) |
| Pass or Fail status | boolean (true/false) |
| World population | long (very large number) |
| Temperature | double (decimal) |
| Number of items in cart | int |
| List of students | Array or String[] |
| Yes/No answer | boolean |
Data Type Conversion (Type Casting)
Sometimes we need to convert one type to another.
Automatic Conversion (Widening)
// Automatic: smaller to larger type
int a = 100;
double b = a; // int to double (automatic)
System.out.println(b); // Output: 100.0
Manual Conversion (Narrowing)
// Manual: larger to smaller type
double x = 99.99;
int y = (int) x; // double to int (need cast)
System.out.println(y); // Output: 99 (decimal lost!)
Frequently Asked Questions
Q1. Why so many data types?
Different types save memory and represent data efficiently. A byte uses only 1 byte of memory; long uses 8 bytes.
Q2. Which type should I use for age?
Use int. Age is a whole number under 200
(well within int range).
Q3. What's the difference between float and double?
float has 6-7 digit precision (4 bytes).
double has 15 digit precision (8 bytes).
Use double for better accuracy.
Q4. Is String a primitive type?
No! String is non-primitive (it's a class).
But it's used so often, it feels primitive.
Q5. Why can't I use = for comparison?
= is assignment. Use == for
comparison (or .equals() for Strings).
Q6. What is default value?
If you don't assign a value, Java gives a default value (0 for numbers, false for boolean, null for objects).
Q7. Can I change data type of a variable?
No! Once declared, data type is fixed. But you can convert values using type casting.
Q8. What's the size of String?
String has variable size — depends on the length of text it contains.
Q9. Which type for boolean values?
Use boolean. It stores only true or false.
Q10. What if my number is too large for int?
Use long. It can hold much larger numbers.
Key Takeaways
- Java has 8 primitive types + non-primitive.
- 5 most used: int, double, char, String, boolean.
- int for whole numbers.
- double for decimals.
- char for single characters (single quotes).
- String for text (double quotes).
- boolean for true/false.
- Java is strongly typed.
- Choose the right type to save memory.
- Practice with different types!
Key Takeaway
Master 5 essential types: int, double, char,
String, boolean! Right data type = right program!
Data types are the building blocks of Java programs.
Once you know when to use each type, you can store any
kind of data efficiently. Choose wisely, code confidently!
Practice using different data types. You will
code with confidence!
⭐ STORE SMART, TYPE CLEAR, CODE BETTER! ⭐
Best of Luck! Practice writing
programs with different types. Every program you write
strengthens your understanding! 👍😊
Flowchart → Visual Thinking → Smart Solutions → Better
Results! 🚀
Home & Online Tuition
Learn from an experienced tutor with personalized guidance.
Available Locations
Expert Home & Online Tuition
Personalized one-to-one tuition that focuses on concept building, practical learning, problem-solving skills, and excellent academic performance. Suitable for school students looking for structured, interactive, and result-oriented learning.
Subjects We Teach
Why Choose Our Tuition?
✅ Concept-Based Learning
✅ Practical Examples
✅ Weekly Tests
✅ Doubt Solving Sessions
✅ Practice Worksheets
✅ MCQ & Assignments
✅ Exam Preparation
✅ Flexible Class Timings