• 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

qureis on scja

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i hava a trouble with a few programming questions...would be grateful if someone helps out..
supposedly a program construct creates two new instances as given below:
class Dog{
String name;
public static void main(Sting []args){
Dog d1=new Dog();
Dog d2=new Dog();
d1.name="aiko";
d2.name="aiko";
if(d1.equalsd(2))
System.out.println("true");
}
}
why no output is produced when according to mackenzie book it gives true??
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is its output really "true" ?

As far as I know, the equals melhod defined in the Object class compares hashCodes.

I just made some implements in your code to print the hashCode:



and the output is:



so their hashCodes are different and this code will never output a "true" message.

are you sure this is all the code?
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to my knowlegdge, the equals method of the String class behaves differently when compared to others. If the same thing was implemented on String objects, the result would have been true.

for example.

String s1 = new String("Hi");
String s2 = new String("Hi");

if(s1.equals(s2))
{
SOP("True");

}

In the above case, the output will be true.

Correct me if i am wrong.

And the equals method of the object class compares the hash codes.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the equals method of Object class just checks

this == obj

and not

this.hashCode() == obj.hashCode()

You can try it by implementing the hashCode method that returns 1 and don't implement the equals method. then also it will return false(i.e. even though every object will have equal hash code)...

In your case since the answer will be false...there is no chance that the answer is true. I have tried the code...

Your code also has some errors...String has been written Sting in main() and d1.equals(d2) has been written d1.equalsd(2)
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic