Java Reference
In-Depth Information
Objects as Parameters
In Chapter 12 you saw that any primitive variable can be passed as a argu-
ment to a method: an int, a double, a boolean, and so on. You also saw that a
string, which is an object, can also be passed as an argument. The fact is
that objects of any class can be passed as arguments. As in the case of prim-
itive data types, the method that receives the object parameter can access it
using an alias.
In the case of objects passed as parameters, there is only one copy of
the object. This means that any change on the object by the method modi-
fies this single copy and affects the original object. In other words, while
primitive data types are always passed by value, objects are passed by
reference.
Incidentally...
Java classes are abstract data types and follow many of the rules of the
primitive types that are part of the language. An object, which can be
viewed as a variable of an abstract data type, is subject to similar rules
as variables of primitive data types.
By the same token, a method can return an object. When the method
that returns an object is declared, the declaration specifies the class of
the object as a return type. Typically, the returned object is created in the
method using the new operator. Methods that receive objects as parame-
ters or that return objects are often of static type.
Consider the following case: a pixel (short for picture element) is the
smallest addressable graphic unit on the video display. You can visualize
a pixel as a small colored dot. The number of horizontal and vertical pix-
els that can be displayed determines the screen resolution. Thus, we
speak of a video system with a resolution of 640-by-480 pixels when there
are 640 individual dots in each screen row, and a total of 480 rows on the
entire screen.
The screen location of a pixel is defined in terms of the screen column
and row. Usually the column is designated as the x- coordinate, and the
row is the y -coordinate. The screen mapping is zero-based; this means
that the pixel at the top-left screen corner is located at x =0, y = 0. Simi-
larly, the pixel located at column 120 and row 62 is at coordinates x = 120,
y = 62.
Search WWH ::




Custom Search