1

Anyone to throw right on the code and its output ?

public class HelloWorld { public static void main(String[] args) { String s1 = "Hello World"; String s6 = "Hello" + " World"; String s7 = "Hello"; String s8 = " World"; String s9 = s7 + s8; String s10 = s7 + " World"; System.out.println(s1==s6); System.out.println(s1==s9); System.out.println(s1==s10); System.out.println(s9==s10); } } 

Output :

true false false false 

I understand that s1 is created in the string constant pool. I am wondering how the creation of s6, s9 and s10 due to the use of the concatenation operator.

3
  • 2
    Why are you comparing strings with ==? Commented Apr 30, 2013 at 4:31
  • 4
    s1 and s6 are compile-time constants (which is actually the same "Hello World" that belongs to the class HelloWorld), but others aren't (although they are added to the literal pool after creation at runtime). Commented Apr 30, 2013 at 4:31
  • @squiguy I came across this example code in a blog and I am trying to understand why the output is the way it is :), I would never use == to compare 2 strings Commented Apr 30, 2013 at 5:54

4 Answers 4

6

there is a lot of optimization done at compile time. So if you look at the de-compiled code after compilation you'll see like :

 String s1 = "Hello World"; String s6 = "Hello World"; String s7 = "Hello"; String s8 = " World"; String s9 = (new StringBuilder(String.valueOf(s7))).append(s8).toString(); String s10 = (new StringBuilder(String.valueOf(s7))).append(" World").toString(); System.out.println(s1 == s6); System.out.println(s1 == s9); System.out.println(s1 == s10); System.out.println(s9 == s10); 

And it explains it easily. So it's compiler behind the scene, who is doing a lot of work for you.

When we concatenate the string literals, it replaces it with a single literal, when we use concatenation with variables, it uses StringBuilder.

Sign up to request clarification or add additional context in comments.

Comments

2

The compiler optimizes this:

String s6 = "Hello" + " World"; 

to this:

String s6 = "Hello World"; 

So s1 and s6 refer to the same object. You might wish for the compiler to do similar compile-time optimizations for the other strings. If you declare s7 and s8 as final, it will:

public class HelloWorld { public static void main(String[] args) { String s1 = "Hello World"; String s6 = "Hello" + " World"; final String s7 = "Hello"; final String s8 = " World"; String s9 = s7 + s8; String s10 = s7 + " World"; System.out.println(s1==s6); System.out.println(s1==s9); System.out.println(s1==s10); System.out.println(s9==s10); } } 

produces:

 true true true true 

It boils down to the compiler being willing to concatenate compile-time constants. It doesn't do static code analysis to determine that s7 and s8 won't have changed before the lines where you declare s9 and s10. Declaring them as final gives it the information that static code analysis theoretically could.

Comments

0

Strings must not be compared using the == operator, but the compareTo() (or compareToIgnoreCase()) method.

... String str01 = "abc"; ... System.out.println(str01.compareTo("abc") == 0); // Will print 'true' System.out.println(str01.compareTo("a" + "b" + "c") == 0); // Will also print 'true' System.out.println(str01.compareTo("ABC") == 0); // Will print 'false' 

'compareTo()' will return an integer that will be zero if the strings are equal, or a number different from zero in any other case. Check: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)

2 Comments

using str01.equals("abc") might be better in this case
@user908821 well, you are right... I like to use compareTo() because it is easier to compare bare integers than strings in java... and because it trained me on implementing the comparable interface
0

They are all saying "Hello World"

s6 = the two strings "Hello" and "World" to be combined.

The use of concatenation is to add strings together in the Java programming language. It is so that you can have multiple lines, and move text throughout a bunch of lines.

For example:

 System.out.println("I enjoy" + "This nice " + "weather!"); 

will produce "I enjoyThisniceweather!

The reason there are no spaces is because when it concatenates, it doesn't do anything, but ignore the '+' sign, and put the "" together to make one big sentence, or String.

for s6, it is saying to add the 2 Strings of "Hello" and " World", meaning it will end up as "Hello World" at the end.

for s9, it is taking the String vars s7 & s8 and concatenating them together. So, when ran, java will read s7 and say "okay, s7 holds the String "Hello" and s8 holds the String " World", and I will put them together creating "Hello World".

It is merely the same thing for s10. It takes s7 String and adds onto the end of it " World".

Hope this helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.