• 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

equals() method called by a Vector object

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
From what I understand, only the String and Wrapper classes override the equals() method, then why does the following code evaluates 'if' statement to true.

import java.util.*;
public class EqualsTest
{
public static void main(String[] args) {
Integer i = new Integer(10);
Vector v = new Vector();
LinkedList l = new LinkedList();

v.add(i);
l.add(i);

if (v.equals(l))
System.out.println("They are equal");
else
System.out.println("They are Not equal");
}
}


Thanks:
Namita
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plenty of classes override equals -- including Vector. If you check the API for Vector's equals method, you'll find that it...

"Compares the specified Object with this Vector for equality. Returns true if and only if the specified Object is also a List, both Lists have the same size, and all corresponding pairs of elements in the two Lists are equal."
 
Namita Jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it mean for the SCJP I have to memorize what all classes override this method and which ones don't ?

I understand that the API clearly states the behaviour of equals() method for the Vector class.

Thanks..
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Namita Jain:
Does it mean for the SCJP I have to memorize what all classes override this method and which ones don't? ...


If I'm not mistaken, the classes that don't override equals are the exception (like StringBuffer), so I would just make note of these.
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic