The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

## Summary of Operators

### Arithmetic Operators

> The following table lists the basic arithmetic operators provided by the Java programming language. These operators can be used only on numeric values.

> Operator | Use | Description
> `+` | `op1 + op2` | Adds `op1` and `op2`
> `-` | `op1 - op2` | Subtracts `op2` from `op1`
> `*` | `op1 * op2` | Multiplies `op1` by `op2`
> `/` | `op1 / op2` | Divides `op1` by `op2`
> `%` | `op1 % op2` | Computes the remainder of dividing `op1` by `op2`

> These short cut operators increment or decrement a number by one.

> Operator | Use | Description
> `++` | `op++` | Increments `op` by 1; evaluates to the value of op before it was incremented
> `++` | `++op` | Increments `op` by 1; evaluates to the value of op after it was incremented
> `--` | `op--` | Decrements `op` by 1; evaluates to the value of op before it was decremented
> `--` | `--op` | Decrements `op` by 1; evaluates to the value of op after it was decremented

> Here are the Java programming language's other arithmetic operators.

> Operator | Use | Description
> `+` | `+op` | Promotes `op` to `int` if it's a `byte`, `short`, or `char`
> `-` | `-op` | Arithmetically negates `op`

### Relational and Conditional Operators

> Use these relational operators to determine the relationship between two values.

> Operator | Use | Returns `true` if
> `>` | `op1 > op2` | `op1` is greater than `op2`
> `>=` | `op1 >= op2` | `op1` is greater than or equal to `op2`
> `<` | `op1 < op2` | `op1` is less than `op2`
> `<=` | `op1 <= op2` | `op1` is less than or equal to `op2`
> `==` | `op1 == op2` | `op1` and `op2` are equal
> `!=` | `op1 != op2` | `op1` and `op2` are not equal

> You can use the following conditional operators to form multi-part decisions.

> Operator | Use | Returns `true` if
> `&&` | `op1 && op2` | `op1` and `op2` are both `true`, conditionally evaluates `op2`
> `||` | `op1 || op2` | either `op1` or `op2` is `true`, conditionally evaluates `op2`
> `!` | `! op` | `op` is `false`
> `&` | `op1 & op2` | `op1` and `op2` are both `true`, always evaluates `op1` and `op2`
> `|` | `op1 | op2` | either `op1` or `op2` is `true`, always evaluates `op1` and `op2`
> `^` | `op1 ^ op2` | if op1 and op2 are different--that is if one or the other of the operands is true but not both

### Shift and Logical Operators

> Each shift operator shifts the bits of the left-hand operand over by the number of positions indicated by the right-hand operand. The shift occurs in the direction indicated by the operator itself.

> Operator | Use | Operation
> `>>` | `op1 >> op2` | shift bits of `op1` right by distance `op2`
> `<<` | `op1 << op2` | shift bits of `op1` left by distance `op2`
> `>>>` | `op1 >>> op2` | shift bits of `op1` right by distance `op2` (unsigned)

> These operators perform logical functions on their operands.

> Operator | Use | Operation
> `&` | `op1 & op2` | bitwise `and`
> `|` | `op1 | op2` | bitwise `or`
> `^` | `op1 ^ op2` | bitwise `xor`
> `~` | `~op2` | bitwise complement

### Assignment Operators

> The basic assignment operator looks as follows and assigns the value of `op2` to `op1`.

> >     op1 = op2;

> In addition to the basic assignment operation, the Java programming language defines these short cut assigment operators that perform an operation and an assignment using one operator.

> Operator | Use | Equivalent to
> `+=` | `op1 += op2` | `op1 = op1 + op2`
> `-=` | `op1 -= op2` | `op1 = op1 - op2`
> `*=` | `op1 *= op2` | `op1 = op1 * op2`
> `/=` | `op1 /= op2` | `op1 = op1 / op2`
> `%=` | `op1 %= op2` | `op1 = op1 % op2`
> `&=` | `op1 &= op2` | `op1 = op1 & op2`
> `|=` | `op1 |= op2` | `op1 = op1 | op2`
> `^=` | `op1 ^= op2` | `op1 = op1 ^ op2`
> `<<=` | `op1 <<= op2` | `op1 = op1 << op2`
> `>>=` | `op1 >>= op2` | `op1 = op1 >> op2`
> `>>>=` | `op1 >>>= op2` | `op1 = op1 >>> op2`

### Other Operators

> The form of the `?:` operator is shown below. This operator returns `op2` if `op1` is true or returns `op3` if `op1` is false.

> >     op1 ? op2 : op3

| Start of Tutorial > Start of Trail > Start of Lesson | Search