4 ms·
> I almost never used operator overloading in C# or Java. Dude, you can't overload operators in Java. You can overload methods of a class, but not operators. O
by S4M 12y ago
> I almost never used operator overloading in C# or Java.
Dude, you can't overload operators in Java. You can overload methods of a class, but not operators. Operator overloading means that you can overload '+' for example for your particular class, which would enable you to write code like (in Java):
Matrix a = zeroMatrix(4,4);
Matrix b = identityMatrix(4,4);
Matrix c = a+b;
You can't do that, so you would have to write:
Matrix c= a.add(b);
Which makes your code verbose and makes Java (and Go) painful for doing numerical analysis.
- nordsieck 12y agoIt's important to note that numerical calculation is a domain particularly suited to operator overloading simply because the underlying domain (math) already uses operator overloading heavily (e.g. multiplication means something different when done on scalars and vectors, but in both cases it is well defined and in common usage).
- Someone 12y agoYou should leave out the 'overloading': - Math (and it's very close relative logic) are the only domains that use operators, period. - other domains use overloading; homonyms are very common.
- philbarr 12y agoAs it stands you can't in Java but you can in C#, although it's usually a really bad idea.
- S4M 12y agoMy comment only applies for Java - I don't know C#. As you say, it's usually a bad idea but it's quite reasonable to do for numerical analysis, as your sibling comment says.
- drdaeman 12y agoWhich leads us to that ugly a.equals(b) instead of sane a == b. When not abused, operator overloading is a bless.
- rmrfrmrf 12y agoExcept that a == b and a.equals(b) are two different things.
- jamra 12y agoYou are right. Can you tell which language I spent more time with?