2

If I want to add two strings

String s1 = "abc";
String s2 = "pqr";

Here, If I used,

s1.concat(s2);

or simple add with + like

s1+s2;

Automatically String will create new object and stores the result due to its immutability, So how I can proceed with it without letting String to create new object.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Nilesh
  • 23
  • 4
  • Possible duplicate of [Immutability of Strings in Java](http://stackoverflow.com/questions/1552301/immutability-of-strings-in-java) – Turing85 Jan 11 '17 at 10:53

5 Answers5

3

You cannot because String is immutable.

Use StringBuilder or StringBuffer and their append method to build the final String. And remember that even those create Strings in constant pool.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

You cant make it without making an string object. If you want a string after appending then ans is simply NO. But when u want an StringBuffer or StringBuilder object after concating the two then u cant get it. But it will not be a type of string .

    String s1="abc";
    String s2="xyz";
    StringBuffer sb=new StringBuffer(s1);
    sb.append(s2);

while making the StringBuffer or StringBuilder object i am just passing the string in the constructor. Here no new object will be created because the said string is already on the string pool.

kaushik
  • 312
  • 1
  • 7
0

You have to either use a mutable data structure or a cache of immutable objects to return when you get a duplicate result.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

The only way to avoid creating a new String object each time you evaluate this expression is to make sure that it is a compile-time constant.

As it says in JLS Sec 15.28:

Constant expressions of type String are always "interned" so as to share unique instances

You can make use of this by declaring the strings final, and using + to concatenate them:

String s1 = "abc";
String s2 = "pqr";

// false, because s1+s2 is evaluated twice.
System.out.println((s1 + s2) == (s1 + s2));

final String s3 = "abc";
final String s4 = "pqr";

// true, because s3+s4 is a constant, and thus the compiler puts the 
// value in the constant pool.
System.out.println((s3 + s4) == (s3 + s4));

// false, because the results of method calls aren't compile-time
// constant.
System.out.println(s3.concat(s4) == s3.concat(s4));

Ideone demo

If the strings you're joining aren't compile time constants, you can't avoid creating a new string, because of String's immutability.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

You can create a String-like object that treats two strings as if they were concatenated, but without actually concatenating them or even making any copies of either string.

Note that you can't subclass String itself, as it is final, but you can implement CharSequence (which String also implements) which can be used in some API methods.

class ConcatenatedStrings implements CharSequence {
    final String s1, s2;
    public ConcatenatedString(String s1, String s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public char charAt(int index) {
        return index < s1.length ? s1.charAt(index) : s2.charAt(index);
    }

    // Don't forget to implement all the other mandatory methods...
}
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70