My Java Notes: Understanding String
Source: Dev.to
What Is a String in Java?
A String in Java is a sequence of characters and is an object of the java.lang.String class.
Example
String name = "Java";Here, "Java" is a string literal.
Ways to Create a String
Using a String Literal
String s1 = "Hello";- Stored in the String Constant Pool (SCP)
- Memory‑efficient
Using the new Keyword
String s2 = new String("Hello");- Creates a new object on the heap, independent of the SCP
Why Strings Are Immutable?
In Java, String objects cannot be changed after they are created. Any operation that appears to modify a string actually creates a new String object.
Example
String s = "Java";
s = s.concat(" Programming"); // a new String is createdString Constant Pool (SCP)
The SCP is a special area of the heap that stores string literals. When a literal is created, the JVM checks:
- If an identical string already exists in the SCP → reuse it
- Otherwise → create a new object in the SCP
Example
String s1 = "Java";
String s2 = "Java"; // s1 and s2 reference the same objectString s3 = new String("Java"); // a distinct object on the heapWhy SCP Is Important?
- Saves memory by avoiding duplicate objects
- Improves performance through reuse of existing strings
Advantages of the String Constant Pool
- Stores only one copy of identical literals
- Reduces memory consumption
- Faster comparison of string literals (
==) - Provides better security due to immutability
- Managed automatically by the JVM
Disadvantages of the String Constant Pool
- Strings are immutable, so frequent modifications are costly
- Excessive literals can increase heap usage
- Slight overhead for JVM checks when creating strings
- Less flexible than
StringBuilderorStringBufferfor mutable operations
Commonly Used String Methods in Java
length()
Returns the length of the string.
String s = "Java";
System.out.println(s.length()); // 4charAt(int index)
Returns the character at the specified index.
System.out.println(s.charAt(1)); // aequals(String another)
Compares the content of two strings.
String a = "Java";
String b = "Java";
System.out.println(a.equals(b)); // trueequalsIgnoreCase(String another)
Compares strings ignoring case.
System.out.println(a.equalsIgnoreCase("java")); // truecompareTo(String another)
Compares strings lexicographically.
System.out.println("Java".compareTo("Java")); // 0toUpperCase()
Converts the string to uppercase.
System.out.println("java".toUpperCase()); // JAVAtoLowerCase()
Converts the string to lowercase.
System.out.println("JAVA".toLowerCase()); // javatrim()
Removes leading and trailing whitespace.
String s = " Java ";
System.out.println(s.trim()); // "Java"contains(CharSequence s)
Checks if the string contains a given sequence.
System.out.println("Java Programming".contains("Java")); // truestartsWith(String prefix)
Checks whether the string starts with the specified prefix.
System.out.println("Java".startsWith("Ja")); // trueendsWith(String suffix)
Checks whether the string ends with the specified suffix.
System.out.println("Java".endsWith("va")); // truesubstring(int beginIndex)
Returns a substring starting from beginIndex.
System.out.println("Java".substring(1)); // "ava"substring(int beginIndex, int endIndex)
Returns a substring between the specified indexes.
System.out.println("Java".substring(1, 3)); // "av"replace(char oldChar, char newChar)
Replaces occurrences of a character.
System.out.println("Java".replace('a', 'o')); // "Jovo"split(String regex)
Splits the string into an array based on the given regular expression.
String[] words = "Java is easy".split(" ");