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";
Here, "Java" is a string literal.
🔹 Ways to Create a String
1️⃣ Using String Literal
String s1 = "Hello";
- Stored in String Constant Pool
- Memory efficient
2️⃣ Using new Keyword
String s2 = new String("Hello");
- 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");
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";
Both s1 and s2 refer to the same object in the String Constant Pool.
But:
String s3 = new String("Java");
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
2️⃣ charAt(int index)
Returns the character at the specified index
System.out.println(s.charAt(1)); // a
3️⃣ equals(String s)
Compares content of two strings
String a = "Java";
String b = "Java";
System.out.println(a.equals(b)); // true
4️⃣ equalsIgnoreCase(String s)
Compares strings ignoring case
System.out.println(a.equalsIgnoreCase("java")); // true
5️⃣ compareTo(String s)
Compares strings lexicographically
System.out.println("Java".compareTo("Java")); // 0
6️⃣ toUpperCase()
Converts string to uppercase
System.out.println("java".toUpperCase());
7️⃣ toLowerCase()
Converts string to lowercase
System.out.println("JAVA".toLowerCase());
8️⃣ trim()
Removes leading and trailing spaces
String s = " Java ";
System.out.println(s.trim());
9️⃣ contains(CharSequence s)
Checks if string contains a sequence
System.out.println("Java Programming".contains("Java"));
🔟 startsWith(String prefix)
Checks starting characters
System.out.println("Java".startsWith("Ja"));
1️⃣1️⃣ endsWith(String suffix)
Checks ending characters
System.out.println("Java".endsWith("va"));
1️⃣2️⃣ substring(int beginIndex)
Extracts substring from index
System.out.println("Java".substring(1)); // ava
1️⃣3️⃣ substring(int begin, int end)
Extracts substring between indexes
System.out.println("Java".substring(1, 3)); // av
1️⃣4️⃣ replace(char old, char new)
Replaces characters
System.out.println("Java".replace('a', 'o'));
1️⃣5️⃣ split(String regex)
Splits string into array
String[] words = "Java is easy".split(" ");
Top comments (0)