On this page, you will find all the tutorials, guides, and examples on Java String, StringBuilder, and StringBuffer.
In Java, String, StringBuilder, and StringBuffer are used to represent and manipulate sequences of characters.
String
Immutable: Once a String object is created, its value cannot be changed. Any modification to the string results in a new object.
Syntax: Strings can be created using string literals or the new keyword.
Performance: Since strings are immutable, repeated modifications can be less efficient as new objects are created for each change.
Thread Safety: Immutable objects are inherently thread-safe, so no synchronization is required.
Example:
String str = "Hello, World!";
StringBuilder
Mutable: Unlike String, StringBuilder is mutable, meaning that its content can be changed without creating a new object.
Syntax: StringBuilder objects must be explicitly created using the new keyword.
Performance: Since it's mutable, StringBuilder is generally more efficient for repeated modifications to the same object.
Thread Safety: It is not synchronized, making it not thread-safe. It's suitable for single-threaded scenarios.
Example:
StringBuilder builder = new StringBuilder("Hello");
builder.append(", World!");
StringBuffer
Mutable: Like StringBuilder, StringBuffer is also mutable.
Syntax: StringBuffer objects must be explicitly created using the new keyword.
Performance: Slightly slower than StringBuilder due to synchronization.
Thread Safety: It is synchronized, making it thread-safe. If you need to manipulate strings across multiple threads, StringBuffer might be the preferred option.
Example:
StringBuffer buffer = new StringBuffer("Hello");
buffer.append(", World!");
Java String Blog Posts, Tutorials, and Examples
- Java String: A Guide to String Basics, Methods, Immutability, Performance, and Best Practices
- Java String Class API Guide - Covers all the String Methods
- When to Use String, StringBuffer, and StringBuilder in Java
- String vs StringBuilder vs StringBuffer in Java
- Best Way to Reverse a String in Java
- Guide to Java String Constant Pool
- Guide to String Best Practices in Java (Best Practice)
- String Special Operations with Examples
- String Comparison Methods with Examples
- String Methods for Character Extraction
- String Searching Methods with Examples
- String Modifying Methods with Examples
- Java 8 StringJoiner Class
Java StringBuffer Blog Posts, Tutorials, and Examples
- Java StringBuffer: Methods, Examples, and Performance Tips
- Java StringBuffer Class API Guide
- String vs StringBuffer in Java with Example (Performance Analysis)
Java StringBuilder Blog Posts, Tutorials, and Examples
Interview Preparation:
Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a 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 a String is Empty or Null
- Java Program to Find Maximum Occurring Character in String
- Java Program To Count the Occurrences of Each Character Using HashMap
- Java Program to Add Characters to a String
- Java Program to Reverse a List
- Java Program to Find the Occurrence of Words in a String using HashMap
Java 8 Programs on String with Output
- Java 8 Program to Find the Frequency of Each Character in a Given String
- Java 8 Program to Retrieve Last Element of a List of Strings
- Java 8 Program to Reverse Each Word of String
- Java 8 Program to Sort List of Strings Alphabetically
- Java 8 Program To Count Characters in a String
- Java 8 Program To Find First Repeated Character in a String
- Java 8 Program To Reverse a String
Java String Conversion Examples
Conversion from String to Wrapper classes or Primitive types
- Java Convert Integer to String Example
- Java Convert Float to String Example
- Java Convert Double to String Example
- Java Convert Short to String Example
- Java Convert Long to String Example
- Java Convert Character to String Example
- Java Convert Byte to String Example
- Java Convert Boolean to String Example
Java String Class Methods
TheString
class in Java is one of the most widely used classes. It provides a variety of methods for manipulating strings, such as comparing, searching, and modifying strings. Understanding these methods is crucial for efficient string manipulation in Java.
- Java String valueOf() Method
- Java String charAt() Method
- Java String concat() Method
- Java String endsWith() Method
- Java String equals() Method
- Java String getBytes() Method
- Java String indexOf() Method
- Java String isEmpty() Method
- Java String lastIndexOf() Method
- Java String replace() Method
- Java String split() Method
- Java String startsWith() Method
- Java String substring() Method
- Java String toLowerCase() Method
- Java String trim() Method
- Java String codePointAt() Method
- Java String codePointBefore() Method
- Java String compareToIgnoreCase() Method
- Java String contentEquals() Method
- Java String regionMatches() Method
- Java String replaceAll() Method
- Java String replaceFirst() Method
- Java String subSequence() Method
- Java String toCharArray() Method
- Java String strip() Method
- Java String stripLeading() Method
- Java String stripTrailing() Method
- Java String isBlank() Method
- Java String lines() Method
- Java String repeat() Method
- Java String indent() Method
- Java String transform() Method
- Java String describeConstable() Method
- Java String resolveConstantDesc() Method
- Java String formatted() Method
- Java String stripIndent() Method
- Java String copyValueOf() Method
- Java String intern() Method
- Java String join() Method
- Java String matches() Method
- Java String offsetByCodePoints() Method
StringBuilder Class Methods
- Java StringBuilder append() Method
- Java StringBuilder capacity() Method
- Java StringBuilder charAt() Method
- Java StringBuilder chars() Method
- Java StringBuilder codePointAt() Method
- Java StringBuilder codePointBefore() Method
- Java StringBuilder codePointCount() Method
- Java StringBuilder codePoints() Method
- Java StringBuilder delete() Method
- Java StringBuilder deleteCharAt() Method
- Java StringBuilder ensureCapacity() Method
- Java StringBuilder getChars() Method
- Java StringBuilder indexOf() Method
- Java StringBuilder insert() Method
- Java StringBuilder lastIndexOf() Method
- Java StringBuilder length() Method
- Java StringBuilder repeat() Method
- Java StringBuilder replace() Method
- Java StringBuilder reverse() Method
- Java StringBuilder setLength() Method
- Java StringBuilder subSequence() Method
- Java StringBuilder substring() Method
- Java StringBuilder trimToSize() Method
- Java StringBuilder toString() Method
StringBuffer Class Methods
- Java StringBuffer append() Method
- Java StringBuffer capacity() Method
- Java StringBuffer charAt() Method
- Java StringBuffer chars() Method
- Java StringBuffer codePointAt() Method
- Java StringBuffer codePointBefore() Method
- Java StringBuffer codePointCount() Method
- Java StringBuffer codePoints() Method
- Java StringBuffer compareTo() Method
- Java StringBuffer delete() Method
- Java StringBuffer deleteCharAt() Method
- Java StringBuffer ensureCapacity() Method
- Java StringBuffer getChars() Method
- Java StringBuffer indexOf() Method
- Java StringBuffer insert() Method
- Java StringBuffer lastIndexOf() Method
- Java StringBuffer length() Method
- Java StringBuffer repeat() Method
- Java StringBuffer replace() Method
- Java StringBuffer setLength() Method
- Java StringBuffer reverse() Method
- Java StringBuffer subSequence() Method
- Java StringBuffer substring() Method
- Java StringBuffer trimToSize() Method
- Java StringBuffer toString() Method
Comments
Post a Comment
Leave Comment