DEV Community

Kesavarthini
Kesavarthini

Posted on

My Java Notes: Understanding String

When working with Java programs, handling text is very common.
For example, storing names, messages, or user input.
In Java, text is handled using the String class.

In this blog, I’ll explain what a String is, how it works, and commonly used String methods in a simple way.

🔹 What Is a String in Java?

A String in Java is a sequence of characters.
In Java, strings are objects of the String class.

Example:

String name = "Java";

Enter fullscreen mode Exit fullscreen mode

Here, "Java" is a string literal.

🔹 Ways to Create a String
1️⃣ Using String Literal

String s1 = "Hello";

Enter fullscreen mode Exit fullscreen mode
  • Stored in String Constant Pool
  • Memory efficient

2️⃣ Using new Keyword

String s2 = new String("Hello");

Enter fullscreen mode Exit fullscreen mode
  • Creates a new object in heap memory

🔹 Why Strings Are Immutable?

In Java, String objects are immutable, meaning their values cannot be changed.

Example:

String s = "Java";
s = s.concat(" Programming");
Enter fullscreen mode Exit fullscreen mode

A new object is created instead of modifying the existing one.

🔹 String Constant Pool

The String Constant Pool (SCP) is a special memory area inside the heap.

When you create a string using a literal, Java first checks:

  • If the same string already exists in SCP → it reuses it
  • Otherwise → it creates a new object

Example:

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

Both s1 and s2 refer to the same object in the String Constant Pool.

But:

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

This creates a new object in heap memory.

🔹 Why SCP is important?

  • Saves memory
  • Improves performance

🔹 Advantages of String Constant Pool (SCP)

  • Stores only one copy of identical string literals
  • Saves memory by avoiding duplicate objects
  • Improves performance by reusing existing strings
  • Allows faster comparison between string literals
  • Provides better security due to immutability
  • Managed automatically by the JVM

🔻 Disadvantages of String Constant Pool (SCP)

  • Strings are immutable (cannot be modified)
  • Not suitable for frequent string modifications
  • Excessive literals may increase heap memory usage
  • Slight overhead while JVM checks SCP
  • Less flexible compared to StringBuilder and StringBuffer

🔹 Commonly Used String Methods in Java

1️⃣ length()

Returns the length of the string

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

2️⃣ charAt(int index)

Returns the character at the specified index

System.out.println(s.charAt(1));  // a

Enter fullscreen mode Exit fullscreen mode

3️⃣ equals(String s)

Compares content of two strings

String a = "Java";
String b = "Java";
System.out.println(a.equals(b));  // true
Enter fullscreen mode Exit fullscreen mode

4️⃣ equalsIgnoreCase(String s)

Compares strings ignoring case

System.out.println(a.equalsIgnoreCase("java"));  // true
Enter fullscreen mode Exit fullscreen mode

5️⃣ compareTo(String s)

Compares strings lexicographically

System.out.println("Java".compareTo("Java"));  // 0
Enter fullscreen mode Exit fullscreen mode

6️⃣ toUpperCase()

Converts string to uppercase

System.out.println("java".toUpperCase());
Enter fullscreen mode Exit fullscreen mode

7️⃣ toLowerCase()

Converts string to lowercase

System.out.println("JAVA".toLowerCase());
Enter fullscreen mode Exit fullscreen mode

8️⃣ trim()

Removes leading and trailing spaces

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

9️⃣ contains(CharSequence s)

Checks if string contains a sequence

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

🔟 startsWith(String prefix)

Checks starting characters

System.out.println("Java".startsWith("Ja"));
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ endsWith(String suffix)

Checks ending characters

System.out.println("Java".endsWith("va"));
Enter fullscreen mode Exit fullscreen mode

1️⃣2️⃣ substring(int beginIndex)

Extracts substring from index

System.out.println("Java".substring(1));  // ava
Enter fullscreen mode Exit fullscreen mode

1️⃣3️⃣ substring(int begin, int end)

Extracts substring between indexes

System.out.println("Java".substring(1, 3));  // av
Enter fullscreen mode Exit fullscreen mode

1️⃣4️⃣ replace(char old, char new)

Replaces characters

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

1️⃣5️⃣ split(String regex)

Splits string into array

String[] words = "Java is easy".split(" ");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)