Java Reference
In-Depth Information
13.5
Why do the following two lines of code compile but cause a runtime error?
Number[] numberArray = new Integer[ 2 ];
numberArray[ 0 ] = new Double( 1.5 );
13.6
Show the output of the following code.
public class Test {
public static void main(String[] args) {
Number x = 3 ;
System.out.println(x.intValue());
System.out.println(x.doubleValue());
}
}
13.7
What is wrong in the following code? (Note that the compareTo method for the
Integer and Double classes was introduced in Section 10.7.)
public class Test {
public static void main(String[] args) {
Number x = new Integer( 3 );
System.out.println(x.intValue());
System.out.println(x.compareTo( new Integer( 4 )));
}
}
13.8
What is wrong in the following code?
public class Test {
public static void main(String[] args) {
Number x = new Integer( 3 );
System.out.println(x.intValue());
System.out.println((Integer)x.compareTo( new Integer( 4 )));
}
}
13.4 Case Study: Calendar and GregorianCalendar
GregorianCalendar is a concrete subclass of the abstract class Calendar .
Key
Point
An instance of java.util.Date represents a specific instant in time with millisecond
precision. java.util.Calendar is an abstract base class for extracting detailed calen-
dar information, such as the year, month, date, hour, minute, and second. Subclasses of
Calendar can implement specific calendar systems, such as the Gregorian calendar, the
lunar calendar, and the Jewish calendar. Currently, java.util.GregorianCalendar for
the Gregorian calendar is supported in Java, as shown in Figure 13.3. The add method is
abstract in the Calendar class, because its implementation is dependent on a concrete cal-
endar system.
You can use new GregorianCalendar() to construct a default GregorianCalendar
with the current time and new GregorianCalendar(year, month, date) to construct
a GregorianCalendar with the specified year , month , and date . The month parameter
is 0 based—that is, 0 is for January.
The get(int field) method defined in the Calendar class is useful for extracting the
date and time information from a Calendar object. The fields are defined as constants, as
shown in Table 13.1.
Listing 13.6 gives an example that displays the date and time information for the current
time.
VideoNote
Calendar and
GregorianCalendar classes
abstract add method
construct calendar
get(field)
 
 
 
Search WWH ::




Custom Search