• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Clustering ??  RSS feed

 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

What is String Clustering in Java, where do we use what are the
advantages and disadvantages and how do we do it .

Can any body provide me an answer ; I google but could
not get anything worthwhile .


Thanks in advance

Neha
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"String clustering"? I've never heard of that. It is not a common Java term. Searching on Google for "java string clustering" also doesn't find anything.

Java does something called string pooling, which is mainly meant as a memory optimization. Is that maybe what you mean?

It works like this: When you use the same string literal more than once in your program, Java stores only one String object which is reused. It doesn't store separate String objects for each time you use the same string literal (which would be unnecessary). Class String is immutable, which means once you create a String object you can never change its contents - this is necessary to make the string pooling mechanism work without problems.

You can make your own String objects shared in the string pool by calling intern() on the string, however in practice you'll only very rarely need to use that method.
[ November 29, 2006: Message edited by: Jesper Young ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not know about String clustering.But string maintains a native pool of strings privately.

When you create a string like
String str = "abc";
First the pool is checked for a existing string with "abc" , if that is avialable then the handle is returned or else a new string is created in the pool with value "abc" and the hendle is retunred.Next time when you create a String with the same value , the same strings handle would be retuned.
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you ; it might be String pooling only .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!