问题与练习:对象
Questions
- 以下程序有什么问题?
public class SomethingIsWrong {
public static void main(String[] args) {
Rectangle myRect;
myRect.width = 40;
myRect.height = 50;
System.out.println("myRect's area is " + myRect.area());
}
}
- 以下代码创建一个数组和一个字符串 对象。代码执行后,存在多少对这些对象的引用?这两个对象都可以进行垃圾收集吗?
...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...
- 程序如何销毁它创建的对象?
Exercises
-
修复问题 1 中显示的名为
SomethingIsWrong
的程序。 -
给定以下名为NumberHolder的类,编写一些代码来创建该类的实例,初始化其两个成员变量,然后显示每个成员变量的值。
public class NumberHolder {
public int anInt;
public float aFloat;
}