Is it possible to implement operation () overloading in Java?
Create multiple objects (for example, a and b) of the developed class.
Class-vector (one-dimensional array). Implement an operation overload for objects of this
class()
: (a(i)=b(j))
.
2 answers
There is no operator overload in vanilla/plain Java.
Yes, but if you try very hard, you can portray it-through plugins to the Java compiler.
There is for example a solution for overloading arithmetic operators - with brackets, of course, it will be more difficult. But if you think carefully, you can probably get out of it.
No, you can't override the behavior of operators in Java.
In such cases, they usually take the example of List
and create methods get
and set
:
a.set(i, b.get(j));
In principle, it would be logical for this class to implement the List
interface or use one of the ready-made implementations (if the task allows the use of standard classes).
You can also look in the direction of the Kotlin language, it works under the JVM and provides a lot of syntactic sugar, including redefining the operator ()
.