How are Strings created and stored in Java? -


to understand how string objects created , stored, tried following program , see output against have query. can please help?

package corejava.immutable;  public class stringtester {      public static void main(string[] args) {         // todo auto-generated method stub          string s1 = "omkar patkar";         string s2 = "omkar patkar";         string s3 = "omkar" + " patkar";         string s4 = "omkar";         string s5 = s4 +" patkar";         string s6 = new string("omkar patkar");          system.out.println("hashcode s1 = "+s1.hashcode());         system.out.println("hashcode s2 = "+s2.hashcode());         system.out.println("hashcode s3 = "+s3.hashcode());         system.out.println("hashcode s4 = "+s4.hashcode());         system.out.println("hashcode s5 = "+s5.hashcode());         system.out.println("hashcode s6 = "+s6.hashcode());          system.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");          system.out.println("indentity hashcode s1 = "+system.identityhashcode(s1));         system.out.println("indentity hashcode s2 = "+system.identityhashcode(s2));         system.out.println("indentity hashcode s3 = "+system.identityhashcode(s3));         system.out.println("indentity hashcode s4 = "+system.identityhashcode(s4));         system.out.println("indentity hashcode s5 = "+system.identityhashcode(s5));         system.out.println("indentity hashcode s6 = "+system.identityhashcode(s6));         system.out.println("indentity hashcode intern s6 = "+system.identityhashcode(s6.intern()));          system.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");          system.out.println("s1 == s2 : - "+(s1 == s2));         system.out.println("s1 == s3 : - "+(s1 == s3));         system.out.println("s1 == s4 : - "+(s1 == s4));         system.out.println("s1 == s5 : - "+(s1 == s5));         system.out.println("s1 == s6 : - "+(s1 == s6));          system.out.println("\ns2 == s3 : - "+(s2 == s3));         system.out.println("s2 == s4 : - "+(s2 == s4));         system.out.println("s2 == s5 : - "+(s2 == s5));         system.out.println("s2 == s6 : - "+(s2 == s6));          system.out.println("\ns3 == s4 : - "+(s3 == s4));         system.out.println("s3 == s5 : - "+(s3 == s5));         system.out.println("s3 == s6 : - "+(s3 == s6));          system.out.println("\ns4 == s5 : - "+(s4 == s5));         system.out.println("s4 == s6 : - "+(s4 == s6));          system.out.println("\ns5 == s6 : - "+(s5 == s6));          system.out.println("\ns1 == s6.intern() : - "+(s1 == s6.intern()));         system.out.println("s2 == s6.intern() : - "+(s2 == s6.intern()));         system.out.println("s3 == s6.intern() : - "+(s3 == s6.intern()));         system.out.println("s4 == s6.intern() : - "+(s4 == s6.intern()));         system.out.println("s5 == s6.intern() : - "+(s5 == s6.intern()));       }  } 

and see following output: -

hashcode s1 = 2062602683 hashcode s2 = 2062602683 hashcode s3 = 2062602683 hashcode s4 = 76311326 hashcode s5 = 2062602683 hashcode s6 = 2062602683 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ indentity hashcode s1 = 4337374 indentity hashcode s2 = 4337374 indentity hashcode s3 = 4337374 indentity hashcode s4 = 18019860 indentity hashcode s5 = 31054905 indentity hashcode s6 = 605645 indentity hashcode intern s6 = 4337374 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ s1 == s2 : - true s1 == s3 : - true s1 == s4 : - false s1 == s5 : - false s1 == s6 : - false  s2 == s3 : - true s2 == s4 : - false s2 == s5 : - false s2 == s6 : - false  s3 == s4 : - false s3 == s5 : - false s3 == s6 : - false  s4 == s5 : - false s4 == s6 : - false  s5 == s6 : - false  s1 == s6.intern() : - true s2 == s6.intern() : - true s3 == s6.intern() : - true s4 == s6.intern() : - false s5 == s6.intern() : - false 

identity hashcodes of s5 , s6 different s1, s2, s3...why so?

in area of memory these objects created? ... object graph understand ...

you have created string literals , string objects. string literals s1 , s2 stored in string pool. same string have same reference. efficient.

string objects created using new keyword result in object stored on heap. treated in same way other object. creating 2 objects same string value result in 2 objects each it's own reference. using new not have same efficiencies string literals in string pool. interning string object moves string pool.

when compare 2 objects using == comparing references. thus, comparing 2 string literals same value result in test being true. however, testing 2 objects created new keyword not. why should use equals method compare objects.

edit

strings created concatenation of 2 string literals result in string literal, example s3. thus, s3 has same identity hashcode s1 , s2. however, s5 created string literal , reference string literal, resulting in new object. explains why has different identity hashcode.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -