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

// 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).
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.
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.
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.
// 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; 
Master Java with Deep Grasping Methodology!Learn More