Java Reference
In-Depth Information
1
public class
ShowErrors {
2
public void
method1() {
3 Circle c;
4 System.out.println(
"What is radius "
5 + c.getRadius());
6 c =
new
Circle();
7 }
8 }
1
public class
ShowErrors {
2
public static void
main(String[] args) {
3 C c =
new
C(
5.0
);
4 System.out.println(c.value);
5 }
6 }
7
8
class
C {
9
int
value =
2
;
10 }
(c)
(d)
8.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 }
8.13
What is the printout of the following code?
public class
A {
private boolean
x;
public static void
main(String[] args) {
A a =
new
A();
System.out.println(a.x);
}
}
The Java API contains a rich set of classes for developing Java programs.
Key
Point
Listing 8.1 defined the
SimpleCircle
class and created objects from the class. You will fre-
quently use the classes in the Java library to develop programs. This section gives some exam-
ples of the classes in the Java library.
8.6.1 The
Date
Class
In Listing 2.6, ShowCurrentTime.java, you learned how to obtain the current time using
System.currentTimeMillis()
. You used the division and remainder operators to extract