Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Java String pool works when String concatenation?

Beware: I'm not trying to compare if the characters are equals. Because I know how to use the String.equals() method. This question is about String reference

I was studying for the OCA exam when I started to learn about the class String and it properties as immutability, etc. According to what I read or may understand about String pool is that when a string is created Java stores this object on what they call String pool and if a new string is created with the same value it is going to make reference to the string on the String pool except is the case we use the new keyword as this creates a new reference even both string contains the same value.

For example:

String a = "foo";
String b = "foo";
String c = new String("foo");
boolean ab = (a == b); // This return true
boolean ac = (a == c); // This return false

To be clear what this code is making is on the first line of code it will create the String a = "foo" and store this on the String pool, and on the second line of code it will create the String b and reference to "foo" because this already exist on the String pool. But line 3 will create a new reference of this string no matter if this already exist. Here is a little graphic example about what is happening: http://cdn.journaldev.com/wp-content/uploads/2012/11/String-Pool-Java1.png

The problem comes on the followings lines of code. When the string is created by concatenation does java make something different or simple == comparator have another behaviour ?

Example A:

String a = "hello" + " world!";
String b = "hello world!";
boolean compare = (a == b); // This return true

Example B:

a = "hello";
b = "hel" + "lo";
compare = (a == b); // This return true

Example C:

a = "Bye";
a += " bye!";
b = "Bye bye!";
compare = (a == b); // This return false

To watch the code running: (http://ideone.com/fdk6KL)

What is happening ?

EDIT

  1. Fixing error on the Example B: b = 'hel' + 'lo'

  2. Adding clarification about the problem. It's not a comparison problem cause I know the use of String.equals() the problem is on the reference on the String pool

like image 502
xsami Avatar asked May 18 '17 02:05

xsami


People also ask

How does Java string concatenation work?

concat() Methodutil package concatenates one string to the end of another string. This method returns a string with the value of the string passed into the method, appended to the end of the string. Example: Java.

How does the string pool work in Java?

How Does String pool work in Java? JVM automatically checks if the same value exists in the string constant pool or not. if yes, it occupies the already existing value. If no, it creates a new string by itself and adds it to the string pool.

What happens when you concatenate two strings in Java?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

What happens when two strings concatenate?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


2 Answers

String a = "ef";
String b = "cd" + a;
        
System.out.println("cdef"==b); // false
        
String c = "cd" + "ef";
        
System.out.println("cdef"==c); // true

when intern() method is invoked on a String object, It will try to find a String with the same sequence of characters in the pool.

if the String Pool already has a String with the same value then the reference of that String from the Pool is returned, otherwise the string is added to the pool, and the reference is returned.

String concatenation will only intern the strings if the expression is a Constant Expression

"cd" + a // is not a Constant Expression
like image 42
Yuresh Karunanayake Avatar answered Sep 29 '22 02:09

Yuresh Karunanayake


"When the string is created by concatenation does java make something different or simple == comparator have another behaviour?"

No it does not change its behavior, what happens is that:

When concatenating two string literals "a" + "b" the jvm joins the two values and then check the string pool, then it realizes the value already exists in the pool so it just simply assign this reference to the String. now in more details:

Look at the compiled bytecode below of this simple program:

public class Test  {    
    public static void main(String... args) {
        String a = "hello world!";
        String b = "hello" + " world!";
        boolean compare = (a == b);
    }
}

Simple program

First the JVM loads the string "hello world! and then push it to string pool (in this case) and then loads it to the stack (ldc = Load constant) [see point 1 in Image]

Then it assign the reference created in the pool to the local variable (astore_1) [see point 2 in Image]

Notice that the reference created in the string pool for this literal is #2 [See point 3 in Image]

The next operation is about the same: in concatenates the string, push it to the runtime constant pool (string pool in this case), but then it realizes a literal with the same content already exists so it uses this reference (#2) and assign in to a local variable (astore_2).

Thus when you do (a == b) is true because both of them are referencing to the string pool #2 which is "hello world!".

Your example C is kind of different tho, because you're using the += operator which when compiled to bytecode it uses StringBuilder to concatenate the strings, so this creates a new instance of StringBuilder Object thus pointing to a different reference. (string pool vs Object)

like image 134
Ivan Alburquerque Avatar answered Sep 29 '22 04:09

Ivan Alburquerque