bhavs
bhavs

Reputation: 2301

String concatenation operator

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.

Upvotes: 1

Views: 160

Answers (4)

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34061

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.

Upvotes: 2

user2277872
user2277872

Reputation: 2973

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

Upvotes: 0

Barranka
Barranka

Reputation: 21057

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)

Upvotes: 0

vishal_aim
vishal_aim

Reputation: 7854

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.

Upvotes: 6

Related Questions