Java Strings
Java Strings
A String in Java is a sequence of characters, used for storing text.
Strings in Java are objects, not primitive data types.
Example:
✅ Creating Strings
There are two common ways to create strings:
1. Using String Literal (Recommended)
2. Using new Keyword
🧵 String Output Example
String Length
Use .length() method:
String Methods
Uppercase and Lowercase
Finding Characters
Use .indexOf() to find character or word position:
Concatenation (Joining Strings)
Method 1: Using +
Method 2: Using .concat()
Special Characters (Escape Characters)
| Character | Meaning |
|---|---|
\" | Double quote |
\\ | Backslash |
\n | New line |
\t | Tab |
Example:
Strings Are Immutable
Once created, a string cannot be changed.
Example:
Comparing Strings
Use .equals() instead of ==.
String Formatting
⭐ Complete Example
📌 Summary
| Feature | Details |
|---|---|
| Type | Non-Primitive |
| Mutable? | No (Strings are immutable) |
| Stored in | String Pool (for literals) |
| Important Methods | length(), toUpperCase(), toLowerCase(), indexOf(), concat(), equals(), contains() |
