• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

String intern()  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

The concept of intern method is "When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned"

Please help me out in the below scenario..

String s="A";
String s1 = new String("A").intern();
So my question here is "s1 first it creates an object in heap memory using new and then refer to string pool(so here it creates one object in heap memory and two references to stringpool ) or s1 directly refers to stringpool without creating anyobject in heap memory(here we can have only two references to stringpool)".
 
Marshal
Posts: 61773
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell, neither of them.
You need to consider the precedences, and execution order. After the declaration of memory to hold a String (s1), the next thing which is encountered is the = operator, because that is the leftmost. But any operators to its right have a higher precedence.
Next is the new operator, which has a very high precedence, so that is executed, using the String class' co[py‑constructor on"A".
Next, the . operator which causes invocation of the intern() method, which checks whether a String identical to its operand exists in the String pool. It must do, because the literal "A" has been used. Although you used "A" twice, it is only in the String Pool once, and the intern() method doesn't add it again. The intern() method returns the String pool equivalent of new String("A"), and now the = operator can be executed, putting a reference to that "A" in the String pool into s1.

The other String created with the new operator is now eligible for garbage collection.
 
keerthi vineela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,

Thanks for the reply.

By your explanation , whether the string is present in literal pool or not ,new operator will create an object in heap memory but it will be garbage collected as it has no reference and the reference is given to stringpool because of intern().

And I have one more doubt related to strings.

i. String s3=new String("B");
Here I have two questions
1.Is this will create an object in heap memory and also places the string in string constant pool .
2.If the above statement is true ,s3 refers to object in heap memory then what about the literal in pool(Because no reference it will be garbage collected or not.And if it is garbage collected what about the below step ii.)
ii. String s4="B" ; If the statement one is true ,then it checks in the pool and already "B" is available ,s4 refers to that.
 
Campbell Ritchie
Marshal
Posts: 61773
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

keerthi vineela wrote:Hi Ritchie,

Thanks for the reply.

You're Welcome


By your explanation , whether the string is present in literal pool or not ,new operator will create an object in heap memory but it will be garbage collected as it has no reference and the reference is given to stringpool because of intern(). . . .

I didn't say anything about whether the string is present in the string pool or not. In the case you mentioned, we knew it already was in the String pool. The documentation for String#intern() says

Otherwise, this String object is added to the pool and a reference to this String object is returned.

This suggests such a String is not garbage collected, but I am not certain.

These are all things you do not actually need to know (at least not once you have finished the certification exams ‍) but if you search for Strings, literally, you find an old JavaRanch journal article which explains all your questions.
 
Campbell Ritchie
Marshal
Posts: 61773
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

keerthi vineela wrote: . . .

i. String s3=new String("B");
Here I have two questions
1.Is this will create an object in heap memory and also places the string in string constant pool .
2.If the above statement is true ,s3 refers to object in heap memory then what about the literal in pool(Because no reference it will be garbage collected or not.And if it is garbage collected what about the below step ii.)
ii. String s4="B" ; If the statement one is true ,then it checks in the pool and already "B" is available ,s4 refers to that.

I thinik that question is also answered by the documentation for intern(). It does not say that two different objects are created. I beleive that objects in the String pool are always regarded as reachable until all classes which have those literals in are unloaded from memory; they would therefore not be garbage collected. But again, you do not need to know such details for programming. I think the JavaRanch journal article will help: I have found it here.
 
keerthi vineela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already completed my OCJP certification

I am looking for a job change , These are some questions asked in the interview.

Thanks alot for the reply.
 
Campbell Ritchie
Marshal
Posts: 61773
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome I am not quite sure about those answers, but I think I am right. The intern() documentation is not 100% clear that the same object is created on the heap and put into the String pool. Maybe 99% clear!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

keerthi vineela wrote:
i. String s3=new String("B");
Here I have two questions
1.Is this will create an object in heap memory and also places the string in string constant pool .



Executing that line doesn't place anything in the pool

Before that line is execute, the String "B" will already be in the constant pool. When that class is initialized (basically when it's loaded, but it can be some time after that), if the String "B" is not already in the constant pool, it will be placed there, since it occurs in this class.

Then when we get to this line, the new operator creates a String object, retrieves a reference to the "B" String in the pool, and uses that as the arg to the "copy constructor" for the new String it's creating.
 
keerthi vineela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused with the intern() concept and when it is useful .
Please give the difference between
1.String s5="abc";
2.String s6=new String("xyz").intern();

According to my understanding
1.String s5="abc" //checks for the string "abc" in the pool and if it is available, it will give a reference to s5 , else it creates a string "abc" in the pool and provides the reference to s5

2.String s6=new String("xyz").intern(); //The same is going to happen here also , means the string "xyz" is checked in the pool and if it is available it will give a reference to s6 , else it creates a string "xyz" in the pool and provides the reference to s5

So , we can directly give String s6="xyz" instead of String s6=new String("xyz").intern() .what is the necessary to use intern() .

Thanks in advance.
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

keerthi vineela wrote:I am confused with the intern() concept and when it is useful .


Calling the intern() method is almost never useful. I can't remember using it even once in my 15 years of Java experience. It's something that you would need only in very special circumstances - certainly not basic, essential knowledge.

keerthi vineela wrote:
Please give the difference between
1.String s5="abc";
2.String s6=new String("xyz").intern();


After the first statement, s5 will refer to a String object that contains "abc" and that's in the string pool.

What happens in the second statement is:
1) There's a String object that contains "xyz" in the pool (because "xyz" is a string literal - just like line 1).
2) You create a new String object that copies the content of the string in the pool; the content of this new String object will be "xyz".
3) You call intern() on that new String object. This will return the String object that was already in the pool (step 1).
4) s6 refers to the String object in the pool. The new String object that you created in line 2 is forgotten.

In the end, the effect of line 2 is the same as String s6 = "xyz";, except that you created an unnecessary temporary String object.

keerthi vineela wrote:So , we can directly give String s6="xyz" instead of String s6=new String("xyz").intern() .what is the necessary to use intern() .


Indeed, and in this example it is totally unnecessary to call intern().
 
keerthi vineela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Jesper de Jong .

Now I understood the concept of intern.

I have some basic doubts about String.

1.String ss=new String("hgsd");
My doubt here is
Is it creates an object in heap memory and also places the literal in pool.Basically what the pool contains whether the references or the copy of object itself.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

keerthi vineela wrote:1.String ss=new String("hgsd");
My doubt here is
Is it creates an object in heap memory and also places the literal in pool.Basically what the pool contains whether the references or the copy of object itself.


A string literal (anything between double quotes in your source code) is always represented by a string in the string pool. So, the "hgsd" in that line of code is a string in the string pool.

The string pool contains references to String objects - the objects themselves, just like any other Java object, are on the heap.

What that line of code does, is create a new String object that is a copy of the string that's in the pool because of the string literal "hgsd".

You should never write code like that line - String objects are immutable, so it is never necessary to make a copy of a string object because you might be worried that the original string object will be modified somewhere in the program - string objects cannot be modified after they have been created. So, creating a new String object that copies its content from a literal is always unnecessary. Just write String ss = "hgsd"; instead, it will have the same result, without creating an unnecessary copy of the String object.
 
keerthi vineela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Jesper de Jong for the reply.

Now I am very much comfortable with string concepts
 
Campbell Ritchie
Marshal
Posts: 61773
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleased that you have got it sorted out

keerthi vineela wrote:. . . These are some questions asked in the interview. . . .

Were they typical of the questions asked? If so, would you want to work there in the first place?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

keerthi vineela wrote:Is it creates an object in heap memory and also places the literal in pool.Basically what the pool contains whether the references or the copy of object itself.


I think you're fixating a bit too much on the mechanics of the storage, which is something you don't really need to worry about in Java. Simply put, the pool is a list of Strings created from String literals.
intern() just checks if the String it's called on equals() one that is already in the pool and, if so, returns the pool instance instead.

You may find the CachedObjects page worth reading.

Winston
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:intern() just checks if the String it's called on equals() one that is already in the pool and, if so, returns the pool instance instead.


It also adds the String to the pool if it is not already there.
 
Ranch Hand
Posts: 227
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Under what scenario when the string wont be present in the pool ?
 
Campbell Ritchie
Marshal
Posts: 61773
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this forum for “Strings, literally,” which should lead you to an old JavaRanch Journal article which I think will answer your question.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!