Implementing an equals() Method : equals method « Utility Classes « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Utility Classes » equals method 
8.2.3.Implementing an equals() Method
public class MainClass {
  public static void main (String [] args) {
     MyClass one = new MyClass(8);
     MyClass two = new MyClass(8);
     if (one.equals(two)) {
        System.out.println("one and two are equal");
     }
  }
}
class MyClass {
  private int moofValue;
  MyClass(int val) {
     moofValue = val;
  }
  public int getMyClassValue() {
     return moofValue;
  }
  public boolean equals(Object o) {
    if ((instanceof MyClass&& (((MyClass)o).getMyClassValue()
         == this.moofValue)) {
      return true;
    else {
       return false;
    }
  }
}
one and two are equal
8.2.equals method
8.2.1.equals() and hashCode()
8.2.2.The signature of equals() from Object class is 'public boolean equals(Object object)'
8.2.3.Implementing an equals() Method
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.