Java Reference
In-Depth Information
Display 5.21
Demonstrating the Class
Person
1
public
class
PersonDemo
2{
3
public static void
main(String[] args)
4
{
5
Person bach =
6
new
Person("Johann Sebastian Bach",
7
new
Date("March", 21, 1685),
new
Date("July", 28, 1750));
8
Person stravinsky =
9
new
Person("Igor Stravinsky",
10
new
Date("June", 17, 1882),
new
Date("April", 6, 1971));
11
Person adams =
12
new
Person("John Adams",
13
new
Date("February", 15, 1947),
null
);
14
System.out.println("A Short List of Composers:");
15
System.out.println(bach);
16
System.out.println(stravinsky);
17
System.out.println(adams);
18
Person bachTwin =
new
Person(bach);
19
System.out.println("Comparing bach and bachTwin:");
20
if
(bachTwin == bach)
21
System.out.println("Same reference for both.");
22
else
23
System.out.println("Distinct copies.");
24
if
(bachTwin.equals(bach))
25
System.out.println("Same data.");
26
else
27
System.out.println("Not same data.");
28
}
29
}
Sample Dialogue
A Short List of Composers:
Johann Sebastian Bach, March 21, 1685-July 28, 1750
Igor Stravinsky, June 17, 1882-April 6, 1971
John Adams, February 15, 1947-
Comparing bach and bachTwin:
Distinct copies.
Same data.