Java Reference
In-Depth Information
9.12
What is wrong in the following code?
1 class Test {
2 public static void main(String[] args) {
3 A a = new A();
4 a.print();
5 }
6 }
7
8 class A {
9 String s;
10
11 A(String newS) {
12 s = newS;
13 }
14
15 public void print() {
16 System.out.print(s);
17 }
18 }
9.13
What is the output of the following code?
public class A {
boolean x;
public static void main(String[] args) {
A a = new A();
System.out.println(a.x);
}
}
9.6 Using Classes from the Java Library
The Java API contains a rich set of classes for developing Java programs.
Key
Point
Listing 9.1 defined the SimpleCircle class and created objects from the class. You will
frequently use the classes in the Java library to develop programs. This section gives some
examples of the classes in the Java library.
9.6.1 The Date Class
In Listing 2.7, ShowCurrentTime.java, you learned how to obtain the current time using
System.currentTimeMillis() . You used the division and remainder operators to extract
the current second, minute, and hour. Java provides a system-independent encapsulation of
date and time in the java.util.Date class, as shown in FigureĀ 9.10.
VideoNote
Use classes
java.util.Date class
java.util.Date
+Date()
+Date(elapseTime: long)
Constructs a Date object for the current time.
Constructs a Date object for a given time in
milliseconds elapsed since January 1, 1970, GMT.
+toString(): String
+getTime(): long
Returns a string representing the date and time.
Returns the number of milliseconds since January 1,
1970, GMT.
+setTime(elapseTime: long): void
Sets a new elapse time in the object.
F IGURE 9.10
A Date object represents a specific date and time.
 
 
 
Search WWH ::




Custom Search