Chapter 07 The Object Memory mode
Review questions
-------------------------------------------------------------------------------
1. Define the following terms: bit, byte, and word.
Answer:
bit: can be in one of two possible states, 0, and 1
byte: 8bits
word: 32bits or 4 bytes
-------------------------------------------------------------------------------
2. What is the etymology of the word bit?
Answer:
a unit of measurement of information (from binary + digit)
-------------------------------------------------------------------------------
3. How many bytes of memory are there in a 384MB machine?
Answer:
384MB = 1024 * 1024 * 384 = 402653184B
-------------------------------------------------------------------------------
4. Convert each of the following decimal numbers to its hexadecimal equivalent:
a) 17
b) 256
c) 1729
d) 2766
Answer:
a) 0x10
b) 0x100
c) 0x6c1
d) 0xace
-------------------------------------------------------------------------------
5. Convert each of the following hexadecimal numbers to decimal:
a) 17
b) 64
c) CC
d) FAD
Answer:
a) 23
b) 100
c) 204
d) 4013
-------------------------------------------------------------------------------
6. What is an address?
Answer:
the byte's index position in the bytes' sequence
-------------------------------------------------------------------------------
7. How many bytes does Java assign to a value of type int? How many bytes are
required for a double?
Answer:
int: 4
double: 8
-------------------------------------------------------------------------------
8. What are the three memory regions in which values can be stored in a Java
program?
Answer:
memory devoted to the program code and static data
stack
heap
-------------------------------------------------------------------------------
9. Using the example in section 7.2 as a model, trace the heap and stack
operations that occur in the execution of the following method:
public void run() {
Rational x = new Rational(4, 5);
Rational y = new Rational(5, 2);
Rational z = x.multiply(y).subtract(z);
println(x + " x " + y + " - " + y + " = " + z);
}
Which objects on the heap are garbage when the println statement is reached.
Answer:
x, y
-------------------------------------------------------------------------------
10. True or false: When you pass a primitive data value from one method to
another, Java always copies that value into the frame of the method being
called.
Answer:
true
-------------------------------------------------------------------------------
11. True or false: When you pass an object from one method to another, Java copies the data in that object into the new frame.
Answer:
false
-------------------------------------------------------------------------------
12. Describe the two phases in a simple mark-and-sweep garbage collector.
Answer:
(不理解题意)
-------------------------------------------------------------------------------
13. What is meant by the term wrapper class? What purpose do wrapper classes
serve?
Answer:
encapsulate each of the primitive types in a full-fledeged object
to substitue the primitive types in some situation
-------------------------------------------------------------------------------
14. What methods can you use to convert between integers and the string
representations of those integers in a particular base?
Answer:
pasrseInt(String str int radix)
toString(int i, int radix)
-------------------------------------------------------------------------------
15. What property identifies a linked structure?
Answer:
contains references to other objects
-------------------------------------------------------------------------------
16. Given that objects of a particular class require a certain amount of space
in memory, it is clear that an object in the heap could never physically
contain another object of that same class and still have room for additional
data. What can a Java programmer do to achieve the same effect?
Answer:
Class p = new Class;
-------------------------------------------------------------------------------
17. What is a stub?
Answer:
A simple implementation that you intend to replace later.
-------------------------------------------------------------------------------
18. Why is important for Java to include the special value null? What does this value represent?
Answer:
The constant null represents a reference to a nonexistent value and can be assigned to any variable that holds an object reference.
----------------------------------------------------------------------------------------------------------
1. Use the static methods Integer.parseInt and Integer.toString to write a
program that converts hexadecimal values into their decimal equivalents. Your
program should continue to read hexadecimal values until the user enters a 0. A
sample run of this program might look like this:
2. The fact that the Integer.parseInt method makes it possible for a program to read
user input as a string and then convert it to an integer makes it possible to write
programs that use something other than an integer—such as a blank line—as a
188 The Art and Science of Java
sentinel to signal the end of the input. Rewrite the AverageList program from
exercise 4-6 so that it uses a blank line to mark the end of the input.
3. But don’t panic. Base 8 is just like base 10 really—if you’re missing two fingers.
—Tom Lehrer, “The New Math,” 1965
Rewrite the Math Quiz program from exercise 6-5 so that it poses its questions in
base 8 instead of base 10, as shown in the following sample run:
4. The Runtime class in the java.lang package includes a few simple methods that
may help you get a better sense of what Java’s garbage collector does. A Runtime
object maintains information about the state of the Java Virtual Machine. If you want
to look at that information, you can get the current runtime environment by calling
the static method getRuntime() and storing the result in a variable like this:
Runtime myRuntime = Runtime.getRuntime();
Once you have this variable, you can find out how much free memory is available by
calling
myRuntime.freeMemory();
Because memory sizes can be large, the value returned by freeMemory is a long
rather than an int and indicates the number of bytes available. You can also
explicitly trigger the garbage collector by calling
myRuntime.gc();
Write a program that allocates 10000 Rational objects without saving any of them
in variables so that they all become garbage. Once you’ve done so, measure the
amount of free memory before and after garbage collection and use the difference to
report how many bytes were freed, as shown in the following sample run: