1

If I have a Java code segment such as:

String description = someFunctionCall();

anotherFunction( description.replace(",", " " ).replace( ".", " " ) );

How many strings get created to be garbage collected later on and how can I make multiple replaces more efficient?

4
  • if you are replacing and not storing it in any variable then no new sting will be created. Commented Feb 26, 2014 at 6:46
  • It's depends on the description Commented Feb 26, 2014 at 6:46
  • 2
    @AchintyaJha A new String object (or more) will be created whether you assign the return value or not. Commented Feb 26, 2014 at 6:47
  • You can use stringBuffer if you are making multiple string replaces. Commented Feb 26, 2014 at 6:48

5 Answers 5

2

String.replace(char, char) creates a new string only if the replacement actually happened.

If you need to replace a limited set of chars or if you need to use different replacements for different input chars then you should use StringBuilder and manually iterate over it using StringBuilder.charAt() and StringBuilder.setCharAt() methods to replace individual chars. This approach will not create any additional objects besides the StringBuilder itself and the resulting String.

You can use String.replaceAll(regex, replacement) or even precompile the regex by Pattern.compile(regex) and then reuse that Pattern object as in pattern.matcher(inputString).replaceAll(replacement). This approach will allow you to perform the replacement seemingly in one call, but it will create a lot of additional gc-eligible objects under the hood.

Sign up to request clarification or add additional context in comments.

Comments

0

String object creation depends on the replaceable character in the string.

You can use replaceAll() to replace the , and . in a single statement.

description=description.replaceAll("[,.]","");

Comments

0

You can use replaceAll(regex, replacment); function of String, which takes regex and replace all occurrence with provided string. it creates string in heaps and provide you the last string (i.e. the output)

Please refer this Difference between , java string replace() and replaceAll()

Comments

0

Once you have assigned String a value, that value can never change— it's immutable.

The good news is that while the String object is immutable, its reference variable is not, so to continue with your code:

1) The VM takes the value of description , replaces the , with " " gives us a new value. 2) Again it takes the new string description , replaces the . with " " gives us another new value.

Since Strings are immutable, the VM couldn't stuff this new value into the old String referenced by description, so it created a new String object, gave it the new value and made description refer to it.

At this point we will have 3 objects,

First one - We created Second one - , replaced with " " Third one- . replaced with " "

Comments

0

To be eficient you should use description.replace(',', ' ' ).replace( '.', ' ' ) instead. Then if there is no ',' or '.' in description no new String will be created, otherwise replace creates a new String.

description.replaceAll("[,]", " " ) will use Pattern and there will be many more objects created

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.