Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The + 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.
int unaryResult = +5;
int binaryResult = 10 + 20;
String concatResult = "Result: " + 30;

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, or char, it is implicitly widened to an int.
  • If the operand is an int, long, float, or double, 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:
  1. If either operand is of type double, the other is widened to double.
  2. Otherwise, if either operand is float, the other is widened to float.
  3. Otherwise, if either operand is long, the other is widened to long.
  4. Otherwise, both operands are widened to int (even if both are byte or short).
If the sum of the operands exceeds the maximum value of the resulting type, Java performs silent numeric overflow without throwing an 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 (Infinity or -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., 42 becomes "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".
Compilation Mechanics: Prior to Java 9, the compiler translated string concatenation using + 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.
int a = 10;
int b = 20;
int c = 30;

// Left-to-right associativity dictates evaluation order
int result = a + b + c; 

// The compiler evaluates the above statement as:
int explicitResult = ((a + b) + c);
Because of left-to-right associativity, mixing numeric addition and string concatenation in the same expression requires explicit grouping via parentheses to prevent unintended string coercion of numeric types.
Master Java with Deep Grasping Methodology!Learn More