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 performs either arithmetic addition or string concatenation depending on the types of its operands. It functions as both a unary and a binary operator, subject to strict type promotion, evaluation, and constant folding rules defined by the Java Language Specification (JLS).
public class PlusOperatorMechanics {
    public static void main(String[] args) {
        // Unary plus triggering numeric promotion
        byte b = 10;
        int unaryResult = +b; 
        System.out.println(unaryResult);

        // Binary arithmetic addition
        int sum = 10 + 20;
        System.out.println(sum);

        // String concatenation
        String concatenated = "Result: " + sum;
        System.out.println(concatenated);
    }
}

Binary Arithmetic Addition

When both operands are of numeric primitive types (or their corresponding wrapper classes, which are automatically unboxed), the + operator performs arithmetic addition. This operation triggers Binary Numeric Promotion before the addition occurs, following these sequential rules:
  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 (this applies to byte, short, and char).
The return type of the evaluated expression strictly matches the promoted type.

String Concatenation

If at least one operand in the binary + expression is of type String, the operator performs string concatenation. The non-string operand is subjected to string conversion:
  • Primitives: Converted as if by String.valueOf(primitive).
  • Objects: Converted by invoking the object’s toString() method. If the object reference is null, it is converted to the string literal "null".
Runtime Compilation Mechanics:
  • In Java 8 and earlier, the javac compiler translates runtime string concatenation into StringBuilder instantiations and .append() chains.
  • In Java 9 and later (JEP 280), the compiler uses the invokedynamic instruction to defer the concatenation strategy to the JVM at runtime via java.lang.invoke.StringConcatFactory. This allows the JVM to optimize memory allocation and execution speed without requiring recompilation.

Compile-Time Constant Expressions (Constant Folding)

When the + operator is applied exclusively to compile-time constants (e.g., literal values or final variables initialized with constant expressions), the Java compiler performs constant folding. The expression is evaluated at compile time rather than at runtime:
  • Numeric Constants: An expression like 1 + 2 is compiled directly into the bytecode as the literal value 3.
  • String Constants: An expression like "a" + "b" is compiled directly as the single string literal "ab". Furthermore, string literals concatenated via constant expressions are automatically interned by the compiler. They point to the exact same instance in the String Pool as if they were declared as a single, unified literal.

Unary Plus

The unary + operator precedes a single numeric operand. While it mathematically yields the identical value of the operand (it does not alter the sign), it triggers Unary Numeric Promotion. Operands of type byte, short, or char are implicitly widened to int, while int, long, float, and double remain unchanged.

Precedence and Associativity

  • Associativity: Left-to-right. In an expression like operand1 + operand2 + operand3, operand1 + operand2 is evaluated first. This strict left-to-right evaluation dictates when the operation switches from arithmetic addition to string concatenation if types are mixed.
  • Precedence:
    • The unary + has high precedence, evaluated after postfix operators but before multiplicative operators.
    • The binary + has additive precedence. This is lower than multiplicative operators (*, /, %) but higher than shift operators (<<, >>, >>>), relational operators (<, >, <=, >=, instanceof), and equality operators (==, !=).
Master Java with Deep Grasping Methodology!Learn More