Java Reference
In-Depth Information
15.4
Why do the following two lines of code compile but cause a runtime error?
Check
Point
Number numberRef = new Integer( 0 );
Double doubleRef = (Double)numberRef;
15.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 );
15.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());
}
}
15.7 What is wrong in the following code? (Note that the compareTo method for the
Integer and Double classes was introduced in Section 10.12.)
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 )));
}
}
15.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 )));
}
}
15.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 calendar
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 15.3. The add method is abstract in the
Calendar class, because its implementation is dependent on a concrete calendar 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.
VideoNote
Calendar and
GregorianCalendar
classes
abstract add method
constructing calendar
 
 
Search WWH ::




Custom Search