Frosted Cupcake
Frosted Cupcake

Reputation: 1970

Difference between concatenation at run time and compile time in java

public class First
{
    public static void main(String[] args)
    {
        String str1="Hello ",str2="World",str3="Hello World";
        System.out.println(str3==("Hello "+"World")); //Prints true
        System.out.println(str3==("Hello "+str2));  //Prints false
    }
}

The reason of the above is given in JLS-

• Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

• Strings computed by concatenation at run time are newly created and therefore distinct.

What I wanted to ask is- Why the strings which are computed at run time differ from those which are computed at compile time? Is it because of the memory allocation,one is allocated memory in heap and one in String pool or there is some other reason?Please clarify.

Upvotes: 2

Views: 1129

Answers (4)

anshul rajput
anshul rajput

Reputation: 1

The compiler recognizes that constants won't change and if you are using the + operator, will concatenate them in the compiled code. That's why in first case it will run the execution as str3==("HelloWorld") since "Helloworld" literal is already present in the string pool they both will point at the same location in the String pool it will print true .

In case of str3==("Hello"+str2),the compiler won't check that str2 has World in it, it will consider it as a variable that can have any value so at run time they will create a new string variable which point to different HelloWorld than the str3 in the string pool, so it will print false.

Upvotes: 0

xp500
xp500

Reputation: 1426

The compiler can't know what str2 contains because it would have to execute the code to know the contents of str2 when you are concatenating it with "Hello " (it could make some optimizations and inline it, since it doesn't change, but it doesn't do it).

Imagine a more complex scenario where str2 is something that a user typed in. Even if the user had typed "World" there was no way the compiler could've known that.

Therefore, it can't perform the comparison str3 == "Hello World" using the same "Hello World" from the constant pool that's assigned to str3 and used in the first comparison.

So the compiler will generate the concatenation by using StringBuilder and will end up creating another String with value Hello World, so the identity comparison will fail because one object is the one from the constant pool and the other one is the one that was just created.

Upvotes: 2

Wand Maker
Wand Maker

Reputation: 18762

Strings are immutable in Java. So, when you concatenate two strings, a third one is created at runtime to represent the concatenated value. So using == returns false as both arguments are pointing to different instances of String object.

For compile time scenario, due to compiler optimization, the concatenated string is already created, and at runtime, boht arguments of == are being represented by same instances. Hence, == returns true as both arguments point to same instance (reference).

Upvotes: 0

M. Shaw
M. Shaw

Reputation: 1742

You should use equals when comparing Objects and not the == operator.

Upvotes: 1

Related Questions