what is meant by string pooling?

Answer Posted / vijay srivastava

Most of the Answers are correct but they are not much clear so I am trying write clear Answer. I hope, you guys will like it.

String pooling is the mechanism that was introduced by Java.
String pooling is possible because String is immutable and final class.

1- String firstString = "Hello";
2- String secondString = "Hello";
3- String thirdString = new String("Hello");
There is no guarantee to create new object for string Step 1 and 2 but in step 3, Jave(JVM) gives guarantee to create object.
In step 1st and 2nd, JVM search the same literal in String pool if it's available then return the reference else JVM will create the object for same and added in to String pool.
In step 3, JVM definitely create the new object, It will not part of String pool. If we want this newly created object should be part of String pool then we need to call intern() method of String class. If we called intern() method then JVM will search the same literal in String pool if JVM found same literal then return reference else JVM will add the same object in String pool.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can private class be extended java?

559


What is a singleton in genetics?

558


How many types of literals are there in JAVA?

588


Why map is used in java?

569


What is the purpose of using java.lang.class class?

518






What is difference between checked and unchecked exception in java?

565


What is the generic class?

527


Should a main method be compulsorily declared in all java classes?

526


what are three ways in which a thread can enter the waiting state? : Java thread

570


When should we create our own custom exception classes?

579


Explain the importance of finally block in java?

566


What defines function?

531


Justify your answer that you can't define a method inside another method in java, if you can then how?

600


I want to re-reach and use an object once it has been garbage collected. How it's possible?

531


What do you know about the garbage collector in java?

566