Introduction
In Java, String is one of the most commonly used classes. It represents a sequence of characters and is widely used for handling text such as names, messages, and data processing.
Strings in Java are immutable, which means once a String object is created, it cannot be changed.
What is a String in Java?
A String is an object that represents a sequence of characters.
Example:
String name = "Ponvel";
Here, "Ponvel" is stored as a String object.
Important Concept: Immutability
Strings are immutable in Java.
Example:
String str = "Hello";
str.concat(" World");
System.out.println(str);
Output:
Hello
Even though we tried to add " World", the original string is not changed.
Correct Way:
str = str.concat(" World");
String Creation Methods
1. Using Literal
String s1 = "Java";
2. Using new Keyword
String s2 = new String("Java");
Difference: Literal vs New Keyword
| Literal | new Keyword |
|---|---|
| Stored in String pool | Stored in heap |
| Memory efficient | More memory used |
| Faster | Slightly slower |
Common String Methods (VERY IMPORTANT)
1. length()
Returns the length of the string.
String str = "Java";
System.out.println(str.length());
Output: 4
2. toUpperCase() & toLowerCase()
String str = "java";
System.out.println(str.toUpperCase()); // JAVA
System.out.println(str.toLowerCase()); // java
3. charAt()
Returns character at specific index.
String str = "Java";
System.out.println(str.charAt(1));
Output: a
4. indexOf()
Finds position of a character.
String str = "Java";
System.out.println(str.indexOf('a'));
Output: 1
5. substring()
Extracts part of string.
String str = "Java Programming";
System.out.println(str.substring(5));
System.out.println(str.substring(0, 4));
Output:
Programming
Java
🔹 6. equals() vs == (VERY IMPORTANT )
String a = "Java";
String b = "Java";
String c = new String("Java");
System.out.println(a == b); // true
System.out.println(a == c); // false
System.out.println(a.equals(c)); // true
Difference: equals() vs ==
| equals() | == |
|---|---|
| Compares values | Compares memory |
| Recommended | Not safe |
7. contains()
Checks if string contains value.
String str = "Java Programming";
System.out.println(str.contains("Java"));
Output: true
8. replace()
Replaces characters.
String str = "Java";
System.out.println(str.replace('a', 'o'));
Output: Jovo
9. trim()
Removes spaces.
String str = " Java ";
System.out.println(str.trim());
10. split()
Splits string into array.
String str = "Java,Python,C++";
String[] arr = str.split(",");
for(String s : arr) {
System.out.println(s);
}
StringBuilder vs StringBuffer vs String
Difference Table:
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutable | No | Yes | Yes |
| Thread Safe | No | No | Yes |
| Performance | Slow | Fast | Medium |
When to Use What?
- Use String → When data is constant
- Use StringBuilder → Fast operations (single thread)
- Use StringBuffer → Multi-threaded applications
Interview Important Questions
Why String is Immutable?
- Security
- Performance (String pool)
- Thread safety
What is String Pool?
- Special memory area to store unique string literals
What is difference between StringBuilder and StringBuffer?
Answer:
- Both are mutable
- StringBuffer is thread-safe
- StringBuilder is faster
Conclusion
Strings are a fundamental part of Java programming. Understanding how they work, especially concepts like immutability, string pool, and string methods, is essential for both coding and interviews.
Mastering String methods will help you solve many real-world problems such as:
- Data processing
- Validation
- Parsing
Top comments (0)