• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

equals method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can some please explain why I'm not getting true for an answer since the equals method compares object contents
Thanks in advance!
class Value {
int i;
}
public class EqualsMethod {
public static void main(String args[])
{
Value v1 = new Value();
Value v2 = new value();
v1.i = v2.i = 100;

System.out.println(v1.equals(v2));
}
}


 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to overridden the equal method from the Object class. The default equal method only compare reference value. Thus, v1.equals(v2) is the same as v1 == v2 which it is false.
hope this help.

[This message has been edited by FEI NG (edited January 02, 2002).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to override the equals method. Your Value class uses the Object class equals method which returns true if and only if the reference refers to the same object. See API javadoc.
Oops, Fei hit the submit key before me.
[This message has been edited by Jim Turk (edited January 02, 2002).]
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic