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

# Java Unary Plus

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

```java theme={"dark"}
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 (`==`, `!=`).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
