Introduction
Data types specify the type of data that a variable can hold. In Java, every variable must have a data type. Java is a strongly-typed language, meaning you must declare the type of every variable.
Why Data Types?
- Memory Allocation: Determines how much memory to allocate
- Operations: Defines what operations can be performed
- Type Safety: Prevents errors by checking type compatibility
- Performance: Optimizes memory usage
Categories of Data Types
Java has two main categories:
Data Types
|
|-----> Primitive Types (8 types)
|
|-----> Reference Types (Classes, Interfaces, Arrays)
Primitive Data Types
Primitive types are the most basic data types. They store simple values directly in memory.
There are 8 Primitive Data Types:
- byte
- short
- int
- long
- float
- double
- char
- boolean
1. Integer Types
Integer types store whole numbers (no decimal points).
a) byte
Details:
- Size: 8 bits (1 byte)
- Range: -128 to 127
- Default value: 0
- Use: Save memory when you know values are small
Example:
byte age = 25;
byte temperature = -10;
byte score = 100;
// Out of range - ERROR
byte big = 200; // ❌ Error: 200 is outside range
Memory:
8 bits = 1 byte
[-128 to 127]
b) short
Details:
- Size: 16 bits (2 bytes)
- Range: -32,768 to 32,767
- Default value: 0
- Use: When byte is too small and int is too large
Example:
short year = 2024;
short population = 30000;
short elevation = -500;
// Out of range - ERROR
short large = 50000; // ❌ Error: outside range
Memory:
16 bits = 2 bytes
[-32,768 to 32,767]
c) int (Most Commonly Used)
Details:
- Size: 32 bits (4 bytes)
- Range: -2,147,483,648 to 2,147,483,647 (about -2 billion to +2 billion)
- Default value: 0
- Use: Default choice for integer values
Example:
int salary = 50000;
int population = 1000000;
int distance = -250;
int count = 0;
// Different number formats
int decimal = 100; // Decimal
int octal = 0144; // Octal (starts with 0)
int hex = 0x64; // Hexadecimal (starts with 0x)
int binary = 0b1100100; // Binary (starts with 0b, Java 7+)
// Underscore for readability (Java 7+)
int million = 1_000_000; // Same as 1000000
int billion = 1_000_000_000;
Memory:
32 bits = 4 bytes
[-2,147,483,648 to 2,147,483,647]
d) long
Details:
- Size: 64 bits (8 bytes)
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- Default value: 0L
- Use: When int range is not enough
- Important: Must use ‘L’ or ‘l’ suffix
Example:
long bigNumber = 10000000000L; // L suffix required
long population = 8000000000L;
long distance = 9999999999999L;
// Without L - ERROR
long num = 10000000000; // ❌ Error: treated as int, which overflows
// With underscore
long trillion = 1_000_000_000_000L;
Memory:
64 bits = 8 bytes
[Very large range]
2. Floating-Point Types
Floating-point types store decimal numbers.
a) float
Details:
- Size: 32 bits (4 bytes)
- Range: Approximately ±3.4 × 10³⁸ (7 decimal digits precision)
- Default value: 0.0f
- Use: When memory is limited
- Important: Must use ‘f’ or ‘F’ suffix
Example:
float price = 99.99f; // f suffix required
float temperature = -15.5f;
float pi = 3.14159f;
// Without f - ERROR
float value = 10.5; // ❌ Error: treated as double
// Scientific notation
float scientific = 1.23e2f; // 123.0
float small = 1.23e-2f; // 0.0123
Memory:
32 bits = 4 bytes
[Decimal numbers with ~7 digits precision]
b) double (Most Commonly Used for Decimals)
Details:
- Size: 64 bits (8 bytes)
- Range: Approximately ±1.7 × 10³⁰⁸ (15 decimal digits precision)
- Default value: 0.0d or 0.0
- Use: Default choice for decimal values
- Note: ‘d’ or ‘D’ suffix optional
Example:
double salary = 50000.75;
double pi = 3.141592653589793;
double distance = 1234.567890;
// Optional d suffix
double value = 100.50d; // d is optional
// Scientific notation
double large = 1.23e5; // 123000.0
double small = 1.23e-5; // 0.0000123
// Special values
double positive = Double.POSITIVE_INFINITY;
double negative = Double.NEGATIVE_INFINITY;
double notANumber = Double.NaN; // Not a Number
Memory:
64 bits = 8 bytes
[Decimal numbers with ~15 digits precision]
float vs double:
- double: More precision (15 digits), default choice
- float: Less precision (7 digits), saves memory
3. Character Type
char
Details:
- Size: 16 bits (2 bytes)
- Range: 0 to 65,535 (Unicode characters)
- Default value: ‘\u0000’ (null character)
- Use: Store single characters
- Important: Use single quotes ’ ’
Example:
char letter = 'A';
char digit = '9';
char symbol = '@';
char space = ' ';
// Unicode representation
char unicode = '\u0041'; // 'A' in Unicode
char rupee = '₹';
// Escape sequences
char newline = '\n'; // New line
char tab = '\t'; // Tab
char backslash = '\\';
char singleQuote = '\'';
char doubleQuote = '\"';
// ASCII value
char ch = 65; // Represents 'A'
// Cannot store multiple characters
char invalid = 'AB'; // ❌ Error: only single character
Common Escape Sequences:
| Escape | Meaning |
|---|---|
\n | New line |
\t | Tab |
\r | Carriage return |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\b | Backspace |
Memory:
16 bits = 2 bytes
[Unicode characters: 0 to 65,535]
4. Boolean Type
boolean
Details:
- Size: Not precisely defined (typically 1 bit, but JVM dependent)
- Values: Only
trueorfalse - Default value: false
- Use: Logical conditions, flags
Example:
boolean isStudent = true;
boolean hasLicense = false;
boolean isValid = true;
// Used in conditions
boolean result = (10 > 5); // true
boolean check = (3 == 4); // false
// Logical operations
boolean and = true && false; // false
boolean or = true || false; // true
boolean not = !true; // false
// Cannot use numbers as boolean
boolean wrong = 1; // ❌ Error: not 0 or 1 like C
Important:
- Java boolean is NOT a number
- Cannot use 0 for false or 1 for true (unlike C/C++)
- Only
trueandfalseare valid
Primitive Data Types Summary Table
| Type | Size | Range | Default | Example |
|---|---|---|---|---|
| byte | 8 bits | -128 to 127 | 0 | byte b = 100; |
| short | 16 bits | -32,768 to 32,767 | 0 | short s = 30000; |
| int | 32 bits | -2³¹ to 2³¹-1 | 0 | int i = 100000; |
| long | 64 bits | -2⁶³ to 2⁶³-1 | 0L | long l = 10000L; |
| float | 32 bits | ±3.4e38 | 0.0f | float f = 10.5f; |
| double | 64 bits | ±1.7e308 | 0.0 | double d = 10.5; |
| char | 16 bits | 0 to 65,535 | ’\u0000’ | char c = 'A'; |
| boolean | 1 bit | true or false | false | boolean b = true; |
Reference Data Types
Reference types don’t store the actual value; they store a reference (memory address) to the object.
Types:
- Classes
- Interfaces
- Arrays
- Strings
- Enums
Examples:
// String (most common reference type)
String name = "John";
String message = "Hello, World!";
// Arrays
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob"};
// Class objects
Scanner input = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
// Default value for reference types
String str = null; // null is default for references
Primitive vs Reference Types
| Feature | Primitive | Reference |
|---|---|---|
| Storage | Actual value | Memory address |
| Memory | Stack | Heap |
| Default | 0, 0.0, false, ‘\u0000’ | null |
| Comparison | == compares values | == compares references |
| Size | Fixed | Variable |
| Examples | int, double, boolean | String, Arrays, Objects |
Example:
// Primitive - stores actual value
int a = 10;
int b = 10;
System.out.println(a == b); // true (compares values)
// Reference - stores address
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
Type Conversion
Implicit Conversion (Widening)
Automatic conversion from smaller to larger type:
byte → short → int → long → float → double
↓
char
Example:
int i = 100;
double d = i; // Automatic: int to double
System.out.println(d); // 100.0
char c = 'A';
int num = c; // Automatic: char to int
System.out.println(num); // 65 (ASCII value)
Explicit Conversion (Narrowing/Casting)
Manual conversion from larger to smaller type:
double → float → long → int → short → byte
↓
char
Example:
double d = 100.99;
int i = (int) d; // Explicit cast required
System.out.println(i); // 100 (decimal part lost)
int num = 65;
char c = (char) num; // Explicit cast
System.out.println(c); // 'A'
Common Mistakes and Errors
1. Type Mismatch
int x = 10.5; // ❌ Error: incompatible types
double x = 10.5; // ✓ Correct
int x = (int) 10.5; // ✓ Correct with casting
2. Integer Division
int result = 5 / 2; // 2 (not 2.5)
double result = 5 / 2; // 2.0 (still integer division)
double result = 5.0 / 2; // 2.5 ✓
double result = (double)5 / 2; // 2.5 ✓
3. Long Literals
long big = 10000000000; // ❌ Error: treated as int
long big = 10000000000L; // ✓ Correct
4. Float Literals
float f = 10.5; // ❌ Error: treated as double
float f = 10.5f; // ✓ Correct
5. Character vs String
char c = "A"; // ❌ Error: use single quotes
char c = 'A'; // ✓ Correct
String s = 'A'; // ❌ Error: use double quotes
String s = "A"; // ✓ Correct
Special Values
Integer Limits:
int max = Integer.MAX_VALUE; // 2,147,483,647
int min = Integer.MIN_VALUE; // -2,147,483,648
long maxL = Long.MAX_VALUE;
long minL = Long.MIN_VALUE;
Floating-Point Special Values:
double inf = Double.POSITIVE_INFINITY; // Infinity
double negInf = Double.NEGATIVE_INFINITY;
double nan = Double.NaN; // Not a Number
// Operations
double result = 1.0 / 0.0; // POSITIVE_INFINITY
double result = -1.0 / 0.0; // NEGATIVE_INFINITY
double result = 0.0 / 0.0; // NaN
Choosing the Right Data Type
Guidelines:
-
Whole numbers:
- Small (-128 to 127):
byte - Medium (-32K to 32K):
short - Default choice:
int - Very large:
long
- Small (-128 to 127):
-
Decimal numbers:
- Default choice:
double - Memory critical:
float
- Default choice:
-
Single character:
char -
Text:
String -
True/False:
boolean -
Default recommendations:
- Integers:
int - Decimals:
double - Characters:
char - Text:
String - Logic:
boolean
- Integers:
Exam Tips
Remember:
-
8 Primitive Types: byte, short, int, long, float, double, char, boolean
-
Sizes:
- byte: 1 byte (8 bits)
- short: 2 bytes
- int: 4 bytes
- long: 8 bytes
- float: 4 bytes
- double: 8 bytes
- char: 2 bytes
- boolean: ~1 bit
-
Default Values:
- Numbers: 0 (or 0.0)
- boolean: false
- char: ‘\u0000’
- References: null
-
Suffixes:
- long:
Lorl - float:
forF - double:
dorD(optional)
- long:
-
Ranges (memorize common ones):
- byte: -128 to 127
- short: -32,768 to 32,767
- int: ~-2 billion to +2 billion
-
char: Single quotes, Unicode, 16-bit
-
boolean: Only true or false (not 0/1)
-
Type Conversion:
- Widening: Automatic
- Narrowing: Requires casting
Common Questions:
- List all primitive data types
- What is the size and range of int?
- Difference between float and double
- Why use L with long?
- Can we use 0 for false in Java?
- Difference between primitive and reference types
- What is default value of int?
- Explain type conversion with examples