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

An operator in Java is a special symbol that directs the compiler to perform specific mathematical, relational, logical, bitwise, or structural operations on one, two, or three operands, yielding a single result. Operators are fundamentally characterized by their arity (unary, binary, or ternary), precedence (evaluation order), and associativity (evaluation direction).

## Arity Classification

* **Unary:** Requires a single operand.
* **Binary:** Requires two operands.
* **Ternary:** Requires three operands.

## Operator Categories and Syntax

### 1. Arithmetic Operators (Binary)

Perform standard mathematical computations on numeric types. The additive operator is overloaded to also perform String concatenation when at least one operand is a `String`.

* `+` : Additive / String Concatenation
* `-` : Subtraction
* `*` : Multiplicative
* `/` : Division (integer division truncates towards zero)
* `%` : Modulo (remainder)

```java theme={"dark"}
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
String concatenated = "Result: " + sum;
```

### 2. Unary Operators

Perform operations on a single operand, such as incrementing/decrementing a value, negating an expression, or inverting the value of a boolean.

* `+` : Unary plus (performs unary numeric promotion, such as promoting a `byte` or `short` to an `int`; it does not change the sign of a negative value)
* `-` : Unary minus (negates an expression)
* `++` : Increment (adds 1)
* `--` : Decrement (subtracts 1)
* `!` : Logical complement (inverts boolean value)

*Technical Note on Evaluation:* Java evaluates expressions strictly left-to-right. For postfix operators, the variable is evaluated and incremented immediately, but the *result* of the postfix expression is the variable's original value prior to the increment. It does not delay the increment until after the enclosing expression completes.

```java theme={"dark"}
byte b = 5;
int promoted = +b; // Unary numeric promotion to int

int x = 5;
int prefixResult = ++x; // x becomes 6 immediately, prefixResult is 6

int y = 5;
int postfixResult = y++; // y becomes 6 immediately, but postfixResult is 5

boolean isFalse = !true;
```

### 3. Assignment Operators (Binary)

Bind the evaluated result of the right-hand expression to the left-hand variable. Assignment operators evaluate right-to-left.

* `=` : Simple assignment
* `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `^=`, `|=`, `<<=`, `>>=`, `>>>=` : Compound assignments.

*Technical Note on Compound Assignment:* According to JLS 15.26.2, a compound assignment expression of the form `E1 op= E2` is equivalent to `E1 = (T) ((E1) op (E2))`, where `T` is the type of `E1`, **except that `E1` is evaluated only once**. This is a critical semantic rule for expressions with side effects. For example, in `arr[i++] += 2`, the index `i` is evaluated and incremented only once, whereas `arr[i++] = arr[i++] + 2` would evaluate and increment `i` twice.

```java theme={"dark"}
int variable = 10;
variable = 20;
variable *= 2 + 3; // Equivalent to: variable = (int) ((variable) * (2 + 3));

int[] arr = {1, 2, 3};
int i = 0;
arr[i++] += 5; // i is incremented only once. arr[0] becomes 6.
```

### 4. Relational Operators (Binary)

Evaluate the magnitude relationship between two operands, strictly returning a `boolean` (`true` or `false`).

* `>` : Greater than
* `<` : Less than
* `>=` : Greater than or equal to
* `<=` : Less than or equal to

```java theme={"dark"}
int operand1 = 15;
int operand2 = 20;
boolean isGreaterOrEqual = operand1 >= operand2;
```

### 5. Equality Operators (Binary)

Evaluate whether two operands are equivalent or not equivalent, returning a `boolean`. According to the Java Language Specification, equality operators have a *lower* precedence than relational operators.

* `==` : Equal to
* `!=` : Not equal to

```java theme={"dark"}
int val1 = 5;
int val2 = 5;
boolean isEquivalent = (val1 == val2);
```

### 6. Logical Operators (Binary)

Perform boolean logic on boolean operands. These operators utilize short-circuit evaluation; the second operand is not evaluated if the first operand definitively determines the overall result.

* `&&` : Conditional-AND (returns `true` if both are `true`)
* `||` : Conditional-OR (returns `true` if either is `true`)

```java theme={"dark"}
boolean expr1 = true;
boolean expr2 = false;
boolean logicalAnd = expr1 && expr2;
```

### 7. Bitwise and Bit Shift Operators (Unary/Binary)

Perform operations on the individual bits of integer data types (`byte`, `short`, `int`, `long`, `char`). The `&`, `|`, and `^` operators can also be applied to `boolean` operands to function as non-short-circuiting logical operators. Applying the bitwise complement (`~`) or any bit shift operator (`<<`, `>>`, `>>>`) to a `boolean` operand results in a compilation error.

* `~` : Unary bitwise complement (inverts bit pattern)
* `&` : Bitwise AND
* `|` : Bitwise inclusive OR
* `^` : Bitwise exclusive OR
* `<<` : Signed left shift (shifts bits left, filling with zero)
* `>>` : Signed right shift (shifts bits right, propagating the sign bit)
* `>>>` : Unsigned right shift (shifts bits right, filling with zero regardless of sign)

```java theme={"dark"}
int intOp1 = 0b1010;
int bitwiseComplement = ~intOp1;
int leftShift = intOp1 << 2;

boolean boolOp1 = true;
boolean boolOp2 = false;
boolean nonShortCircuitAnd = boolOp1 & boolOp2;
```

### 8. Ternary Operator

The only operator in Java that takes three operands. It acts as a condensed conditional expression.

* `? :` : Ternary conditional

```java theme={"dark"}
boolean condition = true;
int result = condition ? 1 : 0;
```

### 9. Type Comparison Operator (Binary)

Compares an object reference to a specific reference type (class, subclass, or interface), returning a `boolean`.

* `instanceof` : Type comparison

```java theme={"dark"}
Object obj = "Hello";
boolean isString = obj instanceof String;
```

### 10. Lambda Operator (Binary)

Introduced in Java 8, the arrow operator separates the parameter list from the body of a lambda expression, enabling functional programming constructs.

* `->` : Lambda operator

```java theme={"dark"}
java.util.function.IntUnaryOperator square = (int n) -> n * n;
```

## Precedence and Associativity

When an expression contains multiple operators, Java uses precedence and associativity rules to determine the order of evaluation.

* **Precedence:** Operators with higher precedence are evaluated before operators with lower precedence (e.g., `*` evaluates before `+`, and `<` evaluates before `==`).
* **Associativity:** Determines the evaluation order of operators with the *same* precedence.
  * *Right-to-Left:* Unary operators, Assignment operators, and the Ternary operator.
  * *Left-to-Right:* All other binary operators.

```java theme={"dark"}
int op1 = 2;
int op2 = 3;
int op3 = 4;
int precedenceResult = (op1 + op2) * op3; // Parentheses override default precedence
```

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