- It is a reference type.
- Syntax:
- String str = new String();
- String str = "Hello".
- Strings are immutable in Java.
OPERATIONS
- ‘+’ : Concatenates two strings.
- endsWith(): Checks if a string ends with a specific substring.
- length(): Retrieves the length of a string.
- indexOf(): Retrieves the index of a character in a string.
- replace(): Replaces a character in a string with another.
- toLowerCase(): Converts the string to lowercase.
- toUpperCase(): Converts the string to uppercase.
- trim(): Removes leading and trailing whitespace from the string.
When we use operations to modify a string, they create new strings based on our operation and assign the new string to our original string.
Escape Sequences
- In a String, we cannot type a backslash (\) directly. If we want to use it, we have to type another backslash (\\).
- \n represents a new line, and \t represents a tab