DEV Community

bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 30 of My Automation Journey – Strings in Java - part 1

🧡 What is String in Java?

If you are just starting with Java, one of the most important things you’ll learn is String.
Don’t worry β€” we’ll explain this in a very simple way so that even a 10th-grade student can understand πŸ‘


πŸ“Œ 1. Introduction – What is a String?

πŸ‘‰ A String in Java is simply a sequence of characters.

Example:

String name = "Bala";
Enter fullscreen mode Exit fullscreen mode

Here:

  • "Bala" is a String
  • It contains characters: B, a, l, a

🧠 Real-Life Example

Think of a String like:

  • Your name β†’ "Ravi"
  • A sentence β†’ "I love Java"
  • A password β†’ "abc123"

πŸ‘‰ Anything inside double quotes (" ") is a String in Java.


πŸ—οΈ 2. How String Came into Java (Origin)

Java was created to be:

  • Secure πŸ”
  • Simple ✨
  • Reliable πŸ’ͺ

So, the designers of Java made Strings:

βœ… Immutable (Very Important Concept)

πŸ‘‰ Once created, a String cannot be changed

Why?

  • Security (no one can modify passwords, URLs)
  • Performance (reuse of same String)
  • Thread safety (safe in multi-threading)

🧱 3. How String is Stored in Memory

There are two ways Strings are created:


πŸ”Ή Method 1: String Literal (Recommended)

String s1 = "Hello";
String s2 = "Hello";
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Both will share the same memory

This area is called:
πŸ‘‰ String Constant Pool (SCP)


πŸ”Ή Method 2: Using new Keyword

String s3 = new String("Hello");
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This creates a new object in heap memory
πŸ‘‰ Even if "Hello" already exists


🧠 Simple Understanding

Method Memory Usage
Literal Reuses memory βœ…
new Creates new memory ❌

πŸ” 4. What is Immutability?

String s = "Hello";
s = s + " World";
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ You might think original String changed ❌
πŸ‘‰ Actually, Java creates a new String


Memory View:

  • "Hello" β†’ old object
  • "Hello World" β†’ new object

πŸ‘‰ Old one stays unchanged


πŸ’‘ Why Immutability is Important?

  • Security (used in passwords, URLs)
  • Performance (String pool reuse)
  • Safe in multi-threading

βš™οΈ 5. Common String Operations

πŸ”Ή Length

s.length();
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Convert to Uppercase

s.toUpperCase();
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Compare Strings

s1.equals(s2);
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Get Character

s.charAt(0);
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Substring

s.substring(0, 3);
Enter fullscreen mode Exit fullscreen mode

⚠️ 6. == vs equals() (Very Important)

String a = "Java";
String b = "Java";
Enter fullscreen mode Exit fullscreen mode

Using ==

πŸ‘‰ Checks memory location

Using equals()

πŸ‘‰ Checks actual value βœ…


❌ Example:

String a = new String("Java");
String b = new String("Java");

System.out.println(a == b);       // false ❌
System.out.println(a.equals(b));  // true βœ…
Enter fullscreen mode Exit fullscreen mode

🌟 7. Merits (Advantages of String)

βœ… 1. Immutable (Safe)

No accidental changes

βœ… 2. Memory Efficient

Uses String Pool

βœ… 3. Thread Safe

Safe for multiple users

βœ… 4. Easy to Use

Many built-in methods


⚠️ 8. Demerits (Disadvantages of String)

❌ 1. Cannot Modify

Every change creates new object

❌ 2. Memory Waste (if overused)

Too many objects β†’ performance issue

❌ 3. Slower for Frequent Changes

Better use:

StringBuilder or StringBuffer
Enter fullscreen mode Exit fullscreen mode

πŸ” 9. When to Use What?

Situation Use
Normal text String
Frequent changes StringBuilder
Multi-thread StringBuffer

πŸ”₯ 10. Interview Tips

πŸ‘‰ Always mention:

  • String is immutable
  • Stored in String Constant Pool
  • Difference between == and .equals()

🏁 Final Summary

πŸ‘‰ String = sequence of characters
πŸ‘‰ Immutable = cannot be changed
πŸ‘‰ Stored in String Pool (memory efficient)
πŸ‘‰ Use .equals() to compare values


πŸ’¬ Final Thought

If you understand String deeply, you’ve already covered 50% of Java fundamentals πŸ’―


Stay tuned πŸš€

πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while keeping the concepts aligned with my learning.

Top comments (0)