Strings
Strings
Definition:
A String in Java represents a sequence of characters. It is a class in the java.lang
package. Strings are immutable, meaning once a String object is created, its content cannot be
changed.
String Creation:
Using string literal:
String str1 = "Hello";
This creates a new string object in the heap, even if an identical string exists in the pool.
CharSequence Interface
The CharSequence interface represents a readable sequence of char values. Common classes
that implement CharSequence include String, StringBuffer, and StringBuilder.
Methods in CharSequence interface:
1. length(): Returns the length of the sequence.
2. charAt(int index): Returns the character at the specified index.
3. subSequence(int start, int end): Returns a new CharSequence from the sequence.
Class String
The String class is part of the java.lang package, and it represents a sequence of characters.
Key points:
String objects are immutable.
The String class implements Serializable, Comparable<String>, and CharSequence interfaces.
Modifying Strings
Since String objects are immutable, Java provides other classes like StringBuffer and
StringBuilder to modify strings.
Common string modification methods:
1. replace(): Replaces all occurrences of a substring.
2. concat(): Concatenates two strings.
3. toUpperCase() / toLowerCase(): Converts the string to upper or lower case.
Searching in Strings
1. int indexOf(int ch): Finds the first occurrence of the specified character.
2. int indexOf(String str): Finds the first occurrence of the specified substring.
3. lastIndexOf(): Returns the index of the last occurrence of a substring.
Class StringBuffer
StringBuffer is a mutable sequence of characters.It can be modified after creation, unlike String.
Common methods:
append(): Adds data to the end of the StringBuffer.
insert(): Inserts data at a specified index.
delete(): Deletes a sequence of characters.
reverse(): Reverses the sequence of characters.
*************************************************************************************************************
*************************************************************************************************************
*************************************************************************************************************
String Pool:
String Pool is a special memory area in the heap used to store string literals.
If two string literals are the same, they share the same reference from the string pool.
Immutability of Strings:
Once a string is created, its value cannot be changed. Modifying operations return a new string.
String s = "Java";
s.concat(" Programming"); // Creates a new string "Java Programming"
System.out.println(s); // Outputs "Java", not "Java Programming"
Common String Methods:
Length of a string:
Substring:
Concatenation:
Equality Check:
Replace Characters:
String Comparison:
compareTo(): Lexicographically compares two strings.
int result = str1.compareTo(str2);
// Returns 0 if both strings are equal, a negative number if str1 is lexicographically less, and a
positive number if greater.
String Interning:
The intern() method forces a string to refer to the string pool.
String Format:
Format strings using String.format(), similar to printf-style formatting.
String Splitting:
Split a string into an array based on a delimiter:
String[] parts = str.split(" ");
StringBuffer class:
StringBuffer class is used to create mutable(modifiable) string objects.The StringBuffer class is
same as String class except StringBuffer class is mutable i.e., it can be changed.
Note: Java StringBuffer class is thread-safe i.e. multiple threads cannot access it
simultaneously. So it is safe and will result in an order.
Mutable Strings:
A String that can be modified or changed is known as mutable String. StringBuffer and
StringBuilder classes are used for creating mutable strings.
Constructor Description