Java Reference
In-Depth Information
As in mathematics, the
*
operator binds more strongly than the
+
operator. That is,
x + y * 2
means the sum of
x
and
y * 2
. If you want to multiply the sum of
x
and
y
with 2, use parentheses:
(x + y) * 2
43
44
S
ELF
C
HECK
13.
Which number type would you use for storing the area of a circle?
14.
Why is the expression
13.println()
an error?
15.
Write an expression to compute the average of the values
x
and
y
.
2.6 Constructing Objects
Most Java programs will want to work on a variety of objects. In this section, you will
see how to construct new objects. This allows you to go beyond
String
objects and
the predefined
System.out
object.
To learn about object construction, let us turn to another class: the
Rectangle
class
in the Java class library. Objects of type
Rectangle
describe rectangular shapesȌ
see
Figure 9
. These objects are useful for a variety of purposes. You can assemble
rectangles into bar charts, and you can program simple games by moving rectangles
inside a window.
Note that a
Rectangle
object isn't a rectangular shapeȌit is an object that contains
a set of numbers. The numbers describe the rectangle (see
Figure 10
). Each rectangle
is described by the x- and y-coordinates of its top-left corner, its width, and its height.