Question:
Is operator overloading possible in java?
arul
2007-02-07 20:17:48 UTC
does java support operation overloading concept?please help with an example
Three answers:
anonymous
2007-02-07 21:04:27 UTC
Yes, it's possible. If you need it, you should have to explain WHY you want to to a friend, and make sure that they agree that you need it prior to doing it, since it's generally a bad idea in Javar.
parashu N
2007-02-07 20:34:07 UTC
yes possible,



> The philosophy behind that may be that

> overloading is not necessarily

> intrinsically "bad", but has the potential

> to generate unintuitive code.



On the other hand, if you've ever tried to make a nice matrix Class, you realize why operator overloading is so useful. Having the option available means you are able to apply a more natural coding style when it applies. Further, I have seen far too many unintuitive method and variable names to accept this argument.



> The one supported instance of String

> concatenation is a natural and intuitive

> use of overloading.



To continue my point, having to write something like



u = v1.add(v2);



to add two vectors (as used in Physics, i.e. magnitude and direction) is much more awkward than



u = v1 + v2;



in this context, since this style of infix mathematical notation is the most natural for the problem domain of Physics.



> Also, to eliminate it just in principle for

> consistency would deprive Java programmers of

> an extremely useful timesaver.



I agree that I wouldn't be as happy programming Java without the String concatenation operator. However, my argument for consistency would be in support of a general operator overloading mechanism rather than elimination of String concatenation.
sweetboy
2007-02-07 20:40:35 UTC
yes operator overloading in java is possible but thats not suggested unless you explicity need the thing


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...