DEV Community

PONVEL M
PONVEL M

Posted on

Understanding String and String Methods in Java

Introduction

In Java, String is one of the most commonly used classes. It represents a sequence of characters and is widely used for handling text such as names, messages, and data processing.

Strings in Java are immutable, which means once a String object is created, it cannot be changed.


What is a String in Java?

A String is an object that represents a sequence of characters.

Example:

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

Here, "Ponvel" is stored as a String object.


Important Concept: Immutability

Strings are immutable in Java.

Example:

String str = "Hello";
str.concat(" World");

System.out.println(str);
Enter fullscreen mode Exit fullscreen mode

Output:

Hello
Enter fullscreen mode Exit fullscreen mode

Even though we tried to add " World", the original string is not changed.

Correct Way:

str = str.concat(" World");
Enter fullscreen mode Exit fullscreen mode

String Creation Methods

1. Using Literal

String s1 = "Java";
Enter fullscreen mode Exit fullscreen mode

2. Using new Keyword

String s2 = new String("Java");
Enter fullscreen mode Exit fullscreen mode

Difference: Literal vs New Keyword

Literal new Keyword
Stored in String pool Stored in heap
Memory efficient More memory used
Faster Slightly slower

Common String Methods (VERY IMPORTANT)


1. length()

Returns the length of the string.

String str = "Java";
System.out.println(str.length());
Enter fullscreen mode Exit fullscreen mode

Output: 4


2. toUpperCase() & toLowerCase()

String str = "java";

System.out.println(str.toUpperCase()); // JAVA
System.out.println(str.toLowerCase()); // java
Enter fullscreen mode Exit fullscreen mode

3. charAt()

Returns character at specific index.

String str = "Java";
System.out.println(str.charAt(1));
Enter fullscreen mode Exit fullscreen mode

Output: a


4. indexOf()

Finds position of a character.

String str = "Java";
System.out.println(str.indexOf('a'));
Enter fullscreen mode Exit fullscreen mode

Output: 1


5. substring()

Extracts part of string.

String str = "Java Programming";

System.out.println(str.substring(5));
System.out.println(str.substring(0, 4));
Enter fullscreen mode Exit fullscreen mode

Output:

Programming
Java
Enter fullscreen mode Exit fullscreen mode

🔹 6. equals() vs == (VERY IMPORTANT )

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

System.out.println(a == b);       // true
System.out.println(a == c);       // false
System.out.println(a.equals(c));  // true
Enter fullscreen mode Exit fullscreen mode

Difference: equals() vs ==

equals() ==
Compares values Compares memory
Recommended Not safe

7. contains()

Checks if string contains value.

String str = "Java Programming";
System.out.println(str.contains("Java"));
Enter fullscreen mode Exit fullscreen mode

Output: true


8. replace()

Replaces characters.

String str = "Java";
System.out.println(str.replace('a', 'o'));
Enter fullscreen mode Exit fullscreen mode

Output: Jovo


9. trim()

Removes spaces.

String str = "  Java  ";
System.out.println(str.trim());
Enter fullscreen mode Exit fullscreen mode

10. split()

Splits string into array.

String str = "Java,Python,C++";

String[] arr = str.split(",");

for(String s : arr) {
    System.out.println(s);
}
Enter fullscreen mode Exit fullscreen mode

StringBuilder vs StringBuffer vs String

Difference Table:

Feature String StringBuilder StringBuffer
Mutable No Yes Yes
Thread Safe No No Yes
Performance Slow Fast Medium

When to Use What?

  • Use String → When data is constant
  • Use StringBuilder → Fast operations (single thread)
  • Use StringBuffer → Multi-threaded applications

Interview Important Questions

Why String is Immutable?

  • Security
  • Performance (String pool)
  • Thread safety

What is String Pool?

  • Special memory area to store unique string literals

What is difference between StringBuilder and StringBuffer?

Answer:

  • Both are mutable
  • StringBuffer is thread-safe
  • StringBuilder is faster

Conclusion

Strings are a fundamental part of Java programming. Understanding how they work, especially concepts like immutability, string pool, and string methods, is essential for both coding and interviews.

Mastering String methods will help you solve many real-world problems such as:

  • Data processing
  • Validation
  • Parsing

Top comments (0)