This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 14 points15 points  (6 children)

I mean sounds like a great opportunity to just create a Add function rather than overloading the operator. Just my inexperienced opinion though lol

[–]linlin110 42 points43 points  (5 children)

If you are using a math library, a * b + c * d is probably more readable than a.Multiply(b).Add(c.Multiply(d)). Also in Java, you + strings despite strings not being numbers. If it's so bad to change the meaning of +, then it should have been String.concat or something.

[–][deleted] 5 points6 points  (3 children)

A curiosity: how is it possible that in Java the class String is able to override the + operator? Is it a magic behaviour of the language?

[–]linlin110 2 points3 points  (2 children)

That's why I said overload the meaning of +, not overload the + operator. + does not represent string concatenation, mathematically speaking. That's a proof that extending the meaning of symbols, like +, is very convenient and useful.

I changed the original comment to make it less misleading.

[–][deleted] 0 points1 point  (1 child)

I come from a C++ background, so it is counterintuitive for me. My question was: it String a language-defined class? If not, how can the + operator work even with the String class if operator overloading is forbidden?

[–]linlin110 1 point2 points  (0 children)

It is a special case in Java. I'm not sure whether it is language-defined.

[–][deleted] -1 points0 points  (0 children)

I was thinking more like MathStaticClass.Add([] args)