Introduction
When fields are declared but not explicitly initialized, Java automatically assigns default values based on the data type.
Default Values by Type
| Data Type | Default Value |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| char | ’\u0000’ (null character) |
| boolean | false |
| Reference (Object) | null |
Example
class Student {
// Fields not initialized
int rollNo; // Default: 0
String name; // Default: null
double marks; // Default: 0.0
boolean isPresent; // Default: false
char grade; // Default: '\u0000'
void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
System.out.println("Marks: " + marks);
System.out.println("Present: " + isPresent);
System.out.println("Grade: " + grade);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.display();
}
}
Output:
Roll No: 0
Name: null
Marks: 0.0
Present: false
Grade: (empty/null character)
Numeric Types
class Numbers {
byte b; // 0
short s; // 0
int i; // 0
long l; // 0L
float f; // 0.0f
double d; // 0.0d
void show() {
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}
Reference Types
class Example {
String str; // null
int[] arr; // null
Student student; // null
void show() {
System.out.println("String: " + str);
System.out.println("Array: " + arr);
System.out.println("Student: " + student);
}
}
Output:
String: null
Array: null
Student: null
Local Variables vs Fields
Fields (Automatically Initialized):
class Example {
int x; // Automatically initialized to 0
void display() {
System.out.println(x); // ✓ OK: prints 0
}
}
Local Variables (NOT Automatically Initialized):
class Example {
void display() {
int x; // NOT initialized
// System.out.println(x); // ❌ Error: variable might not be initialized
int y = 0; // Must initialize manually
System.out.println(y); // ✓ OK
}
}
Explicit vs Default Initialization
class Product {
// Default initialization
String name; // null
double price; // 0.0
int quantity; // 0
// Explicit initialization
String category = "General"; // "General"
boolean available = true; // true
double discount = 0.0; // 0.0
void display() {
System.out.println("Name: " + name); // null
System.out.println("Price: " + price); // 0.0
System.out.println("Quantity: " + quantity); // 0
System.out.println("Category: " + category); // "General"
System.out.println("Available: " + available); // true
}
}
Order of Initialization
class InitOrder {
// 1. Default initialization
int x; // 0 (default)
// 2. Explicit initialization
int y = 10; // 10
// 3. Constructor initialization
int z;
InitOrder() {
z = 20; // 20
}
void show() {
System.out.println("x: " + x); // 0
System.out.println("y: " + y); // 10
System.out.println("z: " + z); // 20
}
}
Order:
- Default values assigned
- Explicit initialization
- Constructor execution
Array Fields
class ArrayExample {
int[] numbers; // null (array reference)
String[] names; // null
void initialize() {
numbers = new int[5]; // Creates array
// Elements: [0, 0, 0, 0, 0] (default int values)
names = new String[3]; // Creates array
// Elements: [null, null, null] (default String values)
}
void display() {
System.out.println("Numbers: " + (numbers == null));
if (numbers != null) {
for (int num : numbers) {
System.out.print(num + " ");
}
}
}
}
Static vs Instance Fields
class Example {
static int staticVar; // 0 (default)
int instanceVar; // 0 (default)
static {
// Static initialization block
staticVar = 100;
}
{
// Instance initialization block
instanceVar = 200;
}
}
Practical Example
class BankAccount {
String accountNumber; // null
String holderName; // null
double balance; // 0.0
boolean isActive; // false
// Constructor to set proper values
BankAccount(String accountNumber, String holderName) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.balance = 0.0; // Explicit, but same as default
this.isActive = true; // Different from default
}
void display() {
System.out.println("Account: " + accountNumber);
System.out.println("Holder: " + holderName);
System.out.println("Balance: " + balance);
System.out.println("Active: " + isActive);
}
}
public class Main {
public static void main(String[] args) {
// Without constructor
BankAccount acc1 = new BankAccount();
acc1.display(); // All default values
// With constructor
BankAccount acc2 = new BankAccount("1234567890", "John");
acc2.display(); // Initialized values
}
}
Best Practices
1. Don’t Rely on Defaults:
// Bad
class Student {
int rollNo; // Relies on default 0
}
// Good
class Student {
int rollNo = 0; // Explicit initialization
}
2. Initialize in Constructor:
class Student {
String name;
int rollNo;
Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
}
Quick Reference
class Example {
// Primitives - default values
int i; // 0
double d; // 0.0
boolean b; // false
char c; // '\u0000'
// References - null
String s; // null
int[] arr; // null
Object obj; // null
// Explicit initialization
int x = 10; // 10 (not default)
String str = "Hi"; // "Hi" (not null)
}
Exam Tips
Remember:
- Fields automatically initialized
- Local variables NOT automatically initialized
- Numeric types: 0
- boolean: false
- char: ‘\u0000’
- References: null
- Arrays: null (reference), elements get defaults after creation
- Explicit initialization overrides defaults
- Constructor runs after default initialization
- Always initialize explicitly for clarity
Common Questions:
- What are default values?
- Default value of int?
- Default value of String?
- Default value of boolean?
- Difference between field and local variable initialization?
- What is default value of object reference?
- Order of initialization?