Jump to content

User Defined Operators

- - - - -

  • Please log in to reply
7 replies to this topic

#1
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
I remember in C# there was a way for the programmer to tell the interpreter what a it meant for a user defined type to have a generic operator applied to it.

For example if you had created a struct called vector and you wanted to be able to do this:
vector v = vector1 + vector2

I'm not sure if i'm using the correct terminology but do you know what i mean? and does java have this?

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
It's called operator overloading and java does not have this feature.

#3
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
drat. :(
Is there a reason it doesn't have it?

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I've come across some these articles before but I've never looked into them.
why java shouldn't have operator overloading - Google Search

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The short version is that C++ operator overloading can get REALLY messy, and that was the standard example of operator overloading at the time. Since Java was already abstracting away from the hardware level more than C++ does, and has a very different variable model from C++, they decided to avoid the complications that could be generated. When C# was being invented, operator overloading was the biggest complaint computational programmers had with Java, and I think MS decided it was worth bringing back. I tend to think of C# as Java with the bad language decisions fixed.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
i see. Maybe i should change my profile pic. :) I'm not surprised that the answer was along these lines though. The reason i started moving away from C# and toward Java in the first place was that A: I liked the potability, and B: It seemed to be a slightly better thought out language.

i'm still a bit bummed though because (as you may have guest) the example i gave above was what i was actually going to use it for and i don't THINK it was going to get real messy. :(

Oh well, i'll live:) Thanks for the help guys.

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

i'm still a bit bummed though because (as you may have guest) the example i gave above was what i was actually going to use it for and i don't THINK it was going to get real messy.
I can see where it can get messy but in your case I think it wouldn't be messy.

I guess you'll just need to use method calls instead. :(
Maybe something like vector = MathVector.add(vector1,vector2)

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
There are a number of aspects of Java that strike me as rather messy attempts to fix oversights in the original spec while preserving backwards compatibility. It is a really nice language, but it has some warts.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users