Java String with String Buffer and String Builder Tutorial

Java String Tutorial:

We hope that you must have got a clear understanding of various concepts in Java from our Java Training series so far. In our previous tutorial, we saw about Java Exceptions in detail.

In this tutorial, we will explore about:

  • How to create a string?
  • Immutability of string
  • String methods
  • How to create String Buffer and String Builder
  • Difference between string and StringBuffer, string builder.

Here is a Video tutorial on Java String:

How to Create String?

Assigning String literals:

String s = “STH”; In this way, the compiler will check if there is any “STH” literal available in the string pool.

If it is available, then a reference will be made to String s.

Creating a new Object:

String s=new(“STH”); In this way, the new string object will be created in the heap.

Example Program:

 public class DemoString {

public static void main(String[] args) {
String s1="STH";
String s2="STH";
String s3=new String("STH");
String s4=new String("STH");

System.out.println("s1=="+System.identityHashCode(s1));
System.out.println("s2=="+System.identityHashCode(s2));
System.out.println("s3=="+System.identityHashCode(s3));
System.out.println("s4=="+System.identityHashCode(s4));
}
} 

String Example

OUTPUT:

Output- String Output

From the output, we can see, that the string created using new will have a different unique hash code though it had the same value.

Immutability of String:

A string is immutable as you cannot change the value.

Example:

 public class Immutablestring {

public static void main(String[] args) {
String str="software";
str.concat("engineering");
System.out.println(str);
}

} 

Java Immutability

From the above example, we understand that we cannot change the string value. If we perform any string operations using string methods (like concat, substring ) a new string will be created. And the original string won’t change.

String Methods

Several string methods are available to perform the string operations.

Some of the methods include:

Substring() – Returns the substring of the string.
Concat() – Concatenates the string.
Length () – Returns the length of the string
valueOf – Convert to string from other data types.

Example:

 public class StringMethods {
        
  public static void main(String[] args) {
                   int i=5;
                   String str="software";
                   String str1="tester";
                   str.concat("engineering");

System.out.println("str.length()=="+str.length());
System.out.println("str.substring=="+str.substring(2, 4));
System.out.println("String.valueOf(i)=="+String.valueOf(i));
String sf=String.valueOf(1.1);
System.out.println("sf=="+sf);
System.out.println("str.equals(str1)=="+str.equals(str1));
System.out.println(str.toUpperCase());
System.out.println("str.contains=="+str.contains("of"));
          }
} 

OUTPUT:

Output- Java String Methods

How to create StringBuffer and StringBuilder?

Both StringBuffer and StringBuilder are mutable.

StringBuffer bf=new StringBuffer();

StringBuilder sb=new StringBuilder();

Example:

 package demoofoop;

public class DemoBuffer {

public static void main(String[] args
                   StringBuffer sb =new StringBuffer("one_");
                   sb.append("two");
                   System.out.println(sb);
                   StringBuilder stb=new StringBuilder("firststring_");
                   stb.append("appendstring");
                   System.out.println("stb==="+stb);
          }
} 

OUTPUT:

Output- StringBuffer and StringBuilder in Java

Key Points to be noted:

  • A string is immutable, hence the length is fixed and you cannot change the value.
  • StringBuffer and StringBuilder value are mutable and you can change the value.
  • StringBuffer is thread safe and StringBuilder is not synchronized. Hence, it is not thread-safe.

Conclusion

In this tutorial, we learned about string, ways of creating a string, string immutability, string buffer and string builder.

Check our upcoming tutorial to know more about Threads in Java!!

PREV Tutorial | NEXT Tutorial