CrazyEngineers
  • radha

    radha

    MemberOct 23, 2014

    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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Alok mishra

    MemberOct 23, 2014

    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 .
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 23, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberOct 24, 2014

    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
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 24, 2014

    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??
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberOct 25, 2014

    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
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 25, 2014

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberOct 25, 2014

    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 .
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberOct 25, 2014

    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 .
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberOct 25, 2014

    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 .
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 25, 2014

    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??
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 25, 2014

    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??
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberOct 26, 2014

    please go through this link
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register