• Post Reply Bookmark Topic Watch Topic
  • New Topic

All About Java Strings  RSS feed

 
Lopez Mirinda
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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. :)
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This JavaRanch journal article may be a useful resource for you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!