Before discussing the specifics of Java's object system, let's define what an object is. Within a computer program, an object consists of
To make the notion of object more concrete, let us consider a simple example. Assume that we want to maintain a directory containing the office address and phone number for each person in the Rice Computer Science Department. In Java, each entry in such a directory has a natural representation as an object with three fields containing the person's name, address, and phone number represented as character strings. We defer discussion about how to represent the directory itself until Section 1.4.
Each entry object must include operations to retrieve the name, address, and phone number fields, respectively.
Let's summarize the form of a directory entry as a table:
Fields: String name; String address; String phone; Methods: String getName(); String getAddress(); String getPhone();This tabular description is not legal Java syntax. We will introduce the actual syntactic details in the next section of this chapter.
The three methods getName, getAddress, and getPhone do not take any explicit arguments because they are invoked by sending a ``method call'' to an object, called the receiver, which serves as an implicit argument for the method. In Java, the code defining the method can refer to this implicit argument using the keyword this, which is reserved for this purpose.
The syntax for invoking the method m (with no arguments) on the object o is
m.o()Consider the following example: assume that a Java program can access an Entry object e and needs to get the value of the name field of this object. The method invocation
e.getName()returns the desired result.
Finger Exercise: In the DrJava programming environment, open the
program file Entry.java, compile it, and type the following
statements in the Interactions pane:
Entry e = new Entry("Corky","DH 3104","x 6042"); e.getName() e.getPhone()The first line defines a variable e as an Entry object with name field "Corky", address field "DH 3104", and phone field "x 6042". The second line is an expression that computes the name field of e. What value does the Java evaluator return for the name field of e? The phone field of e?
Before we explain the Java code in the program Entry.java defining the methods getName, getAddress, and getPhone, let us briefly explore the syntax of Java expressions and statements. Java expressions built from primitive operators look almost exactly the same in Java as in C++.
Finger Exercise: In the DrJava Interactions pane, try evaluating the following expressions:
-5 + 3 -(5 + 3) 5 % 3 5./3. 5 / 0 5./0. 3 + .1 * .1 - 3. 5 < 6 5. > 6.Java expressions directly in the Interactions pane. Did you get the answers that you expected?
Java has the same precedence rules for expressions built from primitive operations as C++.
Finger Exercise: In the DrJava Interactions pane,
try evaluating the following expressions:
72. - 32. * 1.8 (72. - 32.) * 1.8 72. - 30. - 12. 72. - (30. - 12.)Did you get the answers that you expected?
Program statements have essentially the same syntax in Java as in C++. The most common form of statement in Java is an assignment statement that introduces a new variable:
type var = expr ;In the preceding syntax template, type is a Java type name, var is a Java variable name, and expr is an expression of type compatible with the type of var. The assignment statement
int x = 5;introduces the variable x and gives it the value 5.
Finger Exercise: In the DrJava Interactions pane, try
evaluating the following statements and expressions:
int x = 5; x*x double d = .000001; double dd = d*d; dd dd*dd 1. + dd 1. + dd*ddDid you get the answers that you expected?
Java includes all of the basic statement forms found in the C++ programming language expressed in essentially the same syntax. In the remainder of this monograph, we will introduce these statement forms as they are needed. Although Java accepts most C++ syntax, many common C++ constructions are considered bad style in Java.
Note that Java treats boolean as a distinct type from int, eliminating some common sources for error in C++. For example, the test expression in a conditional statement must be of type boolean.
Finger Exercise: In the DrJava Interactions pane, try
evaluating the following sequence of statements and expressions:
int x = 7; if (x = 5) y = 0; else y = 10; yDid you get the behavior that you expected? Repeat the exercise with corrected syntax (replacinng "=" in the test expression by instead of "==").
Finger Exercise: In the DrJava Interactions pane, try evaluating the following sequence of statements and expressions:
boolean switch = (x = 7); switchDid you get the behavior that you expected? Repeat the exercise with corrected syntax (replacinng "=" in the initialization expression by "==").