Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 The equals() method: Strings are the only things that will work
Index -> Programming, Java -> Java Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
randint




PostPosted: Thu Apr 11, 2013 1:26 pm   Post subject: The equals() method: Strings are the only things that will work

As I was programming a user name/password scheme (which uses a substitution cipher to encode passwords and then this cipher is saved to a *.text file), I noticed something very odd about the
code:
Object.equals (Object)
method. If you attempt to compare a
code:
char[]
with another
code:
char[]
, it is guaranteed to return false despite the fact that the elements in the array are identical (size, order, content). So, here is an example
Java:

public class equality
{
   public static char a[] = {'a', 'b', 'c'};
   public static char b[] = {'a', 'b', 'c'};
   public static String sa = new String (a);
   public static String sb = new String (b);
   public static void main (String args[])
   {
      System.out.println (a.equals (b)); //false
      System.out.println (sa.equals (sb)); //true
   }
}

I have no idea why this happens, have I done something wrong? Obviously, the
code:
Object.equals (Object)
method is supposed to test content equality, not object reference equality!
Sponsor
Sponsor
Sponsor
sponsor
2goto1




PostPosted: Thu Apr 11, 2013 1:36 pm   Post subject: RE:The equals() method: Strings are the only things that will work

Object.equals by default tests for object reference equality. See http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object).

So by default, Object.equals(Object) tests for object reference equality. It always has.

String.equals() however overrides this default behaviour to compare if the contents of one string are identical to the contents of another string.
DemonWasp




PostPosted: Thu Apr 11, 2013 2:44 pm   Post subject: RE:The equals() method: Strings are the only things that will work

If you want to compare arrays, java.util.Arrays offers equals methods for all the "primitive" arrays as well as object arrays, as well as deepEquals(), which can be used on multi-dimensional arrays.

In general, though, it is up to the programmer to define how equality (and hashcode, and comparisons, etc) work on their own data types.

Many of the core Java classes override equals(Object) to provide the "expected" behaviour. Strings, the collections: List, Set, Map and all their implementors, and hundreds of other objects.

Arrays, such as char[] and long[], do not define equality; they use the Object.equals(Object) implementation. As 2goto1 mentioned, that just checks that the two objects are at the same memory address (not "equal" so much as "exactly the same object").

If you want to compare a char[] to a String, then you will probably have to write that yourself. You might want to use String.getChars(...), but then it's probably easier to just write a method that compares character-by-character.
randint




PostPosted: Thu Apr 11, 2013 3:36 pm   Post subject: RE:The equals() method: Strings are the only things that will work

in other words, you (DemonWasp and 2goto1) are saying that the equals method is no more than the "==" in anything other than Strings, Lists, Sets and Maps.
DemonWasp




PostPosted: Thu Apr 11, 2013 4:36 pm   Post subject: RE:The equals() method: Strings are the only things that will work

No.

If equals() hasn't been overridden, then the default implementation (in Object.equals()) is equivalent to the == operator.

However, many types (probably most of the Java standard library) override .equals() to provide a more useful definition of equality. Those classes that override .equals() will usually have JavaDoc describing exactly what conditions result in equality.

Also, you can override .equals() yourself when you write new classes, in which case you should also provide JavaDoc describing what makes .equals() return true.
2goto1




PostPosted: Fri Apr 12, 2013 12:36 pm   Post subject: RE:The equals() method: Strings are the only things that will work

Just remember if your type overrides Object.equals(), it is also in your best interest to override Object.getHashCode()
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: