String, StringBuffer, StringBuilder

  • Generally, String is a sequence of characters. But in java, String is an object that represents a sequence of characters. String class is used to create string object.

  • String can be defined as a ‘sequence of characters that is treated as a single data item and initialized by null’ like String x = null;

  • String class is an immutable class which is present inside java.lang package. Immutable means it cannot be changed.

  • Now, the question arises as to why the String class is immutable. Memory in Java is divided into three parts, Heap, Stack and String Pool. String is so important in java that it has its own dedicated memory location. What String pool does it that whenever a string gets repeated, it does not allocate new memory for that string. Instead, it uses the same string by making a reference to it, thereby saving memory.

  • We can create String object in two ways :

    1. String s1 = "javabykiran";
    2. String s2 = new String("javabykiran");

  • Internally, String class uses the String Pool concept. In String pool, we do not have duplicate strings.

      For example:
        String s1 = "javabykiran";
        String s2 = "javabykiran"; //will not create new instance.

      string pool in java

    In the above example, only one instance will be created. First, JVM will not find any string with the value "javabykiran" in string constant pool, so it will create a new instance. After that, for s2, it will find the string with the value "javabykiran" in the pool. This will not create a new instance but will return the reference to the same instance of s1.

  • In java, there are two equals () methods at two places. One in Object class and another in String class.

  • The equals () method of object will check reference of objects i.e. addresses. Whereas equals method of String will check contents of string with character sequence.

  • In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once string object is created, its data or state can't be changed but a new string object is created.
    Example: Consider the above example of s1 and s2. Now, String s3 = s1 + s2; // s3 will be "javabykiranjavabykiran”.

  • In this case, s1 and s2 will not change and still exist after concatenation ('+' sign is overloaded in java, if it gets digits it adds two numbers and if strings then it concatenates).

    string pool example in java

What are the advantages of String Pool?

The advantages of a String Pool are stated below:

  • Resource management
  • Not a heap memory
  • Memory efficient
  • Reusability
  • Duplicate objects are not allowed


When you create a String object without a new operator, the following task happens

  • String literal will be taken and verified in string constant pool.

  • If literal is not available in pool then a new object will be created in the pool and that address will be assigned to reference variable.

  • If string literal is available in the pool then the available object address will be assigned to reference variable.

Program - Lab1

The "==" operator in Java is used to compare two objects. It checks to see if the objects refer to the same place in memory. In other words, it checks to see if the two object names are basically references to the same memory location.

Program -Lab2

  • In java, StringBuffer is a class present in java.lang package. It provides the same functionality as String class but there are also some additional features in StringBuffer class.

  • It is slower than String as it has synchronised methods, so it is a Thread-safe class.

  • The String class is immutable, i.e objects are unmodifiable, whereas StringBuffer class provides mutable sequence of characters. A stringBuffer is like a String, but can be modified.

  • The principal operations on a StringBuffer are append() and insert() methods, which are overloaded so as to accept data of any type. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point. For example, if "s" refers to a String Buffer object whose current contents are "java", then the method call s.append("kiran") would cause the String Buffer to contain "javakiran", whereas z.insert(4, "by") would alter the String Buffer to contain “javabykiran”.

  • StringBuffer class has capacity concept. It always carries 16 extra reserve spaces for characters.
    Since the release of JDK 5, this class has been supplemented with an equivalent class designed for use by a multiple thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster as it has non-synchronised methods.

Lab Examples:

String StringBuffer
1. It is an immutable class, i.e. its objects cannot be changed.
package com;
public class AA {
public static void main(String[] args){
String s = new String("java");
1. It is a mutable class, i.e its objects can be changed only once.
package com;
public class AA {
public static void main(String[] args) {
StringBuffer sb =newStringBuffer("java");
2. String object can be created in two ways:
  1. Using new operator
  2. Without using new operator
2. StringBuffer object can be created in only one way
  1. Using new operator
3. String object will use String constant pool. 3. In StringBuffer object there is no concept of String constant pool.

The appropriate class should be used according to the given requirements. Study the points given below:

  • String class should be used when the content is fixed and will not change frequently.

  • StringBuffer should be used when the content is not fixed and frequently changes, but thread safety is required.

  • StringBuilder should be used when the content is not fixed and frequently changing and thread safety is not required.

  • String is immutable, whereas StringBuffer and StringBuilder are mutable.