Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Java's String#intern() method implemented?

I tried to look at Java's String#intern() method, but it's public native String intern();

In general, how is interning implemented? In String's case?

like image 337
Haywood Jablomey Avatar asked Jul 24 '10 02:07

Haywood Jablomey


People also ask

How is Java's string pool used?

String Pool is a storage area in Java heap. String allocation, like all object allocation, proves to be a costly affair in both the cases of time and memory. The JVM performs some steps while initializing string literals to increase performance and decrease memory overhead.

How is string implemented in Java?

String concatenation is implemented through the StringBuilder (or StringBuffer ) class and its append method. String conversions are implemented through the method toString , defined by Object and inherited by all classes in Java.

What is string in Java with example?

In Java, a string is a sequence of characters. For example, "hello" is a string containing a sequence of characters 'h' , 'e' , 'l' , 'l' , and 'o' . We use double quotes to represent a string in Java.


Video Answer


1 Answers

For Sun Java, start with JVM_InternString, on line ~3639 of jvm.cpp. Technically, the actual String method is in java/lang/String.c, but it immediately calls JVM_InternString. You can continue to StringTable::intern in symbolTable.cpp.

In a more abstract sense, the purpose of interning is to map equivalent strings to a single canonical one.

like image 124
Matthew Flaschen Avatar answered Sep 22 '22 20:09

Matthew Flaschen