GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 461 of 502    Print  
What is String Pool?

  
Total Answers and Comments: 4 Last Update: February 11, 2008     Asked by: sha_kr2001 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sampra
 
A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new operator do not refer to objects in the string pool.Afetr using the string object returns to the pool in the pool there is no rool of gc.and object in the string pool is immutable

Above answer was rated as good by the following members:
Avilesh, shouvanik1979
December 27, 2007 04:46:04   #1  
prashantB Member Since: December 2007   Contribution: 2    

RE: What is String Pool?
A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new operator do not refer to objects in the string pool.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
December 27, 2007 04:48:08   #2  
prashantB Member Since: December 2007   Contribution: 2    

RE: What is String Pool?
Ex1:
public class Program
{
public static void main(String[] args)
{
String str1 "Hello";
String str2 "Hello";
System.out.print(str1 str2);
}
}

The result is

true


Ex2: public class Program
{
public static void main(String[] args)
{
String str1 "Hello";
String str2 new String("Hello");
System.out.print(str1 str2 + " ");
System.out.print(str1.equals(str2));
}

....... hope this will help
}

The result is

false true

 
Is this answer useful? Yes | No
December 27, 2007 08:08:49   #3  
jainendrabohra Member Since: December 2007   Contribution: 1    

RE: What is String Pool?
in java for String there is a special consideration if you are creating a String class object like
String s "jai"
than this will go to string pool and if are again want the same string like
String s2 "jai"
than no other object of String class is created now refrence varible s2 will refere to "jai"
this is done because String objects are immutable.
and you can check it by test like
if(s s2)
this will give the true value and exectue if condition.


means when you do any modificattion on them than another object of String class is created and the reference of that class is gone.like
String s "jai"
s s.concat(" hai");
now is refering to jai hai and refrence to jai string is gone and this object is stored is sting pool. for further use.
like in future we need that object than the new object is not created and by jvm the refrence of that object is given to that refrence varible.

and if you are creating a object by new operator than always new object is created .
e.g.
String s2 "jai".
String s new String ("jai");
and now you are doing the test than this will give false and if condition will not execute.
if(s2 s)
{
//code
}
this will not execute.

 
Is this answer useful? Yes | No
February 11, 2008 06:27:24   #4  
sampra Member Since: February 2008   Contribution: 278    

RE: What is String Pool?
A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new operator do not refer to objects in the string pool.Afetr using the string object returns to the pool in the pool there is no rool of gc.and object in the string pool is immutable
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape