• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java ReplaceAll  RSS feed

 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know the memory cost of this operation in Java 1.5?

If the following:

test.replaceAll("test", "1");

runs on:

test test test test test test

Can anyone tell me how many objects are created to end up with?

1 1 1 1 1 1

Thanks,

Luke
 
Calvin Smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can look in the source code yourself and figure it out.

It uses a java.util.regex.Matcher internally, and that is fairly efficient since it uses a StringBuffer to accumulate the result with appendReplacement and appendTail; but on the other hand, it needs to create the pattern and the matcher everytime if you just use the string.replaceAll, rather than compiling the pattern first and reusing it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!