• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== operator....

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOURCE: www.danchisholm.net



output is:false false true

a,b,c,d are the objects which referenced are in string constant pool..

so c and d are refering to the same object as well as (a+b) also refers to the same object..

then why 1st and 2nd is false....?

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Means

c = a+b, d = a+b

both time new object created by JVM, so they are different.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but after creating object d it check whether it is available in string constant pool or not it is available so object d also pointing to the same which is referenced by object c.javascript: x()
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure to what extent i am right. But i think i will share it.
when we say, c=a+b, it will call,
concat() of string class
or
append() of StringBuffer class(internally it would look like this:- new StringBuilder().append(a).append(b).toString(); )

( not sure which one of these)

Where in both of them returns a new string.

[ December 11, 2008: Message edited by: James Tharakan ]
[ December 11, 2008: Message edited by: James Tharakan ]
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever we make a String object by doing some operation on the Strings then we will always end up with new String Object.

String class has overridden hashCode() and equals() method so meaningfully the String objects may be equal and they will return the same hash code but then also they are situated at different memory location in the Objcet heap.

The default implementation of hashCode() provided by Object is derived by mapping the memory address of the object to an integer value. Because on some architectures the address space is larger than the range of values for int, it is possible that two distinct objects could have the same hashCode(). If you override hashCode(), you can still use the System.identityHashCode() method to access this default value.
src

The following example which I prepared by altering the above question will simplify my point.





The output of the above code is



So it is visible that whenever we make some String object by doing some alteration we will always end up in a new Object in heap. So the system HashCode will be different for them as no two Objects can share the same memory.

The hashcodes we get from the String Objects return the hashcode generated by this function as it is overridden in String class.



To get the actaul hashcode we used the static function identityHashCode() of System class.

The system hashcode for str1 and str2 are same as those objects were created without any alteration on any of the objects.
reply
    Bookmark Topic Watch Topic
  • New Topic