The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Point Objects

At the most basic level, a point is two numbers (coordinates) that we treat collectively as a single object. In mathematical notation, points are often written in parentheses, with a comma separating the coordinates. For example, (0, 0) indicates the origin, and (x, y) indicates the point x units to the right and y units up from the origin.

In Java, a point is represented by a Point object. To create a new point, you have to use the new command:

    Point blank;
    blank = new Point (3, 4);

The first line is a conventional variable declaration: blank has type Point. The second line is kind of funny-looking; it invokes the new command, specifies the type of the new object, and provides arguments. It will probably not surprise you that the arguments are the coordinates of the new point, (3, 4).

The result of the new command is a reference to the new point. I'll explain references more later; for now the important thing is that the variable blank contains a reference to the newly-created object. There is a standard way to diagram this assignment, shown in the figure.

As usual, the name of the variable blank appears outside the box and its value appears inside the box. In this case, that value is a reference, which is shown graphically with a dot and an arrow. The arrow points to the object we're referring to.

The big box shows the newly-created object with the two values in it. The names x and y are the names of the instance variables.

Taken together, all the variables, values, and objects in a program are called the state. Diagrams like this that show the state of the program are called state diagrams. As the program runs, the state changes, so you should think of a state diagram as a snapshot of a particular point in the execution.



Last Update: 2011-01-24