Java Reference
In-Depth Information
item1
item2
name
name
Dryer
itemNum
-1
itemNum
-1
price
0.0
price
0.0
unitsInStock
0
unitsInStock
0
item3
item4
name
name
Washer
Toaster
itemNum
2345
itemNum
8231
price
278.95
price
34.49
unitsInStock
0
unitsInStock
200
FIGURE 8-14
Effect of constructors on objects
If the values passed to a class object do not match the parameters of any constructor,
and if no type conversion is possible, a compile-time error will be generated.
Suppose that
x
is an
int
variable and the value of
x
is
25
. The statement:
System.out.println(x);
outputs:
25
However, the output of the statement:
System.out.println(myClock);
is:
Clock@11b86e7
which looks strange. (Note that when you execute a similar statement, you are likely to
get a different but similar output.) This is because whenever you create a
class
, the Java
system provides the method
toString
to that
class
. The method
toString
is used to
convert an object to a
String
object. When an object reference is provided as a
parameter to the methods
print
,
println
, and
printf
, the
toString
method is called.
Search WWH ::
Custom Search