2

How many object will be created in the String Constant Pool and heap for the following code :

String s1 = "Stack";  
String s2 = s1 + " Overflow";

As per my knowledge all literals are created in the String Constant pool, But the string concat operator internally uses StringBuffer to append the strings , so will an object be created in Heap also ?

trincot
  • 317,000
  • 35
  • 244
  • 286
aditi jain
  • 21
  • 1
  • 1
    As Strings are **Immutable** any appending to the existing string would result in creation of a new string with the previous value appended with the new. So its better to use a **StringBuilder**. – Saif Ahmad Nov 08 '17 at 07:11
  • 1
    To your original question, there will be three strings created, Stack, Overflow and Stack Overflow – akshaya pandey Nov 08 '17 at 07:18
  • Possible duplicate of [Questions about Java's String pool](https://stackoverflow.com/questions/1881922/questions-about-javas-string-pool) – Farhan Yaseen Nov 08 '17 at 07:20
  • @saifahmad your suggestion doesn't make any sense. – JB Nizet Nov 08 '17 at 07:35
  • @akshaya pandey Will any object be created in heap ?.. Because concat operator '+' internally uses StringBuffer to append the strings .. – aditi jain Nov 08 '17 at 11:33
  • Please go through the following link. There is a great explanation of string pools and interns along with examples and images. This is a nice tutorial to clear the concepts http://www.javamadesoeasy.com/2015/05/string-pool-string-literal-pool-string.html – akshaya pandey Nov 08 '17 at 11:41

2 Answers2

0

There two Objects will be created in String Constant Pool. As String s2 = s1 + " Overflow"; At compile time, compiler will append these two strings and only one object will be created in String Constant Pool and other object is s1.

package testPackage;
class Test {
    public static void main(String[] args) {
        String s1 = "Stack";  
        String s2 = s1 + " Overflow";
        System.out.println(s1 == "Stack");
        System.out.println(s1 == s2);
        System.out.println(s2 == "Stack Overflow");
    }
}

produces the output:

true
false
false
Mohd Yasin
  • 427
  • 1
  • 5
  • 13
0
String s1 = "Stack";

"Stack" will be in the String Constant pool.

String s2 = s1 + " Overflow";

Internally + operator uses StringBuilder for concatenating strings.

So internal implementation String s2 = s1 + " Overflow"; will be

String str = new StringBuilder(s1).append("Overflow").toString(); 

Here since it is new StringBuilder(str) so the StringBuilder object will be created in the Heap. Lets look into StringBuilder(String str) constructor

public StringBuilder(String str) {
    super(str.length() + 16);
    append(str);
} 

and super(int capacity) constructor

AbstractStringBuilder(int capacity) {
    value = new char[capacity];
}

Here we can see that the StringBuilder(String str) constructor just creating a char[] array and calling append(String str) method.

If we look at the implementation of append(String str) method of StringBuilder we can see that the append(String str) method is just playing with a char[] array and it's not creating any new object or array.

public StringBuilder append(String str) {
    super.append(str);
    return this;
} 

and the implementation of super.append(String str) is

public AbstractStringBuilder append(String str) {
    if (str == null) str = "null";
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
}

At the end lets look at the toString() method StringBuilder class.

public String toString() {
    // Create a copy, don't share the array
    return new String(value, 0, count);
}

The toString() method of StringBuilder is returning a new String which will be definitely in the Heap since it is created with new String(...);

The above explanation says that The StringBuilder will only create a new string when toString() is called on it. Until then, it maintains an char[] array of all the elements added to it.

So, the conclusion is "Stack" will be in the String Constant pool and s1 + " Overflow" i.e Stack overflow will be in the Heap.