> ## 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 String Concatenation

The `+` 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

```java theme={"dark"}
// Unary operation
+operand

// Binary operation
operand1 + operand2
```

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

1. If either operand is of type `double`, the other is promoted to `double`.
2. Otherwise, if either operand is `float`, the other is promoted to `float`.
3. Otherwise, if either operand is `long`, the other is promoted to `long`.
4. Otherwise, both operands are promoted to `int` (this includes `byte`, `short`, and `char`).

```java theme={"dark"}
short x = 10;
float y = 5.5f;

// x is promoted to float before addition. Result is float.
float result = x + y; 
```

## 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., `42` becomes `"42"`).
* **Objects:** Converted by invoking their `toString()` method. If the object reference is `null`, it is converted to the literal string `"null"`.

In modern Java (Java 9+), string concatenation via the `+` 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.

```java theme={"dark"}
String prefix = "Value: ";
Object obj = null;

// Evaluates to "Value: null"
String result = prefix + obj; 
```

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

```java theme={"dark"}
byte b = 50;

// +b promotes the byte to an int. 
// byte result = +b; // Compilation error: incompatible types
int result = +b; 
```

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

```java theme={"dark"}
// 1. 10 + 20 evaluates as arithmetic addition -> 30
// 2. 30 + "A" evaluates as string concatenation -> "30A"
String a = 10 + 20 + "A"; 

// 1. "A" + 10 evaluates as string concatenation -> "A10"
// 2. "A10" + 20 evaluates as string concatenation -> "A1020"
String b = "A" + 10 + 20; 
```

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