9.4. Operator Reference

This page documents which type combinations are valid for each operator and what type the result will be. For a conceptual overview of operators, see Expressions.

9.4.1. Arithmetic Operators

9.4.1.1. Addition (+)

Left \ Right

Int

Long

Float

Double

String

Int

Int

Long

Float

Double

String

Long

Long

Long

Float

Double

String

Float

Float

Float

Float

Double

String

Double

Double

Double

Double

Double

String

String

String

String

String

String

String

Boolean

String

Player

String

Entity

String

Block

String

Item

String

String concatenation: When either operand is a String, the result is String concatenation. The non-String operand is converted to its textual representation.

@player {{"Score: " + 42}}      # "Score: 42"
@player {{2 + "2"}}             # "22"
@player {{true + " story"}}     # "true story"

9.4.1.2. Subtraction (-)

Left \ Right

Int

Long

Float

Double

Int

Int

Long

Float

Double

Long

Long

Long

Float

Double

Float

Float

Float

Float

Double

Double

Double

Double

Double

Double

Only numeric types support subtraction.

9.4.1.3. Multiplication (*)

Left \ Right

Int

Long

Float

Double

Int

Int

Long

Float

Double

Long

Long

Long

Float

Double

Float

Float

Float

Float

Double

Double

Double

Double

Double

Double

Only numeric types support multiplication.

9.4.1.4. Division (/)

Left \ Right

Int

Long

Float

Double

Int

Int

Long

Float

Double

Long

Long

Long

Float

Double

Float

Float

Float

Float

Double

Double

Double

Double

Double

Double

Warning

Integer division: When both operands are Int or Long, division truncates toward zero:

@player {{5 / 2}}      # 2 (not 2.5!)
@player {{5 / 2.0}}    # 2.5
@player {{5 / 2D}}     # 2.5

To get decimal division, ensure at least one operand is Float or Double.

9.4.1.5. Modulo (%)

Returns the remainder after division.

Left \ Right

Int

Long

Float

Double

Int

Int

Long

Float

Double

Long

Long

Long

Float

Double

Float

Float

Float

Float

Double

Double

Double

Double

Double

Double

@player {{5 % 2}}       # 1
@player {{0.5 % 0.2}}   # 0.1

Note that modulo with floating-point types can produce imprecise results due to how floating-point arithmetic works.

Also, modulo with negative numbers follows the same sign rules as Java’s % operator. This means the result has the same sign as the dividend (left operand). For example, (-5) % 2 yields -1 rather than 2.

9.4.2. Comparison Operators

All comparison operators return Boolean.

9.4.2.1. Equality (==) and Inequality (!=)

Int

Long

Float

Double

Bool

String

Player

Entity

Block

Int

yes

yes

yes

yes

Long

yes

yes

yes

yes

Float

yes

yes

yes

yes

Double

yes

yes

yes

yes

Boolean

yes

String

yes

Player

yes

Entity

yes

Block

yes

  • Numeric types can be compared across types.

  • String comparison is case-sensitive. Use .equalsIgnoreCase() for case-insensitive comparison.

  • Player/Entity/Block compare by identity (same object).

  • Item uses == to compare type AND amount.

9.4.2.2. Relational (<, >, <=, >=)

Left \ Right

Int

Long

Float

Double

Int

yes

yes

yes

yes

Long

yes

yes

yes

yes

Float

yes

yes

yes

yes

Double

yes

yes

yes

yes

Only numeric types support relational operators.

9.4.3. Logical Operators

9.4.3.1. AND (&&)

Returns true only if both operands are true.

Operands

Return Type

Boolean && Boolean

Boolean

@player {{true && true}}    # true
@player {{true && false}}   # false
@player {{false && false}}  # false

Short-circuit: If the left operand is false, the right operand is not evaluated.

# Safe null check which won't throw a NullPointerException
@if player != null && player.isOnline()
    @player You are online!
@fi

9.4.3.2. OR (||)

Returns true if either operand is true.

Operands

Return Type

Boolean || Boolean

Boolean

@player {{true || true}}    # true
@player {{true || false}}   # true
@player {{false || false}}  # false

Short-circuit: If the left operand is true, the right operand is not evaluated.

9.4.3.3. NOT (!)

Prefix unary operator that negates a Boolean.

Operand

Return Type

!Boolean

Boolean

@player {{!true}}   # false
@player {{!false}}  # true

9.4.4. Assignment Operators

These modify variables in place.

Operator

Equivalent

Description

x = y

Direct assignment.

x += y

x = x + y

Add and assign.

x -= y

x = x - y

Subtract and assign.

x *= y

x = x * y

Multiply and assign.

x /= y

x = x / y

Divide and assign.

x %= y

x = x % y

Modulo and assign.

@define Int score = 10
@var score += 5    # score is now 15
@var score *= 2    # score is now 30
@var score -= 10   # score is now 20