TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
+ operator in Java is a binary and unary operator that performs arithmetic addition for numeric primitives and string concatenation for String objects. It is the only operator in the Java language overloaded to support an object type (String), with its behavior strictly determined at compile-time based on the static types of its operands.
Syntax
Binary Arithmetic Addition
When both operands are of numeric types (primitives or their corresponding wrapper classes), the+ operator performs mathematical addition. Before the operation executes, Java applies Binary Numeric Promotion to ensure both operands are of the same type. The promotion rules are evaluated in the following order:
- If either operand is of type
double, the other is promoted todouble. - Otherwise, if either operand is
float, the other is promoted tofloat. - Otherwise, if either operand is
long, the other is promoted tolong. - Otherwise, both operands are promoted to
int(this includesbyte,short, andchar).
String Concatenation
If at least one operand in a binary+ expression is explicitly of type String, the operator performs string concatenation. The non-string operand is subjected to String Conversion at runtime:
- Primitives: Converted to their string representation (e.g.,
42becomes"42"). - Objects: Converted by invoking their
toString()method. If the object reference isnull, it is converted to the literal string"null".
+ operator is implemented at the bytecode level using invokedynamic and the StringConcatFactory, replacing the older StringBuilder.append() compilation strategy for better performance and memory efficiency.
Unary Plus
When used with a single operand, the+ operator acts as the unary plus operator. It indicates a positive value (though numbers are positive by default) and forces Unary Numeric Promotion. If the operand is a byte, short, or char, it is widened to an int.
Precedence and Associativity
The binary+ operator has additive precedence, which is lower than multiplicative operators (*, /, %) but higher than relational operators (<, >, ==).
It evaluates with left-to-right associativity. This associativity is critical when mixing numeric and string operands in a single expression, as the type of the intermediate result dictates the behavior of subsequent + operations.
Master Java with Deep Grasping Methodology!Learn More





