Strings

πŸ“š Strings in Java


✨ What is a String?

  • 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 BehaviorFaster?
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 πŸ“¦)

StatementAnswer
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 Pool
  • new String("abc") β†’ 1 more new object in Heap

Total = 2


πŸ“ŒOCA Exam Tips for These Topics

TopicTip
PrimitivesAlways Stack πŸ“„
Objects (String, Dog, etc.)Always Heap πŸ“¦
String literalsGo into String Pool (special Heap area)
== operatorCompares memory addresses, NOT content
.equals()Compares contents of Strings

🎯 Quick Practice Mini-Test (Try Yourself)

QuestionAnswer
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