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

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

## Classification by Arity

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

## Operator Categories and Syntax

### Unary Operators

Require a single operand. While all are unary by arity, they are distinguished by their syntax position (postfix vs. prefix) and precedence level. Postfix operators associate left-to-right, while prefix and other unary operators associate right-to-left.

| Operator           | Description                                                    |
| :----------------- | :------------------------------------------------------------- |
| `expr++`, `expr--` | Post-increment / Post-decrement (evaluates, then mutates)      |
| `++expr`, `--expr` | Pre-increment / Pre-decrement (mutates, then evaluates)        |
| `+expr`, `-expr`   | Unary plus (promotes type) / Unary minus (arithmetic negation) |
| `!expr`            | Logical complement (inverts boolean value)                     |
| `~expr`            | Bitwise complement (inverts bits of integer types)             |
| `(Type)`           | Type cast (explicitly converts operand to specified type)      |

```java theme={"dark"}
public class UnaryExample {
    public static void main(String[] args) {
        int a = 5;
        System.out.println(a++);        // 5 (Evaluates to 5, then a becomes 6)
        System.out.println(++a);        // 7 (a becomes 7, then evaluates to 7)
        System.out.println(-a);         // -7
        System.out.println(!false);     // true
        System.out.println(~0);         // -1 (Bitwise inversion of all 0s to all 1s)
        System.out.println((double) a); // 7.0
    }
}
```

### Arithmetic Operators

Perform standard mathematical computations. While they primarily operate on numeric types, the additive operator (`+`) is overloaded for `String` concatenation. If at least one operand is a `String`, the `+` operator converts the other operand to a `String` and concatenates them.

| Operator      | Description                                      |
| :------------ | :----------------------------------------------- |
| `+`, `-`      | Addition (or String concatenation) / Subtraction |
| `*`, `/`, `%` | Multiplication / Division / Modulo (Remainder)   |

```java theme={"dark"}
public class ArithmeticExample {
    public static void main(String[] args) {
        System.out.println(10 + 5);    // 15
        System.out.println(10 / 3);    // 3 (Integer division truncates decimal)
        System.out.println(10 % 3);    // 1
        System.out.println("Val: " + 5); // Val: 5
    }
}
```

### Relational and Equality Operators

Evaluate the magnitude or equivalence relationship between two operands, strictly returning a `boolean` value.

| Operator                                     | Description                                                                 |
| :------------------------------------------- | :-------------------------------------------------------------------------- |
| `>`, `>=`, <code>\<</code>, <code>\<=</code> | Greater than / Greater than or equal to / Less than / Less than or equal to |
| `==`, `!=`                                   | Equal to / Not equal to                                                     |

```java theme={"dark"}
public class RelationalExample {
    public static void main(String[] args) {
        System.out.println(5 > 3);  // true
        System.out.println(5 <= 3); // false
        System.out.println(5 == 5); // true
        System.out.println(5 != 3); // true
    }
}
```

### Logical Operators

Perform Boolean logic operations. The conditional operators (`&&`, `||`) exhibit short-circuiting behavior, meaning the second operand is not evaluated if the first operand determines the overall result.

| Operator | Description                        |                                                 |                                                     |
| :------- | :--------------------------------- | ----------------------------------------------- | --------------------------------------------------- |
| `&&`, \` |                                    | \`                                              | Conditional-AND / Conditional-OR (Short-circuiting) |
| `&`, \`  | \`                                 | Boolean logical AND / OR (Non-short-circuiting) |                                                     |
| `^`      | Boolean logical exclusive OR (XOR) |                                                 |                                                     |

```java theme={"dark"}
public class LogicalExample {
    public static void main(String[] args) {
        System.out.println(true && false); // false
        System.out.println(true || false); // true
        System.out.println(true ^ true);   // false
    }
}
```

### Bitwise and Bit Shift Operators

Operate directly on the binary representations of integer types (`byte`, `short`, `int`, `long`, `char`).

| Operator          | Description                                                |                                           |
| :---------------- | :--------------------------------------------------------- | ----------------------------------------- |
| `&`, \`           | `, `^\`                                                    | Bitwise AND / Inclusive OR / Exclusive OR |
| <code>\<\<</code> | Signed left shift (shifts bits left, fills with 0)         |                                           |
| `>>`              | Signed right shift (shifts bits right, preserves sign bit) |                                           |
| `>>>`             | Unsigned right shift (shifts bits right, fills with 0)     |                                           |

```java theme={"dark"}
public class BitwiseExample {
    public static void main(String[] args) {
        System.out.println(5 & 3);     // 1 (0101 & 0011 = 0001)
        System.out.println(5 | 3);     // 7 (0101 | 0011 = 0111)
        System.out.println(5 ^ 3);     // 6 (0101 ^ 0011 = 0110)
        System.out.println(16 >> 1);   // 8
        System.out.println(-16 >>> 1); // 2147483640
    }
}
```

### Assignment Operators

Assign the evaluated value of the right operand to the left operand. Compound assignment operators perform the specified operation and an implicit type cast to the type of the left-hand variable.

| Operator                     | Description                                      |                                   |
| :--------------------------- | :----------------------------------------------- | --------------------------------- |
| `=`                          | Simple assignment                                |                                   |
| `+=`, `-=`, `*=`, `/=`, `%=` | Arithmetic compound assignment                   |                                   |
| `&=`, \`                     | =`, `^=`, <code>&lt;&lt;=</code>, `>>=`, `>>>=\` | Bitwise/Shift compound assignment |

```java theme={"dark"}
public class AssignmentExample {
    public static void main(String[] args) {
        int x = 10;
        x += 5; 
        System.out.println(x); // 15
        x %= 4;
        System.out.println(x); // 3
    }
}
```

### Ternary Operator

The only operator in Java that takes three operands. It evaluates a boolean condition and returns one of two expressions based on the result.

| Operator | Description                            |
| :------- | :------------------------------------- |
| `? :`    | `condition ? exprIfTrue : exprIfFalse` |

```java theme={"dark"}
public class TernaryExample {
    public static void main(String[] args) {
        int a = 10;
        String result = (a > 5) ? "Greater" : "Lesser";
        System.out.println(result); // Greater
    }
}
```

### Type Comparison Operator (`instanceof`)

A binary operator that evaluates whether an object reference is an instance of a specific class, superclass, or interface, returning a `boolean`. As of Java 16, **Pattern Matching for `instanceof`** allows declaring a binding variable directly within the evaluation, eliminating the need for an explicit cast.

| Operator     | Description                                   |
| :----------- | :-------------------------------------------- |
| `instanceof` | Type comparison and optional pattern matching |

```java theme={"dark"}
public class InstanceofExample {
    public static void main(String[] args) {
        Object obj = "Java";
        
        // Standard instanceof
        System.out.println(obj instanceof String); // true
        
        // Pattern Matching for instanceof (Java 16+)
        if (obj instanceof String str) {
            System.out.println(str.toUpperCase()); // JAVA
        }
    }
}
```

## Precedence and Associativity

When an expression contains multiple operators, **precedence** dictates the order of evaluation. Operators with higher precedence are evaluated before those with lower precedence.

When operators of the same precedence appear in the same expression, **associativity** determines the evaluation direction:

* **Right-to-Left:** Applies to prefix Unary, Ternary, and Assignment operators.
* **Left-to-Right:** Applies to postfix Unary operators and all other binary operators.

**Precedence Hierarchy (Highest to Lowest):**

1. Postfix (`expr++`, `expr--`)
2. Unary (`++expr`, `--expr`, `+expr`, `-expr`, `~`, `!`, `(Type)`)
3. Multiplicative (`*`, `/`, `%`)
4. Additive (`+`, `-`)
5. Shift (<code>\<\<</code>, `>>`, `>>>`)
6. Relational (<code>\<</code>, `>`, <code>\<=</code>, `>=`, `instanceof`)
7. Equality (`==`, `!=`)
8. Bitwise AND (`&`)
9. Bitwise XOR (`^`)
10. Bitwise OR (`|`)
11. Logical AND (`&&`)
12. Logical OR (`||`)
13. Ternary (`? :`)
14. Assignment (`=`, `+=`, `-=`, etc.)

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