78
Working with Objects
M
T
W
R
F
S
S
DAY
DAY
4
P2/V4/sqc4 TY Java in 21 Days 030-4 Josette 12.21.95
Ch04 LP#4
Q&A
Q I'm confused about the differences between objects and the primitive data types,
such as
int
and
boolean
.
A The primitive types in the language (
byte
,
short
,
int
,
long
,
float
,
double
, and
char
)
represent the smallest things in the language. They are not objects, although in many
ways they can be handled like objects--they can be assigned to variables and passed in
and out of methods. Most of the operations that work exclusively on objects, however,
will not.
Objects usually represent instances of classes and as such, are much more complex
data types than simple numbers and characters, often containing numbers and
characters as instance or class variables.
Q In the section on calling methods, you had examples of calling a method with a
different number of arguments each time--and it gave a different kind of result.
How is that possible?
A That's called method overloading. Overloading enables the same function name to have
different behavior based on the arguments it's called with--and the number and type
of arguments can vary. When you define methods in your own classes, you define
separate method signatures with different sets or arguments and different definitions.
When that method is called, Java figures out which definition to execute based on the
number and type of arguments with which you called it.
You'll learn all about this on Day 6.
Q No operator overloading in Java? Why not? I thought Java was based on C++,
and C++ has operator overloading.
A Java was indeed based on C++, but it was also designed to be simple, so many of
C++'s features have been removed. The argument against operator overloading is that
because the operator can be defined to mean anything, it makes it very difficult to
figure out what any given operator is doing at any one time. This can result in entirely
unreadable code. Given the potential for abuse, the designers of Java felt it was one of
the C++ features that was best left out.
030-4s CH04.i
1/29/96, 8:49 PM
78