Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Any form of operator overloading supported in java?

Reply
Thread Tools

Any form of operator overloading supported in java?

 
 
sivasu.india@gmail.com
Guest
Posts: n/a
 
      03-16-2007
Java doesn't support multiple inheritance.To overcome that it has
provide interfaces. Is there any such way available for overloading of
operators also?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-16-2007
wrote:
> Java doesn't support multiple inheritance.To overcome that it has
> provide interfaces. Is there any such way available for overloading of
> operators also?


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.

--
Eric Sosman
lid
 
Reply With Quote
 
 
 
 
ck
Guest
Posts: n/a
 
      03-16-2007
On Mar 16, 5:49 pm, Eric Sosman <esos...@acm-dot-org.invalid> wrote:
> 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.


Thanks,

--
Ck
http://www.gfour.net

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-16-2007
Eric Sosman <esos...@acm-dot-org.invalid> wrote:
>> 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.


ck wrote:
> 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


StringBuffer sb = new StringBuffer();
StringBuilder bs = new StringBuilder();

The '=' and 'new' operator are (trivially) overloaded.

-- Lew
 
Reply With Quote
 
ck
Guest
Posts: n/a
 
      03-16-2007
On Mar 16, 6:58 pm, Lew <l...@nospam.lewscanon.com> wrote:
> StringBuffer sb = new StringBuffer();
> StringBuilder bs = new StringBuilder();
>
> The '=' and 'new' operator are (trivially) overloaded.
>
> -- Lew


But that's implicit. But can we do operator overloading otherwise?

--
Ck

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      03-16-2007
ck wrote:

> > The '=' and 'new' operator are (trivially) overloaded.

>
> But that's implicit. But can we do operator overloading otherwise?


No.

The language defines small, fixed, sets of overloading for some operators, but
with those (trivial[*]) exceptions, there is no operator overloading in Java.
There is no user-defined operator overloading at all.

-- chris
[*] Arguably the built-in overloading of + to call object' toString() methods
is non-trivial, but it still isn't all that interesting...


 
Reply With Quote
 
ck
Guest
Posts: n/a
 
      03-16-2007
On Mar 16, 7:19 pm, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
>
> No.
>
> The language defines small, fixed, sets of overloading for some operators, but
> with those (trivial[*]) exceptions, there is no operator overloading in Java.
> There is no user-defined operator overloading at all.
>
> -- chris
>
>[*] Arguably the built-in overloading of + to call object' toString() methods
> is non-trivial, but it still isn't all that interesting...


Thanks.

--
Ck

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      03-16-2007
ck wrote On 03/16/07 09:46,:
> On Mar 16, 5:49 pm, Eric Sosman <esos...@acm-dot-org.invalid> wrote:
>
>>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.

--

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      03-17-2007
Eric Sosman wrote:

> 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.


I think what's significant about + overloading (unlike the overloading of *
etc) is that it invokes programmer-defined code. (It isn't quite a
programmer-defined overloading, but it shares some the problems/opportunities
thereof). For instance, there is no way of creating a "surprising"
implementation of * in the way that

public Stream
toStream()
{
System.exit(0);
}

would cause + to behave unexpectedly.

-- chris


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-17-2007
Chris Uppal wrote:
> I think what's significant about + overloading (unlike the overloading of *
> etc) is that it invokes programmer-defined code. (It isn't quite a
> programmer-defined overloading, but it shares some the problems/opportunities
> thereof). For instance, there is no way of creating a "surprising"
> implementation of * in the way that
>
> public Stream
> toStream()
> {
> System.exit(0);
> }
>
> would cause + to behave unexpectedly.


If one were able to define such a method in a class that overloaded +. I don't
know of an example in Java where one could.

-- Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57