Sunday, March 22, 2009

Java String Internals

General info about Java Strings could be found in API Doc.
Strings are immutable. So they can't be changed.

Strings store value internally in char array and have offset of the first character and characters count.
/** The value is used for character storage. */
private final char value[];

/** The offset is the first index of the storage that is used. */
private final int offset;

/** The count is the number of characters in the String. */
private final int count;

Example of initialization of empty String:
public String() {
this.offset = 0;
this.count = 0;
this.value = new char[0];
}

Strings could share the same character array. Check constructor and substring code:
// Package private constructor which shares value array for speed.
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}

public String substring(int beginIndex, int endIndex) {
...
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
}

Sample:
String str = "Hello World";
String substr = str.substring(6, 11);


Low level system implementation of String class follows Flyweight Design Pattern
/**
* Returns a canonical representation for the string object.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/

public native String intern();

Sample:
String world = "World";
String str = "Hello World";

String substr1 = str.substring(6, 11);
String substr2 = str.substring(6, 11).intern();

12 comments:

Illya Havsiyevych said...

Pool of unique strings is stored in «Perm Gen» memory

John clark said...

Hey,
Nice piece of information provided...
Is anybody aware of java courses online ? i have enrolled myself in http://www.wiziq.com/course/12145-the-6-week-complete-java-primer-with-training-certificate but i am not sure about this course so anybody has any kind of idea about this kind of course please let me know....

arumugam said...



Thank you for the info. It sounds pretty user friendly. I guess I’ll pick one up for fun. thank u






Java Courses

Kir said...

All said above are not valid for java 7 and above

vikas shukla said...

Very good post admin. Sufficient content is given here to understand every thing. For more information Please visit Java Internals

Harini R said...


thanks for shared wonderful information about giving the best information.its more useful and more helpful. great doing keep sharing
java training in chennai

sakthi said...

Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
rpa training in bangalore
best rpa training in bangalore
RPA training in bangalore
rpa course in bangalore
rpa training in chennai
rpa online training

Mounika said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
Python Online certification training
python Training institute in Chennai
Python training institute in Bangalore

SRI said...

All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training

ragini ragini said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
AWS training in chennai

AWS Training in Bangalore

siva said...

This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.
Thank you for this blog. This for very interesting and useful.
Java training in Chennai
Java training in Bangalore
Java online training
Java training in Pune
Java training in Bangalore|best Java training in Bangalore

service care said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you.
Keep update more information..
oneplus service center near me
oneplus service
oneplus service centres in chennai
oneplus service center velachery
oneplus service center in vadapalani