/* * OUTPUT: * foo1: bar 1 * foo2: bar 1 * foo1 == foo2: No * foo1 == foo3: Si * foo1.equals(foo2): Si * s1,s2,s3: foo,foo,foo * s1 == s2: No * s1 == s3: Si * s1.equals(s2): Si */ import java.util.*; class Foo{ public String s; public int i; public Foo(String s, int i) { this.s = s; this.i = i; } public String toString() { return s + " " + i ; } /* public boolean equals(Foo foo) { return s.equals(foo.s) && ( i == foo.i);}*/ } public class c2 { public static void main (String args[]) { Foo foo1 = new Foo( "bar" , 1 ); Foo foo2 = new Foo( "bar" , 1 ); Foo foo3 = foo1; String s1 = "foo"; String s2 = "fo"; String s3 = "foo"; int i1=314; int i2=310; Integer i3; s2 = s2 + "o"; i2 = i2 + 4; /* i3 = i2; * Valid since Java 1.5, compiler will automatically do the conversions */ i3 = new Integer( i2 ); System.out.println( "foo1: " + foo1 ); System.out.println( "foo2: " + foo2 ); System.out.println( "foo1 == foo2: " + (foo1 == foo2 ? "Si" : "No" )); System.out.println( "foo1 == foo3: " + (foo1 == foo3 ? "Si" : "No" )); System.out.println( "foo1.equals(foo2): " + (foo1.equals(foo2) ? "Si" : "No" )); System.out.println( "s1,s2,s3: " + s1 + "," + s2 + "," + s3 ); System.out.println( "s1 == s2: " + (s1 == s2 ? "Si" : "No" )); System.out.println( "s1 == s3: " + (s1 == s3 ? "Si" : "No" )); System.out.println( "s1.equals(s2): " + (s1.equals(s2) ? "Si" : "No" )); System.out.println( "i1,i2,i3: " + i1 + "," + i2 + "," + i3 ); System.out.println( "i1 == i2: " + (i1 == i2 ? "Si" : "No" )); /* * Next line gives syntax error with Java < 1.5 * Incompatible type for int System.out.println( "i1 == i3: " + (i1 == i3 ? "Si" : "No" )); System.out.println( "i3.equals(i2): " + (i3.equals(i2) ? "Si" : "No" )); */ System.out.println( "i1 == i3: " + (i1 == i3.intValue() ? "Si" : "No" )); System.out.println( "i3.equals(i2): " + (i3.equals(new Integer(i2)) ? "Si" : "No" )); } }