• Post Reply Bookmark Topic Watch Topic
  • New Topic

String's intern() - details  RSS feed

 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


After learning quite a bit about Strings here at Javaranch, I have some questions. When I decompile the String class, I see:

public native String intern();

There is no method body, I assume some native code is linked in somehow? Can anyone tell me how this code looks, or where I can find information on it (I know functionally what it does, but I want to know exactly what it does)?

Second, I can find no private members that make it look like the String class can have a private area with references to String constant objects, while in many posts it is mentioned that the String class has this. I've seen in the JVM specification that each String constant has its entry in the string constant pool (CONSTANT_String_info), does intern()'s code scan the constant pool (by tag?)? Or is there some magic that adds some form of collection to the strings private area?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ronald Schild:
Hello,


After learning quite a bit about Strings here at Javaranch, I have some questions. When I decompile the String class, I see:

public native String intern();

There is no method body, I assume some native code is linked in somehow? Can anyone tell me how this code looks, or where I can find information on it (I know functionally what it does, but I want to know exactly what it does)?

Usually native code is in a DLL for Windows, or a .so library file for Linux/Unix. This is most likely the same. The only problem is - there are so many DLLs in the Java installation folder

You can download the JVM source code from http://java.sun.com/javase/downloads/?intcmp=1281 - just scroll down. I warn you though - it's not going to be an easy read.

Second, I can find no private members that make it look like the String class can have a private area with references to String constant objects, while in many posts it is mentioned that the String class has this. I've seen in the JVM specification that each String constant has its entry in the string constant pool (CONSTANT_String_info), does intern()'s code scan the constant pool (by tag?)? Or is there some magic that adds some form of collection to the strings private area?

It's probably a "private member" of the native code that all together forms the JVM. You can check the source.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The open source JDK?

I'll check it this evening, can't download it at work.


Thanks for the link. Although it'll be a grind. Any comprehensive sources are still welcome.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[]: I've seen in the JVM specification that each String constant has its entry in the string constant pool (CONSTANT_String_info),

It's not obvious, but there are several different "pools" here. There's one constant pool in each class file, and one runtime constant pool (in the JVM) for each class or interface that is loaded. But these are not the same as the String pool maintained by the intern() method. There is just one String pool in the entire JVM, and it contains Strings only. There are many constant pools and runtime constant pools, and they contain both strings and primitives, as well as other things. It's unfortunate that the specs use such similar terms for these different things without explicitly pointing out that they're not the same thing. But they're not.

If you have further questions about the String pool, it might be easier to ask them here, as the JDK code is probably going to be pretty hard to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!