Java can't do operator overloading, but +
works okay for String
and Integer
and some other classes. How is this possible?
update:
Why does this work?
Integer i = 4;
Integer p = 5;
System.out.println(i*p); // prints 20
Java can't do operator overloading, but +
works okay for String
and Integer
and some other classes. How is this possible?
update:
Why does this work?
Integer i = 4;
Integer p = 5;
System.out.println(i*p); // prints 20
As @yan said, this is the exception, not the rule. Strings have a special status in Java. There's a whole subsection of the Java Language Specification devoted to
+
in its role as the string concatenation operator: §15.18.1.Regarding your update, that's another special case. Java is sometimes, depending on the case, smart enough to convert things that are not
String
s intoString
s whenString
s are needed. One of these special cases is the one you described, where primitives are showing up in a place that needs aString
. The primitives are first converted to their reference types —Integer
,Double
, &c. — and then intoString
s via thetoString()
method.Another special case is when one
String
and one non-String
are being combined with the string concatenation operator+
, as described in JLS §5.4 — String Conversion.For completeness:
+
in its more common "adding numbers together" role is described in the other part of of §15.18, §15.18.2 — Additive Operators (+ and -) for Numeric Types.Java doesn't allow custom operator overloading, but the compiler can still be told by the compiler developer that String1 + String2 == String1String2, and to substitute the proper concatenation method call for the + operator.
What actually is execute is
+
is not an example of operator overloading.+
is built into the language as a concatentation operator and an arithmetic-addition operator.What this means is that a person writing a program with Java cannot overload operators, but as far as the grammar of the Java language is concerned,
+
is defined as a concatenation and an addition operator.EDIT
It works for other classes such as
Integer
andDouble
because of autoboxing.If you take a look at the bytecode of a Java program that performs string concatenation, you'll see that it creates
StringBuilder
and uses theappend()
method. The Java compiler sees the+
operator and realizes that the operands are strings and not primitive types (likeint
).If you look at the bytecode of a program that does integer addition, you will see that it uses the
iadd
instruction to perform integer addition. This is because the compiler realizes that the operands to the+
operation are integers.As far as doing something like
Integer i = 4
, the bytecode will show that you're actually doingInteger i = Integer.valueOf(4)
. This is called autoboxing. Later on, when you do something likei + p
, where bothi
andp
are of typeInteger
, the generated bytecode will show that you're doingi.intValue() + p.intValue()
, where the return types of both methods areint
(the actual bytecode instruction again, isiadd
).This is why
+
worksInteger
even though they are not actual primitive types.+
is a built-in operation. It's an exception, not a rule.It works for primitive wrappers like Integer because of autoboxing.
It works for String because that's a special case for concatenating strings: