string pool in java

when we declare a string object in the form of String aa=new String("java") and String aa="java" ,then is that in both cases the string object is made on the heap area.

Or is it the case that in first case we have a string pool wherein we have a string literal whose value is "java" and the reference of that string literal in the string pool is returned to the object in the heap created by the expression new String("java") ,and now the reference of this object is actually returned to the reference variable a.

And in the second case we have the reference variable a directly pointing directly to the string literal "java" which is present in string pool.

Replies

  • Alok mishra
    Alok mishra
    when we initialize a string reference by a string literal then the literal goes into string pool and if again another string reference is initialized by the same literal value then jvm does not create a new space for that literal , it just assigns the reference of already created literal to the new reference . string pool /literal pool is also an special purpose space on heap .
  • radha gogia
    radha gogia
    Alok mishra
    when we initialize a string reference by a string literal then the literal goes into string pool and if again another string reference is initialized by the same literal value then jvm does not create a new space for that literal , it just assigns the reference of already created literal to the new reference . String pool /literal pool is also an special purpose space on heap .
    Okay this I understood but actually what is the case when we actually write string a=new string("java"); if i already have declared the string literal java before this step as string s="java", so now is it that the java would again be created on the heap region or the previous reference of the string literal "java" would be returned to the object in heap whose reference would then be returned to variable a.
  • micheal john
    micheal john
    String a = new String("java"); will instantiate an object of type string outside the string literal pool (heap space), but
    String s = "java"; if we don't use new operator for string instantiation then this object will be inside the string literal pool.
    s==a will be false
  • radha gogia
    radha gogia
    micheal john
    String a = new String("java"); will instantiate an object of type string outside the string literal pool (heap space), but
    String s = "java"; if we don't use new operator for string instantiation then this object will be inside the string literal pool.
    s==a will be false
    See I am getting this but my question is that the "java" written inside new String("java") ,this string literal will be present in string pool or not..
    Meaning to say that JVM will create only one string literal "java" but it will be pointed once by the object on the heap and once by the string refernce variable s.,right or wrng??
  • micheal john
    micheal john
    radha gogia
    See I am getting this but my question is that the "java" written inside new String("java") ,this string literal will be present in string pool or not..
    Answer is "no"
    new string () won't be in string pool
  • radha gogia
    radha gogia
    micheal john
    Answer is "no"
    new string () won't be in string pool
    I am not talking about the object created through new String("java"),I am talking about the string literal "java" where will this literal be stored?
  • Alok mishra
    Alok mishra
    radha gogia
    I am not talking about the object created through new String("java"),I am talking about the string literal "java" where will this literal be stored?
    memory allocated (or objects created ) using 'new' keyword will always be in heap .
  • Alok mishra
    Alok mishra
    radha gogia
    I am not talking about the object created through new String("java"),I am talking about the string literal "java" where will this literal be stored?
    memory allocated (or objects created ) using 'new' keyword will always be in heap .
  • Alok mishra
    Alok mishra
    radha gogia
    I am not talking about the object created through new String("java"),I am talking about the string literal "java" where will this literal be stored?
    memory allocated using ' new ' keyword will always be in heap .
  • radha gogia
    radha gogia
    Alok mishra
    memory allocated using ' new ' keyword will always be in heap .
    I understood this ,I am talking the string literal "java' where wil this literal be located on the string pool or not??
  • radha gogia
    radha gogia
    Alok mishra
    memory allocated using ' new ' keyword will always be in heap .
    I understood this ,I am talking the string literal "java' where wil this literal be located on the string pool or not??
  • micheal john
    micheal john
    please go through this link
    #-Link-Snipped-#

You are reading an archived discussion.

Related Posts

Just pour in ideas on how to interface an FSR to 89c51. I am not clear on what I have to feed from an FSR to the ADC 0804
How to do Stacking Sequence of Composite Materials? Which sequence is best?
String a= "/u0041"; String h=new String("A"); System.out.println(a==h); System.out.println(a.hashCode()==h.hashCode()); Please explain why the second print statement is giving false when in the string pool we have the same string literal "A".
Quote: An autonomous Audi RS 7 concept car has completed a lap on the Grand Prix track in Hockenheim in a little over two minutes. Endquote https://www.theengineer.co.uk/news/a...ial/1019396.article?cmpid=tenews_619096#video
Somebody experienced in Matlab please contact me. I need to run c++ program in matlab.