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
How are java objects passed to a method and what are native methods?
Can we override compareto method?
What are basic keywords?
What does 0 mean in boolean?
Is singleton class thread safe?
Is java hashset ordered?
How can the checkbox class be used to create a radio button?
Write a program to solve producer consumer problem in java?
What is api data?
Can we define static methods inside interface?
Difference between process and thread?
What are the differences between include directive and include action?
why would you use a synchronized block vs. Synchronized method? : Java thread
how we can create packages in java?
What is the history of java?