Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework
this week in the
Java in General
forum!
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
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Java in General
how to make equal objects?
Gabe Herbert
Greenhorn
Posts: 10
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm trying to figure out what what would enable two objects to qualify as "equal"...
When I create two Integer's they are equal, but when I create two of TPVEvent, which simply holds 3 variables, the objects do not equal!
Would someone explain this to me?
class IntCompare { public static void main(String[] args) { Integer int1 = new Integer(42); Integer int2 = new Integer(42); TPVEvent tpv1 = new TPVEvent(1, 2, 3); TPVEvent tpv2 = new TPVEvent(1, 2, 3); System.out.println(int1.equals(int2) ); //returns true System.out.println(tpv1.equals(tpv2) ); //returns false } } class TPVEvent { int t; int p; int v; public TPVEvent(int t, int p, int v) { this.t = t; this.p = p; this.v = v; } }
Christophe Verré
Sheriff
Posts: 14691
16
I like...
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You need to override the equals() method of the Object class.
For details :
http://www.geocities.com/technofundo/tech/java/equalhash.html
[My Blog]
All roads lead to JavaRanch
Ulf Dittmer
Rancher
Posts: 43081
77
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
... and, as that article points out, the hashCode() method as well.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
When the JVM create a new Wrapper and when the JVM use a living one? ( K&B 5.0)
equals() question required
What is the use of Clone and Equal?
Need help with code!!!
if statement help
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...