As with many programming languages such as C, C++ and C# (known commonly as the C family), it is possible to “overload methods” (sometimes called functions if not used in classes, such as C) in order to take a different amount of parameters so that they can be used in multiple scenarios with similar internals.
As Java is derived from the C style of languages (C family), it too can perform Method Overloading.
Methods of the same name can be declared in the same class, as long as they have different sets of parameters. (Deitel, P. & Deitel, H., 2014)
Sometimes it is not enough to only overload a method; this is where Operator Overloading comes into play. Just as the C family can Overload Methods, it can also overload Operators; this is called Operator Overloading.
Given that Java was created with the C family as a master reference to work from, it would seem reasonable to believe that it would have Operator Overloading as it’s C family counterparts do – but this is strangely not the case.
Java does not facilitate Operator Overloading.
This is because James Gosling (one of the core designers of Java) felt that it can make C++ an unreadable mess and didn’t want it in Java saying that “I left out operator overloading as a fairly personal choice because I had seen too many people abuse it in C++”. (James Gosling, 2000).
Operator Overloading is sometimes termed operator ad hoc polymorphism. Which is a specific case of polymorphism, where different operators have different implementations depending on their arguments. (Wikipedia, n.d.)
Operator Overloading means that you can use the same operator (e.g. a+b) on different data types and get slightly differing yet similar behaviours out of them.
In Java there are surprisingly only two times that Operator Overloading can be used and it is only ever used on the a+b (addition) operator.
This is for addition with int
, float
, long
and double
numerical values as well as concatenation with the String
variable types.
Java has a few very interesting casting nuances that make use of the a+b operator for overloading, as demonstrated below:
As you can see, sum1
adds 1
and 2
with the obvious mathematical output of 3
as an integer, before casting it to a String type ready for printing back to the System.out buffer.
While sum2
creates an internal temporary String of 1
and concatenates a value of 2
making for an output of 12
.
If you had to remove the .toString()
on line 8 it would throw an error (Error:(8, 23) java: incompatible types: java.lang.Integer cannot be converted to java.lang.String) because even though the value being inserted into new Integer
is a String (“1″+2), it’s output/return type is an Integer, which cannot be assigned to sum2
of type String. Hence we have to call a toString()
on the return type even though our insert type is technically a String.
References:
“The C Family of Languages: Interview with Dennis Ritchie, Bjarne Stroustrup, and James Gosling” (2000) – Available from: http://www.gotw.ca/publications/c_family_interview.htm (Accessed on 16th April 2017)
“Operator overloading” (n.d.) – Available from: https://en.wikipedia.org/wiki/Operator_overloading (Accessed on 16th April 2017)
“JFront – Operator Overloading for Java” (1999) – Available from: http://gginc.duckdns.org/jfront/ (Accessed on 16th April 2017)
“Deitel, P. & Deitel, H.” (2014) – Java: How to program: early objects, 10th ed. Upper Saddle River: Prentice Hall. (Accessed on 16th April 2017)