+ not an example of operator overloading. + is built into the language as a concatenation operator and an arithmetic addition operator.
This means that a person writing a program with Java cannot overload operators, but as for Java grammar, + is defined as concatenation and addition operator.
EDIT
It works for other classes like Integer and Double due to autoboxing .
If you look at the bytecode of a Java program that concatenates strings, you will see that it creates a StringBuilder and uses the append() method. The Java compiler sees the + operator and understands that the operands are strings, not primitive types (for example, int ).
If you look at the bytecode of a program that adds an integer, you will see that it uses the iadd instruction to perform an integer add. This is because the compiler understands that the operands for the + operation are integers.
To do something like Integer i = 4 , the bytecode will show that you are really doing Integer i = Integer.valueOf(4) . This is called autoboxing. Later, when you do something like i + p , where both i and p are of type Integer , the generated bytecode will show that you are doing i.intValue() + p.intValue() , where the return types of both methods are int ( real bytecode instruction again, iadd ).
This is why + Integer works, although they are not actual primitive types.
Vivin paliath
source share