• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Object Creation  RSS feed

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


Suppose if I execute How many Object will be created? I want to eliminate those symbols from my String Array. Tell me the good code/ efficient way.


Thanks:
Ramakrishna K.C
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:Suppose if I execute How many Object will be created?

Do you really care? Does it work?

I want to eliminate those symbols from my String Array. Tell me the good code/ efficient way.

Looks perfectly reasonable to me; and very readable.

The fact is that unless this String is a million characters long, the likelihood is that any savings you get will be measured in micro- (or possibly even nano-) seconds. However, just to satisfy your curiosity:
str = str.replaceAll("[{}\\[\\]]", "");
will do the trick as well, and may be a little quicker. Not as readable as your effort though, IMO.

Correctness and readability are generally far more important than speed.

Winston
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston

Yup, I tested that code. I got the output as I expected. It removes all the flower and square brackets. But, I din't get the answer for that object creations. How many Object it'll create, if I execute that code?

Thanks:
Ramakrishna K.C
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:How many Object it'll create, if I execute that code?

It depends. Most String methods that alter a String will return a new String unless the new String is the same as the old String in which case the old string will be returned.
You'll have to look at the source code for String (or maybe the Javadoc will tell you) if replace works this way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!