Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What does interning mean and why is it required  RSS feed

 
praveena satish
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does interning mean and why is it required
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveena satish wrote:What does interning mean and why is it required


Yeah, it would probably be a good idea to elaborate what you are asking here. What "interning" are you referring to here? Are you asking about the intern() method of the String class?

Henry
 
Campbell Ritchie
Marshal
Posts: 55705
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As a general rule interning means putting certain objects into a particular memory location. That memory location probably removes duplicates so there are not two identical objects.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and i was all set to move this to a jobs discussion forum, as i thought they were asking about being an intern.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveena,
Intern means two things
1) Memory locations for Strings as described above
2) Participating in an internship which is a time boxed entry level type job where did your toe in the field.
 
praveena satish
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,yes should have been more specific.i wanted the purpose of String intern() method.as understand from the discussion it is required for removing duplicates.

  String s="hello".concat("world");
          String s2=s.intern();  
  System.out.println(s==s2);
In the above program, seeing the first line an new instance is created in heap,but not in SCP.
in the second line, the intern method creates an instance in the SCP but returns the same reference s;

so if add few more lines of code,
                  String s="hello".concat("world");
  String s2=s.intern();
  String s3="helloworld";
  System.out.println(s==s2);//true
  System.out.println(s==s3);//true
  System.out.println(s2==s3);//true

so here should we understand it as the reference s is pointing to both the instances one in heap another one is SCP?



 
Campbell Ritchie
Marshal
Posts: 55705
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveena satish wrote:. . . the intern method creates an instance in the SCP but returns the same reference s . . .
No, that doesn't look right. The String#intern() method returns
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
So all interned Strings are in the “pool”. The first String put into that pool is the literal "helloworld" which is entered into the pool whenever that class is loaded. Then the two intern() calls find a String with “the same contents” alread in the pool, and therefore return a reference to that String. Then you are testing all three references in the pool (not on the ordinary parts of the heap). I think the Strings remaining in the ordinary part of the heap may now be eligible for GC if there are no other references to them, but I am not certain.
Your three uses of the same‑object operator (==) simply demonstrate the transitive property of the equivalence relation represented by Object#equals().
I looked through the API index and there appears only to be one intern() method in the whole standard API.

You don't need to know this for ordinary programming; I think this discussion would fit better in something like the OCJP forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!