π Strings in Java
β¨ What is a String?
- A String is a sequence of characters (like words, sentences).
- In Java, String is a class β NOT a primitive type!
- Strings are stored in the Heap π¦ because they are objects.
π How do we create Strings?
1. Using String Literals (Fast Way)
String name = "Charlie";
"Charlie"
is stored in a special Heap area called the String Pool.- If the same String literal already exists, Java reuses it to save memory.
π§ Memory Map:
[STACK] π
-------------
| name (ref) | ---> [HEAP] π¦
-------------
| "Charlie" |
(String Pool)
-------------
2. Using new
Keyword (Forces a New Object)
String city = new String("Paris");
- Forces creation of a new String object in the Heap, even if “Paris” already exists in String Pool!
π§ Memory Map:
[STACK] π
| city (ref) | —> [HEAP] π¦
————-
| “Paris” |
(New Object)
————-
π₯ Quick Comparison
Created Like… | Memory Behavior | Faster? |
---|---|---|
String s = "abc"; | Reuses from String Pool | β Yes |
String s = new String("abc"); | Creates new object in Heap | β No |
π§ Important Notes:
- String objects are immutable β You canβt change a String once created.
- Any modification creates a new String in memory.
Example:
String a = "Hello";
a = a + " World"; // creates a new String "Hello World"
π§ Memory:
- “Hello” is still there (until garbage collected)
- “Hello World” is new!
π― Memory Fun Tip:
If you ever do a lot of String modifications (like in loops),
π use StringBuilder
(stored in Heap, mutable) for better performance!
π Final Diagram Summary
[STACK] π
-------------
| name (ref) | ---> [HEAP] π¦
| city (ref) | ---> [HEAP] π¦
-------------
|
v
[HEAP] π¦
-------------
| "Charlie" | (String Pool)
| "Paris" | (New Object)
-------------
π Quick Quiz (For you!)
Guess: (Stack π or Heap π¦)
Statement | Answer |
---|---|
int age = 30; | Stack π |
String s = "Welcome"; | Stack ref π β Heap π¦ |
String x = new String("Wow"); | Stack ref π β Heap π¦ (new object) |
π§ OCA-Level Sample Questions (with Answers!)
1οΈβ£ What will be printed?
public class Test {
public static void main(String[] args) {
String a = "cat";
String b = "cat";
String c = new String("cat");
System.out.println(a == b);
System.out.println(a == c);
}
}
Options:
- A) true true
- B) true false
- C) false true
- D) false false
β Answer: B) true false
Why?
a == b
β true β both point to same object in String Pool.a == c
β false βc
points to new object in Heap (new String("cat")
).
2οΈβ£ Where is the value 42 stored?
int age = 42;
Options:
- A) Heap
- B) Stack
- C) String Pool
- D) Garbage Collector
β Answer: B) Stack
Why?int
is a primitive β stored directly in Stack.
3οΈβ£ What is true about String objects in Java?
- A) They are mutable
- B) They are stored in Stack
- C) They are immutable
- D) They can change value without creating new objects
β Answer: C) They are immutable
Why?
Strings cannot change after creation β new Strings are made instead.
4οΈβ£ Which code will create exactly one String object?
A) String s = new String("hello");
B) String s = "hello";
C) String s1 = "hello"; String s2 = "hello";
D) String s1 = new String("hello"); String s2 = new String("hello");
β Answer: B) String s = “hello”;
Why?
"hello"
literal goes to String Pool (only one object).new String()
always creates new object, even if same text.
5οΈβ£ How many objects are created?
String a = "abc";
String b = new String("abc");
Options:
- A) 1
- B) 2
- C) 3
- D) 4
β Answer: B) 2
Why?
"abc"
literal β 1 object in String Poolnew String("abc")
β 1 more new object in Heap
Total = 2
πOCA Exam Tips for These Topics
Topic | Tip |
---|---|
Primitives | Always Stack π |
Objects (String, Dog, etc.) | Always Heap π¦ |
String literals | Go into String Pool (special Heap area) |
== operator | Compares memory addresses, NOT content |
.equals() | Compares contents of Strings |
π― Quick Practice Mini-Test (Try Yourself)
Question | Answer |
---|---|
String s = “java”; Where is “java”? | String Pool |
String s = new String(“java”); Where is the object? | Heap |
int x = 5; Stack or Heap? | Stack |
String mutable or immutable? | Immutable |