📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. What is a String?
In Java, a string is a sequence of characters. The String class provides methods to manipulate these characters, like concatenating two strings, converting characters to uppercase, and so on.Key Features of String
2. Creating String Objects
There are two ways to create a String object:- By string literal
- By new keyword
1. Using String Literal
String s="javaguides";
String s1="javaguides";
String s2="javaguides";
//will not create new instance
2. Using a new Keyword
public static void main(String[] args) {
String str = new String("Java Guides");
// create String object using new Keyword
int length = str.length();
System.out.println(" length of the string '" + str + "' is :: " + length);
}
length of the string 'Java Guides' is:: 11
char chars[] = {
'a',
'b',
'c'
}
;
String s = new String(chars);
3. Important String Class Methods
1. length(): Finding the Length
String name = "JavaGuides";
int len = name.length();
System.out.println("Length: " + len); // Output: Length: 10
2. concat(): Concatenating Strings
String first = "Java";
String second = "Guides";
String full = first.concat(second);
System.out.println(full); // Output: JavaGuides
3. charAt(): Accessing Specific Characters
String word = "JavaGuides";
char letter = word.charAt(4);
System.out.println(letter); // Output: G
4. substring(): Extracting Substrings
String word = "JavaGuides";
String part = word.substring(4, 9);
System.out.println(part); // Output: Guide
5. toLowerCase() and toUpperCase(): Changing Case
String mixed = "JavaGuides";
System.out.println(mixed.toLowerCase()); // Output: javaguides
System.out.println(mixed.toUpperCase()); // Output: JAVAGUIDES
6. trim(): Removing Whitespace
String spaced = " JavaGuides ";
System.out.println(spaced.trim()); // Output: JavaGuides
7. replace(): Replacing Characters
String original = "JavaGuides";
String replaced = original.replace("Java", "Spring");
System.out.println(replaced); // Output: SpringGuides
8. equals(): Comparing Strings
String one = "JavaGuides";
String two = "javaguides";
boolean isEqual = one.equals(two); // false
9. indexOf() and lastIndexOf(): Finding Occurrences
String example = "JavaGuides";
int firstIndex = example.indexOf('a'); // 1
int lastIndex = example.lastIndexOf('a'); // 3
4. Why String is Immutable in Java
5. Java String Best Practices
1. Use String Literals Where Possible:
String str1 = "JavaGuides";
String str2 = "JavaGuides";
// Use equals for content comparison
if (str1.equals(str2)) { /*...*/ }
2. Avoid Concatenation in Loops:
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 100; i++) {
builder.append(i);
}
String result = builder.toString();
3. Use String Formatting:
String formatted = String.format("Order %d: %s", orderId, orderName);
4. Avoid Using == for String Comparison:
if (str1.equals(str2)) { /*...*/ }
5. Use equalsIgnoreCase for Case-Insensitive Comparison:
if (str1.equalsIgnoreCase(str2)) { /*...*/ }
6. Prefer isEmpty Over length() Check for Emptiness:
if (str.isEmpty()) { /*...*/ }
7. Use StringBuilder Over StringBuffer for Single-Threaded Operations:
8. Handle null Strings Carefully:
9. Utilize String Methods for Cleaning and Parsing:
10. Be Careful with Character Encoding:
6. Java String Best Practices - Cheat Sheet
Conclusion
Related Java String Posts
- Java Program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Check if Input String is Palindrome
- Java Program to Find all Permutations of String
- How to Remove or Trim All White Spaces from a String in Java
- How to Remove Leading and Trailing White Space From a String in Java
- Java Program to Count Duplicate Characters in a String
- Remove Character from String in Java (Java 8)
- Java Program to Count Vowels and Consonants in a String (Java 8)
- 4 Ways to Find First Non-Repeated Character in String in Java
Comments
Post a Comment
Leave Comment