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 created
String 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 object
String s3 = new String("Java"); // a distinct object on the heap
Why 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()); // 4
charAt(int index)
Returns the character at the specified index.
System.out.println(s.charAt(1)); // a
equals(String another)
Compares the content of two strings.
String a = "Java";
String b = "Java";
System.out.println(a.equals(b)); // true
equalsIgnoreCase(String another)
Compares strings ignoring case.
System.out.println(a.equalsIgnoreCase("java")); // true
compareTo(String another)
Compares strings lexicographically.
System.out.println("Java".compareTo("Java")); // 0
toUpperCase()
Converts the string to uppercase.
System.out.println("java".toUpperCase()); // JAVA
toLowerCase()
Converts the string to lowercase.
System.out.println("JAVA".toLowerCase()); // java
trim()
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")); // true
startsWith(String prefix)
Checks whether the string starts with the specified prefix.
System.out.println("Java".startsWith("Ja")); // true
endsWith(String suffix)
Checks whether the string ends with the specified suffix.
System.out.println("Java".endsWith("va")); // true
substring(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(" ");