Java Reference
In-Depth Information
yields true because x1 and x2 contain the same value. However, variables u1
and u2 contain different values. Indeed, there are two different objects, a3 and
a4 , whose contents happen to be the same. In this situation, an equality test
u1 == u2
evaluates to false even though the two folders contain the same thing. This is
because the test is comparing the values in u1 and u2 (which are the folder names
a3 and a4) , not the contents of the folders.
The value null
If we declare a variable u3 like this,
JFrame u3= null ;
then u3 initially contains the value null , which means that u3 does not contain
the name of an object. In this situation, executing u3.getTitle() will cause an
error message to appear and execution to stop. (Try it!) So, before you try to
access components of u3 , assign a value to the variable, using a new-expression
or perhaps an assignment like u3 = u1; (which copies the name from u1 into
u3 ).
The three kinds of methods
We have already discussed two kinds of methods. Calls to functions are
expressions and produce values. Calls to procedures are statements and do not
produce values; instead, they perform some task, like billing a patient or storing
a value in some variable.
The third kind of method is the constructor , whose only purpose is to assign
initial values to fields of a folder when the folder is created. The name of a con-
structor is always the name of the class, and a constructor can be called only in
a new-expression.
Above, we used the new-expression
new JFrame("first Title")
to create a new folder of class JFrame and call method JFrame(String) to ini-
tialize the folder with the value of the argument "first Title" . Method JFrame
is a constructor.
1.3.4
Objects of class String
Now that we know about objects, we can state that a String is a class and
String value is the name of an object. After declaring a variable of type String
and assigning a value to it, as in
String s1= "xyz";
variable s1 looks like this:
 
Search WWH ::




Custom Search