mydomain
No ADS
No ADS

Java String, StringBuffer and StringBuilder Tutorial with Examples

  1. Hierarchical Inheritance
  2. Mutable and immutable concept
  3. String
  4. StringBuffer vs StringBuilder

1. Hierarchical Inheritance

No ADS
When working with text data, Java provides you with three classes including String, StringBuffer and StringBuilder. When working with big data, you should use StringBuffer or StringBuilder to optimize the efficiency. Basically, three classes have many similarities.
  • String is immutable (More details of this concept are in the document). It does not allow the existence of subclass.
  • StringBuffer, StringBuilder are mutable.

StringBuilder and StringBuffer are alike, except for the using situation related to Multi Thread.
  • To handle the text with many threads, you should use StringBuffer in order to prevent the conflict among threads.
  • To handle the text with one thread, you should use StringBuilder.
As for the handling speed, StringBuilder is the best, following is StringBuffer and String is the worst.

2. Mutable and immutable concept

Consider an illustration:
// This is a class with value field and name field.
// When you create this class, you cannot reset the value and all other fields from outside.
// This class does not have methods for the purpose of resetting fields from outside.
// It means this class is immutable
public class ImmutableClassExample  {
   private int value;
   private String name;
   public ImmutableClassExample(String name, int value)  {
          this.value = value;
          this.name= name;
   }
   public String getName()  {
          return name;
   }
   public int getValue()  {
         return value;
   }
}
// This is a class owning a value field.
// After creating the object, you can reset values of the value field by calling setNewValue(int) method.
// This is a mutable class.
public class MutableClassExample  {
     private int value;
     public MutableClassExample(int value)  {
           this.value= value;
     }
     public void setNewValue(int newValue)  {
          this.value = newValue;
     }
}
String is a immutable class. String includes various fields like length, but values in these fields can not change

3. String

No ADS
String is one of the most important classes in Java and anyone who starts with Java programming uses String to print something on console by using famous System.out.println() statements. Many Java beginners not aware that String is immutable and final in Java and every modification in String result creates a new String object.
String is a very special class
In Java, String is a special class, the reason is that it is used on a regular basis in a program, so it must have the required performance and flexibility. That is why String have object and both primitive properties.
String is primitive:
You can create a string literal which is stored in the stack, requires less storage space, and cheaper to handle.
  • String literal = "Hello World";
You can use the + operator to concatenate 2 strings, this operator familiarity and use for the primitive data type such as int, float, double.
The literal string that is contained in a common pool. Thus the two literal strings with the same content using the same memory location on the stack, which helps save memory.
String is Object
Since String is a class, so it can be created through the new operator.
  • String object = new String ("Hello World");
The String object stored on the heap, requires to manage complexity and cost of memory storage space. Two object Strings have same content stored on two different areas of the heap memory.
For example:
// Implicit construction via string literal
String str1 = "Java is Hot";

// Explicit construction via new
String str2 = new String("I'm cool");
 
String Literal vs. String Object
As mentioned, there are two ways to construct a string: implicit construction by assigning a string literal or explicitly creating a String object via the new operator and constructor. For example,
String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // same reference
String s4 = new String("Hello");  // String object
String s5 = new String("Hello");  // String object
We used the following illustrative image to explain it:
The string literal with the same content, they will share the same storage location in the common pool. Meanwhile the String object stored in the Heap, and do not share storage locations including two string object has the same content.
equals() vs ==
equals() used to compare two objects, for String means to compare the contents of two string. For reference types == operator means comparison storage area address of the objects. Consider, for example:
String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // same reference
String s4 = new String("Hello");  // String object
String s5 = new String("Hello");  // String object

s1 == s1;         // true, same pointer
s1 == s2;         // true, s1 and s2 share storage in common pool
s1 == s3;         // true, s3 is assigned same pointer as s1
s1 == s4;         // false, different pointers
s4 == s5;         // false, different pointers in heap

s1.equals(s3);    // true, same contents
s1.equals(s4);    // true, same contents
s4.equals(s5);    // true, same contents
In fact you should use the string literal, instead of using the 'new' operator, which helps speed up your program.
The methods of the String
Below is list methods of String:
SN
Methods
Description
1
char charAt(int index)
Returns the character at the specified index.
2
int compareTo(Object o)
Compares this String to another Object.
3
int compareTo(String anotherString)
Compares two strings lexicographically.
4
int compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences.
5
String concat(String str)
Concatenates the specified string to the end of this string.
6
boolean contentEquals(StringBuffer sb)
Returns true if and only if this String represents the same sequence of characters as the specified StringBuffer.
7
static String copyValueOf(char[] data)
Returns a String that represents the character sequence in the array specified.
8
static String copyValueOf(char[] data, int offset, int count)
Returns a String that represents the character sequence in the array specified.
9
boolean endsWith(String suffix)
Tests if this string ends with the specified suffix.
10
boolean equals(Object anObject)
Compares this string to the specified object.
11
boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations.
12
byte getBytes()
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
13
byte[] getBytes(String charsetName)
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
14
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.
15
int hashCode()
Returns a hash code for this string.
16
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
17
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
18
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
19
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
20
String intern()
Returns a canonical representation for the string object.
21
int lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
22
int lastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
23
int lastIndexOf(String str)
Returns the index within this string of the rightmost occurrence of the specified substring.
24
int lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
25
int length()
Returns the length of this string.
26
boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
27
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
28
boolean regionMatches(int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
29
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
30
String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
31
String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.
32
String[] split(String regex)
Splits this string around matches of the given regular expression.
33
String[] split(String regex, int limit)
Splits this string around matches of the given regular expression.
34
boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
35
boolean startsWith(String prefix, int toffset)
Tests if this string starts with the specified prefix beginning a specified index.
36
CharSequence subSequence(int beginIndex, int endIndex)
Returns a new character sequence that is a subsequence of this sequence.
37
String substring(int beginIndex)
Returns a new string that is a substring of this string.
38
String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.
39
char[] toCharArray()
Converts this string to a new character array.
40
String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.
41
String toLowerCase(Locale locale)
Converts all of the characters in this String to lower case using the rules of the given Locale.
42
String toString()
This object (which is already a string!) is itself returned.
43
String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.
44
String toUpperCase(Locale locale)
Converts all of the characters in this String to upper case using the rules of the given Locale.
45
String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
46
static String valueOf(primitive data type x)
Returns the string representation of the passed data type argument.

4. StringBuffer vs StringBuilder

No ADS
StringBuilder and StringBuffer are very similar, the difference is that all of the StringBuffer methods are synchronized, it is appropriate when you work with multi-threaded application, multiple threads can access a StringBuffer object at the same time . Meanwhile StringBuilder have similar methods but not synchronized, but so that its performance is higher, you should use StringBuilder in single-threaded applications, or used as a local variable in a method.
The methods of StringBuffer (StringBuilder similar)
// Constructors
StringBuffer()             // an initially-empty StringBuffer
StringBuffer(int size)     // with the specified initial size
StringBuffer(String s)     // with the specified initial content

// Length
int length()

// Methods for building up the content
// type could be primitives, char[], String, StringBuffer, etc
StringBuffer append(type arg)  // ==> note above!
StringBuffer insert(int offset, type arg) // ==> note above!

// Methods for manipulating the content
StringBuffer delete(int start, int end)
StringBuffer deleteCharAt(int index)
void setLength(int newSize)
void setCharAt(int index, char newChar)
StringBuffer replace(int start, int end, String s)
StringBuffer reverse()

// Methods for extracting whole/part of the content
char charAt(int index)
String substring(int start)
String substring(int start, int end)
String toString()

// Methods for searching
int indexOf(String searchKey)
int indexOf(String searchKey, int fromIndex)
int lastIndexOf(String searchKey)
int lastIndexOf(String searchKey, int fromIndex)
StringBuilderDemo.java
package org.o7planning.tutorial.strbb;


public class StringBuilderDemo {

    public static void main(String[] args) {
       
        // Create StringBuilder object
        // with no characters in it and
        // an initial capacity specified by the capacity argument
        StringBuilder sb = new StringBuilder(10);
        
        // Append the string Hello ... on sb.
        sb.append("Hello...");
        System.out.println("- sb after appends a string: " + sb);

        // append a character
        char c = '!';
        sb.append(c);
        System.out.println("- sb after appending a char: " + sb);

        // Insert a string at index 5
        sb.insert(8, " Java");
        System.out.println("- sb after insert string: " + sb);
        
    
        // Delete substring at index 5 to 8
        sb.delete(5,8);

        System.out.println("- sb after delete: " + sb);
    }
}
Results of running the example:
- sb after appends a string: Hello...
- sb after appending a char: Hello...!
- sb after insert string: Hello... Java!
- sb after delete: Hello Java!
No ADS

Java Basic

Show More
No ADS