Java Reference
In-Depth Information
int
jfWidth= window.getWidth();
We now look in Fig. 1.6 for a method that will change the height of the win-
dow. The only one that looks appropriate is method
setSize
. But this requires
us to give as arguments both the width and the height. So, we use the statement:
window.setSize(jfWidth, jfWidth);
Combining the steps
We can eliminate the need for variable
jfWidth
by putting function calls as
arguments to method
setLocation
. Thus, instead of using the two statements
shown above, use the following single statement:
window.setSize(window.getWidth(), window.getWidth());
Note that the arguments are evaluated before the call
setSize(…)
is executed.
Primitive values versus class values
Consider variables
x1
,
x2
,
u1
, and
u2
that are initialized as follows:
int
x1= 5;
int
x2= 5;
JFrame u1=
new
JFrame("peace");
JFrame u2=
new
JFrame("peace");
Variables
x
1 and
x2
contain the same value:
5
. Variables
u1
and
u2
, on the other
hand, contain the names of objects, as shown in Fig. 1.7. Thus, there is a funda-
mental difference between values of primitive types (like
int
) and values of
class types (like
JFrame
). This difference has several ramifications in program-
ming, one of which has to do with testing for equality, which we now discuss.
About equality
==
The expression
x1 == x2
a4
a3
5
x1
JFrame
JFrame
JFrame(String)
JFrame(String)
x2
5
title
title
toString()
equals(Object)
...
(more methods)
toString()
equals(Object)
...
(more methods)
u1
a3
peace
peace
u2
a4
Figure 1.7:
Two different
JFrame
objects with the same content
Search WWH ::
Custom Search