aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String concatenation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String concatenation" Watch "String concatenation" New topic
Author

String concatenation

Carlo Smits
Greenhorn

Joined: Jul 09, 2001
Posts: 22
In Jaworski's applet I found the following question:
//what is the output displayed by the following program?
class Question
{
public static void main(String [] args)
{
String s1 = "ab";
String s2 = "abcd";
String s3 = "cd";
String s4 = s1 + s3;
s1 = s4;
System.out.println("s1"+((s1 == s2)? "==" : "!=")+" s2");
}
}
The answer is: s1 != s2

Apparently, after concatenating s1 and s3, the reference s4 does not refer to a literal in the string pool.
However, if you replace
String s4 = s1 + s3;
with
String s4 = "ab" + "cd";
the result is: s1 == s2
Apparently, the concatenation of these two string literals again results in a reference string literal. Does anybody know what's going on?

------------------
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Carlo,
It is simply the compiler doing the work for you. In your first case, the compiler can not make any assumptions because you are using variables in the addition (references can point to any object).
The compiler assigns string literals to the "string table" which means they can be treated as constants because they are immutable.
Using that logic, in your second example the compiler can replace "ab" + "cd" with "abcd" without changing anything. Then it is easy to see that compiler created literal matches an already defined "string table" entry.
Regards,
Manfred.
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
If you use s4 = "ab" + "cd", at compiling time the compiler knows it is a string literal "abcd" and optimize to use any string literal in string pool if existed. It's importmant to know all these happen in the compiling time (It is compiler depended, See JLS 15.8.1). So if any variable is one operand or any string is generated during run time, then two string equals but not "==".


green horn
Carlo Smits
Greenhorn

Joined: Jul 09, 2001
Posts: 22
Thanks all! It get it now.
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
Hi Carlo,
Consider the following code:

After compiling, please run "javap -c X" and see the result. Here's what the compiler does when creating:
s3: compiler created a string literal "abcd"
s4:
1) compiler created a StringBuffer object,
2) load first string literal ("ab") into the StringBuffer
3) create a string literal "cd"
4) append the string literal "cd" into the StringBuffer
5) store the String as a result of StringBuffer.toString()
s5:
1) compiler created a StringBuffer object,
2) load first string literal ("ab") into the StringBuffer
3) load the second string literal ("cd") and append the string into the StringBuffer
4) store the String as a result of StringBuffer.toString()
so, if I try the following code:

it'll give me a result:
true; true; false; false;
because we're comparing the address of memory here, and not what it contains.
correct me if I'm wrong.
- eric
 
jQuery in Action, 2nd edition
 
subject: String concatenation