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 an overloaded operator that functions as a unary numeric operator, a binary arithmetic addition operator, and a binary string concatenation operator. Its behavior is strictly determined by the data types of its operands during compilation.
Unary Plus Operator
When applied to a single numeric operand,+ acts as a unary operator. While it does not alter the sign of the operand, it triggers Unary Numeric Promotion according to the Java Language Specification (JLS).
- If the operand is of type
byte,short, orchar, it is implicitly widened to anint. - If the operand is an
int,long,float, ordouble, the type remains unchanged. - If the operand is a wrapper class (e.g.,
Byte,Integer), it undergoes unboxing before promotion.
Binary Arithmetic Addition
When both operands are numeric primitives (or unboxed numeric wrapper types), the+ operator performs arithmetic addition. Before the operation executes, the JVM applies Binary Numeric Promotion to ensure both operands are of the same type:
- If either operand is of type
double, the other is widened todouble. - Otherwise, if either operand is
float, the other is widened tofloat. - Otherwise, if either operand is
long, the other is widened tolong. - Otherwise, both operands are widened to
int(even if both arebyteorshort).
ArithmeticException. The exact behavior depends on the numeric type:
- Integer Arithmetic (
int,long): The high-order bits are discarded, and the value wraps around. - Floating-Point Arithmetic (
float,double): The value does not wrap around; instead, it overflows to positive or negative infinity (Infinityor-Infinity) in accordance with IEEE 754 standards.
String Concatenation
If at least one operand in a binary+ expression is of type String, the operator performs string concatenation. The non-string operand is subjected to string conversion:
- Primitives: Converted to their string representation (e.g.,
42becomes"42"). - Object References: The JVM invokes the object’s
toString()method. - Null References: If an operand is
null, it is converted to the literal string"null".
+ into StringBuilder instantiations and chained .append() calls. Since Java 9 (JEP 280), the compiler translates + concatenation into an invokedynamic instruction that delegates to java.lang.invoke.StringConcatFactory. This allows the JVM to dynamically optimize the concatenation strategy at runtime without requiring recompilation.
Precedence and Associativity
- Precedence: The unary
+has a high precedence, evaluating before multiplicative and additive operations. The binary+has a lower precedence, evaluating after multiplicative operators (*,/,%) but before relational, equality, and assignment operators. - Associativity: The binary
+operator is strictly left-to-right associative.
Master Java with Deep Grasping Methodology!Learn More





