Skip to main content

String Pool In Java



Java Virtual Machine maintains an internal list of references for interned Strings ( pool of unique Strings) to avoid duplicate String objects in heap memory.

Whenever the JVM loads String literal from class file and executes, it checks whether that String exists in the internal list or not. If it already exists in the list, then it does not create a new String and it uses reference to the existing String Object. JVM does this type of checking internally for String literal but not for String object which it creates through 'new' keyword. You can explicitly force JVM to do this type of checking for String objects which are created through 'new' keyword using String.intern() method. This forces JVM to check the internal list and use the existing String object if it is already present.

e.g.

String obj1 = "hello";
String obj2 = "hello";
String obj3 = "hello";

Here we are declaring three String reference variables and in heap one object is created and all the reference variable refer to the same object.

The concept of String Pools is because String objects are immutable.Z

Lets take the previous example code...

If string objects are not immutable and if we changed obj1, say

obj1 = obj1+"world";

This will reflect in all the three reference variables.

Hence because of the immutable property of String Objects we can use the same object to referred by more than one reference variable.

Note: If we are creating String Objects with 'new' keyword , JVM will not check internal String of Pools, and it will create a new heap object.

Comments

Popular posts from this blog

Initializing fields to 0-false-null is redundant

One of the most fundamental aspects of a programming language is how it initializes data. For Java, this is defined explicitly in the language . For fields and array components , when items are created, they are automatically set to the following default values by the system: numbers: 0 or 0.0 booleans: false object references: null This means that explicitly setting fields to 0, false, or null (as the case may be) is unnecessary and redundant. Since this language feature was included in order to, in part, reduce repetitive coding, it's a good idea to take full advantage of it. Insisting that fields should be explicitly initialized to 0, false, or null is an idiom which is likely inappropriate to the Java programming language. _______________________________________________

One good Question about Garbage Collection

Class CardBoard {        Short story = 200;        CardBoard go(CardBoard cb){             cb = null;             return cb;        }        public static void main(String args[]){             CardBoard c1 = new CardBoard();             CardBoard c2 = new CardBoard();             CardBoard c3 = c1.go(c2);             c1 = null; // (1)            // some code        } } After executing line (1) how many objects are eligible for garbage collection ? First we can go through the code, - We are creating two objects and they are referred by reference variables c1 and c2.- During the execution of thisline CardBoard c3 = c1.go(c2); no new objects are created in the heap. - The...

Explain the Class Loader Mechanism of JVM?. How to write a custom Class Loader Class?

Every java program has at least three class loaders. - The bootstrap class loader - The extension class loader. - The system class loader. The bootstrap class loader loads the system classes from the JAR Fle rt.jar(part of JRE System Library) . It is an integral part of the virtual machine and is usually implemented in C. There is no Class loader object corresponding to the bootstrap class loader. For example. String.class.getClassLoade r() will return null since String class is loader by bootstrap loader. The extension class loaders loads "standard extensions "from the jre/lib/ext directory. The system class loader loads the application classes. It locates the classes in the directories and JAR/ZIP files on the class path. The System class loader also called application class loader. In Sun's java implementation , the extension adn system class loaders are implemented in java. Both are instances of URLClass loader. You can just go through the source code of URLCla...