The String class in Java represents a sequence of characters and belongs to the java.lang package. It provides built-in methods for comparing, searching, extracting, and manipulating text.
- String objects are immutable and safe to share across threads.
- String literals are stored in the String Pool to enable object reuse.
Example: Creating and manipulating a String.
class GFG{
public static void main(String[] args){
String str = "Geeks";
System.out.println(str.concat("forGeeks"));
System.out.println(str);
}
}
Output
GeeksforGeeks Geeks
Explanation: The concat() method returns a new String without modifying the original String.
Key Features of the String Class
- Supports safe sharing of String objects across multiple threads.
- Declared as a final class, preventing inheritance.
- Supports lexicographical comparison through Comparable<String>.
- Implements CharSequence, Serializable, Constable and ConstantDesc.
- Supports Unicode text representation using UTF-16 semantics.
String Constructors
String constructors allow strings to be created from character arrays, byte arrays, Unicode code points and existing character sequences.
Example: Creating strings using different constructors.
import java.nio.charset.StandardCharsets;
class GFG{
public static void main(String[] args){
char[] ch = {'J', 'a', 'v', 'a'};
byte[] b = {65, 66, 67};
String s1 = new String(ch);
String s2 = new String(b, StandardCharsets.UTF_8);
String s3 = new String(new StringBuilder("Geeks"));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
Output
Java ABC Geeks
Explanation: The program creates strings from a character array, a byte array using UTF-8 encoding, and a StringBuilder object.
Common String Constructors
The String class provides constructors to create strings from character arrays, byte arrays, Unicode code points, and other string-related objects.
Constructor | Description |
|---|---|
Creates an empty string. | |
Creates a new String with the same character sequence as the specified string. | |
| Creates a string from a character array. |
| Creates a string from a specified portion of a character array. |
| Creates a string by decoding bytes using the default charset. |
| Creates a string by decoding bytes using the specified charset. |
| Creates a string from a specified range of Unicode code points. |
| Creates a string from a StringBuffer object. |
| Creates a string from a StringBuilder object. |
Categories of String Methods
The String class provides methods for different operations, including comparison, searching, extraction, modification, and conversion.
Methods | Description |
|---|---|
Returns the string length and checks whether it is empty or contains only whitespace. | |
charAt(), codePointAt() | Accesses a character or its Unicode code point at a specified index. |
Compares strings based on their contents or lexicographical order. | |
indexOf(), lastIndexOf(), contains(), startsWith(), endsWith() | Searches for characters or substrings and checks string prefixes and suffixes. |
substring(), subSequence() | Extracts a specified portion of a string. |
concat(), replace(), replaceAll(), repeat() | Creates new strings by joining, replacing, or repeating character sequences. |
toUpperCase(), toLowerCase() | Converts characters into uppercase or lowercase. |
trim(), strip(), stripLeading(), stripTrailing() | Removes whitespace from both ends or a specified side of a string. |
Splits a string into an array or joins multiple character sequences using a delimiter. | |
valueOf(), toCharArray(), getBytes() | Converts values or strings into string representations, character arrays, or byte arrays. |
Creates formatted strings using format specifiers and supplied arguments. |