Java Reference
In-Depth Information
persthatareaccessiblefromOracle's“JavaHotSpotGarbageCollection”pageat ht-
jsp-140228.html .
Chapter4 pursuesgarbagecollectionfurtherbyintroducingyoutoJava'sReference
API, which lets your application receive notifications when objects are about to be fi-
nalized or have been finalized.
Note Throughoutthisbook,Ioftenreferto API inbothbroadandnarrowcontexts.
of Reference as APIs themselves.
The following exercises are designed to test your understanding of classes and ob-
1. Listing2-2 presentsan Image classwiththreeconstructorsanda main()
method for testing this class. Expand Image by introducing private int
fields named width and height , and a private one-dimensional byte
array field named image . Refactor the Image() constructor to invoke
the Image(String filename) constructorvia this(null) .Refact-
or the Image(String filename, String imageType) con-
structor such that, when the filename reference is not null, it creates a
byte array of arbitrary size, perhaps with the help of an expression such
as (int) (Math.random()*100000) (return a randomly generated
integer between 0 and 99999 inclusive), and assigns this array's reference
to the image field. Similarly, it assigns an arbitrary width to the width
field and an arbitrary height to the height field. If filename contains
null, it assigns -1 to each of width and height . Continuing, introduce
getWidth() , getHeight() , and getImage() methods that return
the values of their respective fields, and introduce a getSize() method
thatreturnsthelengthofthearrayassignedtothe image field(or0if im-
age containsthenullreference).Finally,refactorthe main() methodsuch
that, for each constructor, the following sequence of method calls occurs:
System.out.println("Image = "+image.getImage());
System.out.println("Size = "+image.getSize());
System.out.println("Width = "+image.getWidth());
System.out.println("Height = "+image.getHeight()); .
Search WWH ::

Custom Search