Java Reference
In-Depth Information
Here is a test program for using the
isEqual
predicate:
public static void main(String[] args)
{
Date day1=new Date(23,12,1971);
Date day2=day1; // beware not copying here: shallow copying
Date day3=new Date(23,12,1971);
System.out.println(isEqual(day1,day3));
System.out.println(day1);
System.out.println(day2);
System.out.println(day3);
}
Running this program, we get the following console output:
true
Date@3e25a5
Date@3e25a5
Date@19821f
This shows that
day1
and
day3
are identical (that is, they match field by
field ) although their references are different. Figure 5.3 illustrates the object
creations: Their references are stored into the local stack function and the
objects are stored into the global (persistent) memory.
Date day1=new Date(23,12,1971);
Date day2=day1;
Date day3=new Date(23,12,1971);
...
Date@
dd=23
mm=12
yyyy=1971
Date@
dd=23
mm=12
yyyy=1971
Figure 5.3
Testing for object
equality using a tailored predicate
that compares field by field the
objects.
local memory
(function call stack)
global memory
5.4 Array of objects
Since newly created classes also define new corresponding types, we can create
an array of objects. For example, to create an array of objects of type
Date
,
we use the following syntax:
Date [ ] tabDates=new Date[31];
Search WWH ::
Custom Search