What is Operator Overloading in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Operator overloading is a programming method where operators are implemented in user-defined types with specific logic dependent on the types of given arguments.

Operator overloading makes it easier to specify a user-defined implementation for operations with one or both operands of a user-defined class or structure. This allows user-defined types to behave similarly to primitive data types. It is to note that Java doesn't supports operator overloading (It will be discussed in detail later in the article).

Let's start our discussion with advantages of operator overloading then we will discuss operator overloading in other programming language followed by operator overloading in Java.

Advantages of Operator Overloading

  1. Programmers can employ notation that is closer to the target domain by using operator overloading. For example, rather than saying M1.add, we may write M1 + M2 to add two matrices (M2).
  2. Operator overloading gives built-in types the same syntactic support as user-defined types.
  3. The software is simpler to grasp when operators are overloaded.

Example of Operator Overloading in C++

Code Explanation

The output of Above Code will be

Arithmetic operator + is used to add two number but in above example it is used to add string of fullname class this is called operator overloading and + is overloaded . As it perform diffrent action based on parameter passed.

Operator Overloading in Java

Does Java Support Operator Overloading?

Java doesnt Support Operator Overloading Except for one case i.e + operator it can be used to add number as well as strings.

Code Explanation

The Output of above Code will be

Output of This Code will be

In The First Example + Operator Add Two Integer value but in Second it Add Two String.

This is Called Operator Overloading. In Above Example + operator is overloaded as it perform diffrent action depends on paramter passed.

It is the only operator overloading that Java supports. Aside from that, user-defined operator overloading is not supported in Java. The handling of + for concatenating strings is the only component of Java that comes close to operator overloading in java.

Why Operator Overloading in Java is not Supported?

  1. Overloading operators causes faults in the software. Operator overloading causes programmers to become perplexed. Also, compared to other languages, dealing with languages that offer operator overloading has a higher risk of error. Method overloading is a feature of Java. Method Overloading performs the same function as operator overloading while removing the possibility of errors.

  2. One of the goals of java designers is to have a simple and clear design. They didn't want to imitate the language, but rather to have a clear, really object oriented language. Operator overloading would definitely make design more complex than it would be without it, and it will also slow down the JVM because it will have to perform extra effort to figure out what operators actually mean, reducing the opportunity to optimise the language by guaranteeing operator behaviour in Java.

  3. Another advantage of not enabling operator overloading in Java is that you can think of this. The lack of operator overloading has made the language easier to manage and process, which has led to the development of tools that process the language, such as an integrated development environment (IDE) or a refactoring tool. C++ is substantially inferior to refactoring tools.

Conclusion

  • In response to the question of whether or not Java allows operator overloading, the answer is no.

  • Only one idea in Java is similar to this: string concatenation with the plus operator.

  • Aside from that, Java does not allow you to design your own operators. Operator overloading is supported by a few other JVM-based languages, such as Java. Groovy and Kotlin are two of them. These, on the other hand, are not the same as Java.

To learn more about overloading vs overriding in java Click here