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() |
