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

29 comments:

Illya Havsiyevych said...

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

Unknown 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....

Kir said...

All said above are not valid for java 7 and above

Unknown said...

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

Unknown 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

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

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

Training for IT and Software Courses said...

Thank you for valuable information.I am privilaged to read this post.aws training in bangalore

Training for IT and Software Courses said...

Congratulations This is the great things. Thanks to giving the time to share such a nice information.best Mulesoft training in bangalore

Johan said...

3) Thank you for sharing .The data that you provided in the blog is informative and effective.mulesoft training in bangalore

Ecare Technologies said...

Really nice post. Thank you for sharing amazing information...
RPA Training in Bangalore - Marathahalli | RPA Training Institutes | RPA Course Fees and Content | RPA Interview Questions
- ECare Technologies located in Marathahalli - Bangalore, is one of the best RPA Training institutes with 100% Placement support. RPA Training in Bangalore provided by RPA Certified Experts and real-time Working Professionals with handful years of experience in real-time RPA Projects.

Anonymous said...

python training in bangalore | python online training
aws training in bangalore | aws online training
artificial intelligence training in bangalore | artificial intelligence online training
machine learning training in bangalore | machine learning online training
blockchain training in bangalore | blockchain training in bangalore
uipath training in bangalore | uipath online training

Training for IT and Software Courses said...

After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.

Machine Learning certification Online Training in bangalore

Machine Learning certification courses in bangalore

Machine Learning certification classes in bangalore

Machine Learning certification Online Training institute in bangalore

Machine Learning certification course syllabus

best Machine Learning certification Online Training

Machine Learning certification Online Training centers

Training for IT and Software Courses said...

This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me.

Teradata Online Training

Teradata Classes Online

Teradata Training Online

Online Teradata Course

Teradata Course Online

Buy Seo Service said...

The content of this website was really informative. 50 High Quality Backlinks for just 50 INR
2000 Backlink at cheapest
5000 Backlink at cheapest
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest
Boost DA upto 35+ at cheapest
Boost DA upto 45+ at cheapest

ram said...

It was wonderfull reading your article. Great writing styleIamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder

IamLinkfeeder said...

David Forbes is president of Alliance Marketing Associates IncIamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder

Thakur98 said...

We are used to the fact that we know only religious and public holidays and celebrate only them.Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder Iamlinkfeeder

Himachali Khabarnama said...

Annabelle loves to write and has been doing so for many years.Backlink Indexer My GPL Store Teckum-All about Knowledge

Tec said...

K.G.F: Chapter 2 : Directed by Prashanth Neel. With Yash, Sanjay Dutt, Raveena Tandon, Prakash Raj. The blood-soaked land of Kolar Gold Fields

Himachal News Network said...

Welcome to CapturedCurrentNews – Latest & Breaking India News 2021
Hello Friends My Name Anthony Morris.latest and breaking news

Valentines Day Wishes 2021 said...

thank you for sharing best opinion following us. i at the back of your say and all you share in the back of us is uptodate and quite informative, i'd in imitation of to bookmark the page suitably i can come right here once more to retrieve you, as you have got finished a terrifi process.! https://crackdj.com/windows-7-ultimate-product-key/

Beel SEO EXPORT said...

I in plan of fact respect the fine of subjects you proclaim here. thanks for sharing us a on your liking opinion that is certainly obliging. good hours of day!! Microsoft Office 2010 Free Download With Crack

cyber pc said...

Its a cozy delight reading your claim.Its full of aspire i am looking for and i lionize to claim a comment that "The content material of your proclaim is incredible" extremely good accomplish..... https://wishesquotz.com/happy-memorial-day-quotes/

Aquaman said...

Step-by-Step Hacking Tutorials about WiFi hacking,
Kali Linux, Metasploit, exploits, ethical hacking, information security, malware analysis and scanning
hacking Tutorial

hubert said...

Thanks for this great software for sharing any data easily from one pc to another pc.
https://thepcsoft.com/anydesk-crack-latest/

srikanth digital said...

I really enjoyed reading your post! It was not only informative but also helped me enhance my knowledge with the latest information. Please continue blogging. Your article is well-written and contains valuable information that is beneficial for readers. Thank you for sharing it, and I look forward to reading more posts like this from you.

Top CEC colleges in Hyderabad

srikanth digital said...

I strongly believe in leaving comments on websites to inform blog writers that they have contributed something valuable to the internet, Thanks for sharing.

Colleges In Hyderabad For B.Com

iteducationcentre said...

Great Post. Thanks for sharing.
also, check Java course in Pune