public class TestEquals
1:
2: /**
3: This is just a demonstration program to see how
4: equals and == work.
5: */
6: public class TestEquals
7: {
8: public static void main(String[] args)
9: {
10: int[] a = new int[3]; int[] b = new int[3]; int i;
11: for (i = 0; i < a.length; i++)
12: a[i] = i;
13: for (i = 0; i < b.length; i++)
14: b[i] = i;
15: if (b == a)
16: System.out.println("Equal by ==.");
17: else
18: System.out.println("Not equal by ==.");
19: if (equals(b,a))
20: System.out.println("Equal by the equals method.");
21: else
22: System.out.println("Not equal by the equals method.");
23: }
24: public static boolean equals(int[] a, int[] b)
25: {
26: boolean match;
27: if (a.length != b.length)
28: match = false;
29: else
30: {
31: match = true; //tentatively
32: int i = 0;
33: while (match && (i < a.length))
34: {
35: if (a[i] != b[i])
36: match = false;
37: i++;
38: }
39: }
40: return match;
41: }
42: }