Why does + work with strings in Java?

Java cannot overload the statement, but + works fine 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 
+7
source share
6 answers

+ 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.

+7
source

It works for primitive shells like Integer, due to autoboxing .

It works for String because it is a special case for string concatenation :

The Java language provides special support for the string concatenation operator (+) and for converting other objects to strings. String concatenation is implemented through the StringBuilder (or StringBuffer) class and its add method. String conversions are implemented using the toString method defined by Object and inherited by all Java classes. For more information about string concatenation and conversion, see Gosling, Joy, and Steel, Java Language Specification.

+6
source

+ is a built-in operation. This is an exception, not a rule.

+5
source

Java does not allow user operator overloading, but the compiler developer can still say that String1 + String2 == String1String2, and replace the call to the proper concatenation method for the + operator.

+2
source

The Java language provides special support for the string concatenation operator (+) and for converting other objects to strings.

 String s = "string 1" + "string 2"; 

Actually performed

 (new StringBuilder()).append("string 1").append("string 2").toString() 
+2
source

As @yan said, this is an exception, not a rule. Strings have special status in Java. There is a whole subsection of the Java language specification devoted to + in its role as a string concatenation operator: Β§15.18.1 .

Regarding your update, this is another special case. Java sometimes, depending on the case, is smart enough to convert things that are not String to String when String is required. One of these special cases is the one you described where the primitives appear in the place where String is required. Initial primitives are first converted to their reference types - Integer , Double , & c. - and then to String using the toString() method.

Another special case is when one String and one non- String are combined with the string concatenation operator + , as described in JLS Β§5.4 - String Conversion .

For completeness: + in its more general role, "adding numbers together" is described in another part of Β§15.18, Β§15.18.2 - Additive operators (+ and -) for number types .

+1
source

All Articles