Martlark
Martlark

Reputation: 14581

java string replace and memory

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?

Upvotes: 1

Views: 3669

Answers (5)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

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

Upvotes: 0

keerthana murugesan
keerthana murugesan

Reputation: 581

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 " "

Upvotes: 0

Oleg Estekhin
Oleg Estekhin

Reputation: 8395

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.

Upvotes: 2

Shahrukh A.
Shahrukh A.

Reputation: 1101

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()

Upvotes: 0

Nambi
Nambi

Reputation: 12042

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("[,.]","");

Upvotes: 0

Related Questions