Start Search Contents Index Links About
EqualsTest.java

/**
 
 * Example of equals, hashCode methods.
 *
 */
public class EqualsTest {

  public static void main(String[] args) {

    //construct to Integer objects
    //notice the auto-boxing
    Integer a = 1;
    Integer b = 2;
    
    //test the equals method
    System.out.println("a = a: "+a.equals(a));
    System.out.println("a = b: "+a.equals(b));
    System.out.println("a = 1: "+a.equals(1));
    
    //look at the hashCode method
    System.out.println("a: "+a.hashCode());
    System.out.println("b: "+b.hashCode());
    System.out.println("2: "+(new Integer(2)).hashCode());
    
    System.out.println("test: "+(new String("test")).hashCode());
    
    /*
     * Expected output:
     
     * a = a: true
     * a = b: false
     * a = 1: true
     * a: 1
     * b: 2
     * 2: 2
     * test: 3556498
     
     */

  }

}


site: Håvard Rast Blok
mail:
updated: 27 July 2007