Java Reference
In-Depth Information
In C and C++, programmers frequently have to write separate versions of programs to
support different computer platforms, because the primitive types are not guaranteed to
be identical from computer to computer. For example, an
int
on one machine might be
represented by 16 bits (2 bytes) of memory, on a second machine by 32 bits (4 bytes), and
on another machine by 64 bits (8 bytes). In Java,
int
values are always 32 bits (4 bytes).
Portability Tip 4.1
The primitive types in Java are portable across all computer platforms that support Java.
Each type in Appendix D is listed with its size in bits (there are eight bits to a byte)
and its range of values. Because the designers of Java want to ensure portability, they use
internationally recognized standards for character formats (Unicode; for more informa-
tion, visit
grouper.ieee.org/groups/754/
).
Recall from Section 3.2 that variables of primitive types declared outside of a method
as instance variables of a class are
automatically assigned default values unless explicitly ini-
tialized
. Instance variables of types
char
,
byte
,
short
,
int
,
long
,
float
and
double
are all
given the value
0
by default. Instance variables of type
boolean
are given the value
false
by default. Reference-type instance variables are initialized by default to the value
null
.
Simple Drawings
An appealing feature of Java is its graphics support, which enables you to visually enhance
your applications. We now introduce one of Java's graphical capabilities—drawing lines.
It also covers the basics of creating a window to display a drawing on the computer screen.
Java's Coordinate System
To draw in Java, you must understand Java's
coordinate system
(Fig. 4.17), a scheme for
identifying points on the screen. By default, the upper-left corner of a GUI component
has the coordinates (0, 0). A coordinate pair is composed of an
x
-coordinate
(the
horizon-
tal coordinate
) and a
y
-coordinate
(the
vertical coordinate
). The
x
-coordinate is the hor-
izontal location moving from
left to right
. The
y
-coordinate is the vertical location moving
from
top to bottom
. The
x
-axis
describes every horizontal coordinate, and the
y
-axis
every
vertical coordinate. Coordinates indicate where graphics should be displayed on a screen.
Coordinate units are measured in
pixels
. The term pixel stands for “picture element.” A
pixel is a display monitor's smallest unit of resolution.
+
x
(0, 0)
x
-axis
+y
(x, y)
y
-axis
Fig. 4.17
|
Java coordinate system. Units are measured in pixels.