• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Do u agree...?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Do u agree with the result of the following code:
class q1{

public static void main(String args[]){
String s1=new String("jamit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);

String s3="jarit";
String s4="jarit";
String s2=s1.replace('m','r');

System.out.println(s2==s3);
System.out.println(s3==s4);

System.out.println("\n\nNow reserch is here:");
System.out.println(s3.hashCode());
System.out.println(s4.hashCode());
System.out.println(s2.hashCode());
if(s2==s3)
System.out.println("s2==s3");

}
}

jarit
jamit
false
true
Now reserch is here:
100895878
100895878
100895878
bottom line is that why it is returning 'false' even the hashcodes are same.
Thanks in advance.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s2==s3 compares the String object references. It does not compare the string contents; for that, you need to say s2.equals(s3) . Hash codes are not relevant here.
[ November 11, 2002: Message edited by: Ron Newman ]
 
Muhammad Shahzad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ron, i got it but what are hashcodes then.
Are they not the refrences.
Thanks
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hash codes are used when you use an object as a key in a HashSet, HashMap, or Hashtable.
 
Muhammad Shahzad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for your kind help.I had a wrong concept about hashcodes.
Thanks again.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic