Java Reference
In-Depth Information
Display 6.3
Testing Arrays for Equality
(part 2 of 2)
26 System.out.println(
27 "An equalArrays method is usually a more useful test.");
28 }
29
public static boolean equalArrays
(
int
[] a,
int
[] b)
30 {
31
if
(a.length != b.length)
32
return false
;
33
else
34 {
35
int
i = 0;
36
while
(i < a.length)
37 {
38
if
(a[i] != b[i])
39
return false
;
40 i++;
41 }
42 }
43
return true
;
44 }
45 }
Sample Dialogue
c and d are not equal by ==.
== only tests memory addresses.
c and d are equal by the equalArrays method.
An equalArrays method is usually a more useful test.
Arguments for the Method
main
★
The heading for the
main
method of a program looks as if it has a parameter for an
array of base type of
String
:
public static void
main(String[] args)
The identifier
args
is in fact a parameter of type
String[]
. Because
args
is a
parameter, it could be replaced by any other nonkeyword identifier. The identifier
args
is traditional, but it is perfectly legal to use some other identifier.
We have never given
main
an array argument, or any other kind of argument,
when we ran any of our programs. So, what did Java use as an argument to plug in for