1 Quick Review: Primitive vs Reference Types
Before we dive into Strings, let's quickly review what we learned in the Arrays module about data types in Java.
Store the actual value directly in the variable.
8 types: byte, short, int, long, float, double, char, boolean
Store the address (reference) to the actual data in memory.
Examples: Arrays, Strings, Objects
String is NOT a primitive type! It is a reference type. This means a String variable holds an address pointing to a String object in memory, not the actual characters themselves.
2 How is a String Stored in Memory?
Let's say we want to store "Welcome to Java" in our program. What happens in memory?
Understanding the Structure:
- The variable
messageis stored in Stack memory - It contains only the address (0x5B2F) pointing to the String object
- The actual String object is stored in Heap memory
- The String object contains a sequence of characters (char array internally)
- Each character has an index starting from 0
3 What is Immutability?
Immutable means "cannot be changed." When we say String is immutable in Java, it means:
Once a String object is created in memory, its content cannot be modified.
This is a very important concept that we will explore in more detail in future lessons about Object-Oriented Programming. For now, let's understand the basics.
Why is String Immutable?
There are several important reasons:
- String Interning (Memory Optimization) - We will learn this in the next lesson! It helps save memory by reusing String objects.
- Security - Strings are often used for sensitive data (passwords, file paths, network connections). Immutability prevents accidental or malicious modification.
- Thread Safety - In multi-threaded programs, immutable objects don't need synchronization because they can't be changed.
- Caching - The hash code of a String can be cached because it never changes.
Note for Students: We will discuss thread safety and multi-threading in more detail when we cover StringBuilder and StringBuffer. For now, just understand that immutability is a design decision that provides many benefits.
4 Visualizing Immutability: What Really Happens?
Let's see what happens when we try to "modify" a String. Consider this code:
Looking at the code, you might think: "The String s was changed from 'Java' to 'Java Programming'."
What Actually Happens:
The original String "Java" was NOT modified! Instead:
- A NEW String object
"Java Programming"was created - The variable
snow points to this new object - The old
"Java"object still exists (until garbage collected)
String s = "Java";
s = s + " Programming";
A NEW object is created!
The original String "Java" at address 0x100 was never changed. It still contains "Java"!
What changed was the reference stored in variable s - it now points to a completely new object at address 0x200.
5 The Danger: String Modification in a Loop
Now let's see why understanding immutability is critical. What happens when we modify a String inside a loop?
Each iteration creates a NEW String object. The old ones become garbage!
10 objects were created and immediately abandoned!
Imagine this loop runs 1000 times or 1,000,000 times:
- Each iteration creates a new String object
- The old String is discarded (becomes garbage)
- This wastes memory and CPU time
- The garbage collector has to clean up all these abandoned objects
Solution Preview:
In Lesson 4, we will learn about StringBuilder and StringBuffer - mutable alternatives that solve this problem efficiently!
6 Proving Immutability with Code
Let's write a simple program to prove that Strings are immutable:
Output:
original: JAVA saved: Java Same object? false
Explanation: The saved variable still contains "Java" because the original String object was never changed. The toUpperCase() method created a new String "JAVA" and original now points to that new object.
7 Summary
- String is a reference type - The variable stores an address, not the actual characters
- String is immutable - Once created, the content cannot be changed
- "Modification" creates new objects - Operations like concatenation create new String objects
- The old objects become garbage - They are eventually cleaned up by the garbage collector
- Loop concatenation is inefficient - Use StringBuilder for frequent modifications (we'll learn this later)
What's Next?
In the next lesson, we'll learn about String Interning and the String Pool - how Java uses immutability to save memory by reusing String objects!