3.12 Just Biding Your Time
If you've got time to kill, there's always the nop instruction. It takes no arguments, and it doesn't do anything at all:
nop ; Do nothing
It is useful as a spacefiller during the code-generation process when writing bytecodes directly rather than using Oolong. Because the operation doesn't do anything, you can reserve space in the class file you are building with a number of nop s. Later, you can go back and replace the nop s with operations that actually do something. If you don't replace them, then the correctness of your program is not affected, because the nop s do nothing.
Of course, this works only when you are assembling class files. Once the class has been loaded into memory, it cannot be changed.
Chapter 4. Classes and Objects
Java is an object-oriented programming language. To execute a Java program on the Java virtual machine, the JVM provides support for a number of basic object-oriented programming constructs. In this chapter we will explore that part of the JVM instruction set that facilitates an object-oriented programming style.
4.1 What Is an Object?
An object is an entity with four properties:
You can think of a class as a template for objects. When an object is created using the class as a template, we say that the object is an instance of the class. The object has a slot to store the value of each nonstatic field in the class. (Static fields and methods are independent of objects; they are discussed in section 4.10.) Each instance of a class shares the same fields, though the fields may hold different values. If you know the class of an object, then you can be sure that the object has all of the fields in that class.
The class also defines what methods may be invoked on the object. When one of these methods is invoked, the class of the object determines exactly which set of code is executed. There are actually four different kinds of invocation in the JVM, each with different rules about how to select which code is executed when a method is called.
The class definition determines how fields and methods may be accessed. This protection allows the programmer to limit the amount of code that has access to these objects, making programs easier to understand and debug, as well as offering a foundation on which secure systems can be built.
In addition to the four properties described here, each object has a fifth property called a monitor . The monitor is like a lock on the object: only one thread may have possession of the monitor at a time. Monitors are discussed in section 16.3.
4.1.1 Objects and References
Objects are represented by reference s. Think of a reference as a rope attached to the object. It's not the object itself, but you can use it to get to the object. Each reference takes up one slot on the stack or local variable array, just like an int or float .
When drawing pictures of the JVM memory space, we use
Figure 4.1. JVM memory layout
In keeping with the definition of Wallaby , the object has three fields: name, age , and color . The age is one of the numeric types, so it shows the numerical value 17. The other two fields are object types. They contain reference s to other objects in the heap.
The name field of the Wallaby contains a reference to an object that is a java/lang/String , which has been abbreviated as String . A String is an object just like any other. The fields of a String are private, so you can't really see them. To avoid making the diagram even more cluttered, we have omitted the details of class String . We will just use the text of the String to represent the String . However, you must remember that it's really an object.
The color field holds a reference to an object of class Color . That object has a name as well, though this name is unrelated to the name of a Marsupial . The name of this color is the String "gray" .
There is a special
. Fields that contain references are
For example, a newly created Wallaby object looks like this before it is initialized:
|Java name||JVM name|
|Class name 1||Class name 2||Same package?|
|EDU/umd/cs||Computer Science Department, University of Maryland|
|GOV/nasa||U.S. National Aeronautics and Space Administration|
If the package name is omitted, then the class is
Many examples in this book use the default unnamed package to make the examples easier to read. It's safe in the context of the book, because it's sitting on a page, not interacting with other classes. The executable code that comes with this book uses the domain-derived package name