I am trying to concatenate two strings, one string with some value and another with empty.
Example:
String string1="Great"
String string2="";
and concatenating these two string with concat function and operator
Example:
String cat=string1.concat(string2)
String operator=string1 string2
As per my understanding, while using empty string in concat function as the string2 is empty no new reference will be created. But while using operator a new reference will be created in the string pool constant. But in the below code while using the operator new reference is not created.
public class Main {
public static void main(String[] args) {
String string1="Great",string2="";
String cat=string1.concat(string2);
if(string1==cat)
{
System.out.println("Same");
}
else
{
System.out.println("Not same");
}
String operator=string1 string2;
if(operator==string1)
System.out.println("Same");
else
System.out.println("Not same");
}
}
Output:
string 1 :69066349
cat :69066349
Same
string1 :69066349
operator :69066349
Not same
From the above code, as it's using operator, the reference for the variable : operator should refer to the new memory, but it's pointing to the string1 reference. Please explain the above code.
CodePudding user response:
It is all in the documentation.
For String.concat
, the javadoc states this:
If the length of the argument string is 0, then this
String
object is returned.
For the
operator, JLS 15.8.1 states:
The result of string concatenation is a reference to a
String
object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.The
String
object is newly created (§12.5) unless the expression is a constant expression (§15.29).
As you can see, the results will be different for the case where the 2nd string has length zero and this is not a constant expression.
That is what happens in your example.
You also said:
But while using
operator a new reference will be created in the string pool constant.
This is not directly relevant to your question, but ... actually, no it won't be created there. It will create a reference to a regular (not interned) String
object in the heap. (It would only be in the class file's constant pool ... and hence the string pool ... if it was a constant expression; see JLS 15.29)
Note that the string pool and the classfile constant pool are different things.
Can I add a couple of things:
You probably shouldn't be using
String.concat
. Theconcat
for performance reasons.It is a bad idea to exploit the fact that no new object is created so that you can use
==
rather thanequals(Object)
. Your code will be fragile. Just useequals
always for comparingString
and the primitive wrapper types. It is simpler and safer.
In short, the fact that you are even asking this question suggests that you are going down a blind alley. Knowledge of this edge-case difference between concat
and
is ... pointless ... unless you are planning to enter a quiz show for Java geeks.