Home > OS >  Referece is not creating while using operator to concat two strings in java
Referece is not creating while using operator to concat two strings in java

Time:12-22

I am trying to Concatenate two strings one string with some value and another with empty. Eg:-

String string1="Great"
String string2="";

and concatenating these two string with concat function and operator Eg:-

 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 creating.

 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 we using operator the reference for the variable : operator should refer to the new memory. But its 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.


Can I add a couple of things:

  • You probably shouldn't be using String.concat. The operator is more concise, and the JIT compiler should know how to optimize away the creation of unnecessary intermediate strings ... in the few cases where you might consider using concat for performance reasons.

  • It is a bad idea to exploit the fact that no new object is created so that you can use == rather than equals(Object). Your code will be fragile. Just use equals always for comparing String 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.

CodePudding user response:

You can use this

public class StrBuilder  
{  
 /* Driver Code */  
public static void main(String args[])  
{  
    StringBuilder s1 = new StringBuilder("Hello");    //String 1  
    StringBuilder s2 = new StringBuilder(" World");    //String 2  
    StringBuilder s = s1.append(s2);   //String 3 to store the result  
        System.out.println(s.toString());  //Displays result  
}  
}  

It will concat your two strings

Output: Hello World

I hope it's helpful for you.

  • Related