• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

All About Java Strings

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a little knowledge about the usage of string, manipulating it etc.
I am looking for a good tutorial on the internal implementation of a string, its memory management and much more about it which I am not aware of.

I have googled for the same, I did get bits and pieces information from variuos sites. Looking for a more elaborate ones.

Any pointers would be greatly appreciated.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lopez,
On a high level, a String is an array of characters with methods around it. It's immutable (can't change once created) so there is no internal memory management. There's also a String pool so Java doesn't waste memory creating the same String over and over.

If you have some experience with Java, you could read the implementation of the String class. If not, you can just use the beginner tutorials for now and come back to this when you know more.
 
Ranch Hand
Posts: 80
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable (Value Cannot be modified)

But still we can change the reference of String

i.e String s = "abc"; // s refer to an object "abc" in pool memory
s = s.concat("d"); // s refer to previous reference variable in pool and creates new s
S.o.p(s); // it prints new s reference variable

Output : abcd



But The Original reference variable 's' is not changed but no longer refrenced in a pool (LOST)

Hope You Got this basic.
 
Lopez Mirinda
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you every one for providing insight into Sting class and how it works.

Came across this public native String intern(); in the String class. Where and when is it used?
the doc days "All literal strings and string-valued constant expressions are * interned." what does it actually mean? and since it is public method, where can it actually be used programatically?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We recently had a topic on the intern() method: String intern()

And you can find other discussions as well, when you use the forum's search function to search for "String intern", for example.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lopez Mirinda wrote:I am looking for a good tutorial on the internal implementation of a string, its memory management and much more about it which I am not aware of.


Then you're looking for the wrong thing.

Firstly: Java is an Object-Oriented language, which means that designers don't want you to know about the internals of their classes; and that applies to any class, not just String.

Secondly: Java is a memory-managed language, which means that it deals with memory.

It may be worth your while to read the CachedObjects page, which explains a bit about the String pool; but the fact of the matter is: it's not that important, and you shouldn't obsess about it. Unless you plan on creating gazillions of Strings with the new keyword when you don't need to, it's not likely to make much difference to your program.

What you need is a good tutorial on how to USE Strings; and for that, the Oracle one is probably as good a place as any to start.

Another place to look is the API documentation itself (java.lang.String ←click). They may seem a bit dry at the moment, but getting familiar with them is part and parcel of becoming a good Java programmer.

Winston
 
Lopez Mirinda
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper, thank you for the intern link. was really helpful. And will definitely use the search option in future.

Thank you for the detailed reply, Winston.

I am looking out for a job change and this is one of the frequently asked questions in the interviews. So, trying to get into the nitty gritties of the language. Started with String.

Might post a few more questions wrt other topics later. :)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This JavaRanch journal article may be a useful resource for you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic