ck wrote On 03/16/07 09:46,:
Only in a trivial sense. Defining a new class or interface
"overloads" the `=', `[]', `instanceof', and (for concrete
classes) `new' operators, and creates a new cast operator. The
other operator overloadings are those built into Java, and are
not extensible.
Could you please cite this with a simple example? As far as I know,
Java does not allow operator overloading, though it has implicitly
done operator overloading in case of Strings.
Here what I did not understand is what you mean by
Only in a trivial sense. Defining a new class or interface
"overloads" the `=', `[]', `instanceof', and (for concrete
classes) `new' operators, and creates a new cast operator.
I understand "operator overloading" to mean using one
operator symbol (+ or * or >>= or whatever) to represent a
a suite of different operations that apply to different
operand types. (Maybe there's a more formal definition
floating around; I dunno.) With that in mind:
- `=' is the symbol for a family of related but
different operations. There is an `=' operator
whose operands are an int variable and an int-
valued expression; there is another `=' operator
whose operands are a String reference and a String-
valued expression. Whenever I define a new class
or interface, I automatically create a matching
`=' operator whose operands are a NewClass reference
and an expression whose value is a NewClass.
- `new' is the symbol for a family of related but
different operations. Using this symbol, I can
apply an operator that constructs a String or an
operator that constructs a BigInteger or an operator
that constructs a Gizmo. Each constructable class
I define adds a new flavor of `new' to the mix.
... and so on. As I said, it's a fairly trivial form
of overloading, yet "overloading" it certainly is as far
as I can see. (Hmmm: And I should have included == and !=
among the operators that are trivially overloaded whenever
you define a new type.)
Some people seem to make a big deal out of the fact
that the `+' operator is overloaded to handle Strings.
I don't find this at all surprising or unique: `+' is
already overloaded to handle doubles, floats, longs, and
ints. Similarly, `/' is overloaded to handle doubles,
floats, longs, and ints. The four operand types lead to
four different division operations; the `/' symbol stands
for all four of them and is thus overloaded -- again, in
this rather trivial and unsurprising way.
The long and short, though, is that you cannot get
`*' to operate on your Matrix class.