*Java Glossary*S2C Home **«** *Java Glossary*

Glossary Term | Description |
---|---|

Operators | Recognized java values and their ranges. |

Operator Precedence | Operator symbols order of preference. |

### Operators Top

Symbols used for mathematical and logical manipulation that are recognized by the compiler are commonly known as *operators* in Java.

Category | Description | Typical Usage |
---|---|---|

Arithmetic Operators | Used for mathematic calculations | `+, -, /, *, %, ++, --` |

Relational Operators | Used for comparison | `==, !=, <, <=, >, >=` |

Logical Operators | Used for `boolean` comparison | `&, &&, |, ||, ^, !` |

Assignment Operators | Used for assignment | `=` |

Shorthand Assignment Operators | Used for shorthand assignment | `+=, -=, /=, *=, %=, &=, |=, ^=` |

Bitwise Logical Operators | Used for bitwise comparison | `&, |, ^, ~` |

Bitwise Shift Operators | Used for bitwise shifting | `<<, >>, >>>` |

Bitwise Shorthand Assignment Operators | Used for bitwise shorthand assignment | `&=, |=, ^=, <<=, >>=, >>>=` |

Type Casting | Automatic type Conversion | `(type)` |

The `new` Operator | Object creation | `new` |

The `instanceof` Operator | Reference test | `instanceof` |

The ` ? : ` Operator | Tenary conditional | `? :` |

#### Arithmetic Operators Top

We are familiar with most of the *arithmetic operators* in the table below from everyday life and they generally work in the same way in Java. The *arithmetic operators* work with all the Java numeric primitive
types and can also be applied to the `char`

primitive type.

Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|

+ | Addition | `int b = 5; b = b + 5;` | 10 | |

- | Subtraction | `int b = 5; b = b - 5;` | 0 | |

/ | Division | `int b = 7, b = b / 22;` | 3 | When used with an `integer` type, any remainder will be truncated. |

* | Multiplication | `int b = 5; b = b * 5;` | 25 | |

% | Modulus | `int b = 5; b = b % 2;` | 1 | Holds the remainder value of a division. |

++ | Increment | `int b = 5; b++;` | 6 | See below. |

-- | Decrement | `int b = 5; b--;` | 4 | See below. |

#### Relational Operators Top

Relational Operators refer to the relationships that values can have to each other. Relational Operators produce a `true`

or `false`

result and are used with control statements such as `if`

and `while`

. Any type can be compared for equality or inequality but only types that support an *ordering* relationship can be applied for comparison. The table below clarifies this.

Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|

== | Equal to | `int a = 5; int b = 5;` | `true` | All types can be compared for equality |

!= | Not Equal to | `int a = 5; int b = 5;` | `false` | All types can be compared for inequality |

< | Less than | `int a = 5; int b = 5;` | `false` | Can be used with all numeric types and the `char` type. |

<= | Less than or equal to | `int a = 5; int b = 5;` | `true` | Can be used with all numeric types and the `char` type. |

> | Greater than | `int a = 5; int b = 5;` | `false` | Can be used with all numeric types and the `char` type. |

>= | Greater than or equal to | `int a = 5; int b = 5;` | `true` | Can be used with all numeric types and the `char` type. |

#### Logical Operators Top

Logical Operands must be the `boolean`

type and the result of a logical operation is the `boolean`

type and are used with control statements such as `if`

and `while`

. The following
table shows all possible combinations and their result.

Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|

& | `AND` | `boolean a = false; boolean b = false;` `boolean a = false; boolean b = true;` `boolean a = true; boolean b = false;` `boolean a = true; boolean b = true;` |
`false` `false` `false` `true` | Will check both operands for `true` values, even if the first operand is `false` . |

&& | Short-circuit `AND` | `if (a && b) {..}` | Same results as `AND` but if the first operand returns `false` , the second operand will not be checked (short-circuited) and `false` is returned. | |

| | `OR` | `boolean a = false; boolean b = false;` `boolean a = false; boolean b = true;` `boolean a = true; boolean b = false;` `boolean a = true; boolean b = true;` |
`false` `true` `true` `true` | Will check both operands for `true` values, even if the first operand is `true` . |

|| | Short-circuit `OR` | `if (a || b) {..}` | Same results as `OR` but if the first operand returns `true` , the second operand will not be checked (short-circuited) and `true` is returned. | |

^ | `XOR` (exclusive `OR` ) | `boolean a = false; boolean b = false;` `boolean a = false; boolean b = true;` `boolean a = true; boolean b = false;` `boolean a = true; boolean b = true;` |
`false` `true` `true` `false` | Will check both operands and return `true` if they have different boolean values. |

! | `NOT` | `boolean a = false;` `boolean a = true;` |
`true` `false` | Will check if operand is not `true` . |

#### Assignment Operators Top

The single equal sign `=`

is used for assignment in Java and we have been using this throughout the lessons so far. This operator is fairly self explanatory and takes the form *variable = expression;*. A
point to note here is that the *type* of *variable* must be compatible with the *type* of *expression*.

#### Shorthand Assignment Operators Top

The shorthand assignment operators allow us to write compact code that is is implemented more efficiently.

Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|

+= | Addition | `int b = 5; b += 5;` | 10 | |

-= | Subtraction | `int b = 5; b -= 5;` | 0 | |

/= | Division | `int b = 7, b /= 22;` | 3 | When used with an integer type, any remainder will be truncated. |

*= | Multiplication | `int b = 5; b *= 5;` | 25 | |

%= | Modulus | `int b = 5; b %= 2;` | 1 | Holds the remainder value of a division. |

&= | AND | `boolean a = false; boolean b = false;` `boolean a = false; boolean b = true;` `boolean a = true; boolean b = false;` `boolean a = true; boolean b = true;` |
`false` `false` `false` `true` | Will check both operands for `true` values and assign `true` or `false` to the first operand dependant upon the outcome of the expression. |

|= | OR | `boolean a = false; boolean b = false;` `boolean a = false; boolean b = true;` `boolean a = true; boolean b = false;` `boolean a = true; boolean b = true;` |
`false` `true` `true` `true` | Will check both operands for `true` values and assign `true` or `false` to the first operand dependant upon the outcome of the expression. |

^= | XOR | `boolean a = false; boolean b = false;` `boolean a = false; boolean b = true;` `boolean a = true; boolean b = false;` `boolean a = true; boolean b = true;` |
`false` `true` `true` `false` | Will check both operands for different boolean values and assign `true` or `false` to the first operand dependant upon the outcome of the expression. |

#### Bitwise Logical Operators Top

The bitwise logical operators perform the same operations as the logical operators discussed in the last lesson but work on a bit-by-bit basis on the integer type. The following table shows all possible combinations for a bit using `0`

and `1`

.

Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|

& | `AND` | bit a = 0 and bit b = 0 a & b bit a = 0 and bit b = 1 a & b bit a = 1 and bit b = 0 a & b bit a = 1 and bit b = 1 a & b |
`0` `0` `0` `1` | Will turn the result bit to `0` unless both bits are `1` .Useful for switching bits off. |

| | `OR` | bit a = 0 and bit b = 0 a | b bit a = 0 and bit b = 1 a | b bit a = 1 and bit b = 0 a | b bit a = 1 and bit b = 1 a | b |
`0` `1` `1` `1` | Will turn the result bit to `1` if either bits are `1` .Useful for switching bits on. |

^ | `XOR` (exclusive `OR` ) | bit a = 0 and bit b = 0 a ^ b bit a = 0 and bit b = 1 a ^ b bit a = 1 and bit b = 0 a ^ b bit a = 1 and bit b = 1 a ^ b |
`0` `1` `1` `0` | Will switch the result bit to `1` if only one of the bits is `1` otherwise the result bit is switched to 0.Useful for highlighting unmatched bits. |

~ | `NOT` | bit a = 0 ~a bit a = 1 ~a |
`1` `0` | Useful for switching bits to show the compliment of the number. |

#### Bitwise Shift Operators Top

The bitwise shift operators allow us to shift the bits that make up an integer type to the left or right by a specified amount.

Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|

<< | `Left shift` | `int a = 1234; a = a << 2;` | `4936` | Will shift the value by the specified number of bits to the left.value << numberOfBits. |

>> | `Right shift` | `int a = 1234; a = a >> 3;` | `154` | Will shift the value by the specified number of bits to the right.value >> numberOfBits. |

>>> | `Signed right shift` | `int a = -12345; >>> 4;` | `-772` | Will shift the value by the specified number of bits to the right, whilst retaining the signed bit. If the value is negative then the resultant moved in bits will be sign-extended. value >>> numberOfBits. |

#### Bitwise Shorthand Assignment Operators Top

The bitwise shorthand assignment operators allow us to write compact code that is implemented more efficiently. When using the bitwise shorthand assignment operators there is no need to cast either.

Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|

&= | Bitwise AND | `byte a = 74; a &= 124;` | 72 | Will give the Bitwise AND result of 74 and 124. |

|= | Bitwise OR | `byte a = 74; a |= 124;` | 126 | Will give the Bitwise OR result of 74 and 124. |

^= | Bitwise XOR | `byte a = 74; a ^= 124;` | 54 | Will give the Bitwise XOR result of 74 and 124. |

<<= | Left shift | `short a = 1234; a <<= 2;` | 4936 | Left shift 1234 by 2 bits. |

>>= | Right shift | `short a = 1234; a >>= 3;` | 154 | Right shift 1234 by 3 bits. |

>>>= | Signed right shift | `short a = -12345; a >>>= 4;` | -772 | Signed right shift -12345 by 4 bits. |

#### Automatic Type Conversion Top

The following table shows which types can be assigned to which other types, of course we can assign to the same type so these boxes are greyed out.

When using the table use a row for the left assignment and a column for the right assignment. So in the highlighted permutations `byte = int`

won't convert and `int = byte`

will convert.

Type | `boolean` |
`char` |
`byte` |
`short` |
`int` |
`long` |
`float` |
`double` |
---|---|---|---|---|---|---|---|---|

`boolean = ` | NO | NO | NO | NO | NO | NO | NO | |

`char = ` | NO | NO | NO | NO | NO | NO | NO | |

`byte = ` | NO | NO | NO | NO | NO | NO | NO | |

`short = ` | NO | NO | YES | NO | NO | NO | NO | |

`int = ` | NO | YES | YES | YES | NO | NO | NO | |

`long = ` | NO | YES | YES | YES | YES | NO | NO | |

`float = ` | NO | YES | YES | YES | YES | YES | NO | |

`double = ` | NO | YES | YES | YES | YES | YES | YES |

#### The `new`

Operator Top

Object creation is a three step process involving declaration, creation and assignment. We create objects using the `new`

operator which allocates space on the heap for an existing reference variable or one declared at instantiation:

```
/*
Examples of class instantiation
*/
```**public class Testinstanceof {
public static void main (String[] args) {
Cat moggy = new Cat(); ** // Declare, create and assign Cat instance
**Cat moggy2;** // Declare Cat reference
**moggy2 = new Cat();** // Allocate and assign Cat instance
**}
}**

#### The `instanceof`

Operator Top

The `instanceof`

operator can only be used with reference variables. We use the `instanceof`

operator to find out whether an object is of a particular type. Because Java is a strongly typed language, it cares about the type of the variables we declare and try to use. So just be aware when using the
`instanceof`

operator to check for a `String`

object, when you have
an instance of `Monkey`

that knows nothing about `String`

, you will get a compiler error.

#### The `? :`

Operator Top

The `? :`

tenary (takes three operands) operator can be used to replace an `if....else`

construct of the following form:

Construct | Description |
---|---|

`if....else` | |

`if (condition) { ` | Assign result of `expression1` to
var if `condition` evaluates to `true` ,otherwise assign result of `expression2` to var. |

`? :` Operator | |

`expression1 ? expression2 : expression3 ` |
`expression1` can be any expression that returns a`boolean` value.`expression2` and`expression3` can be any returned type apart from`void` .`expression2` and`expression3` must be of the same type type.
If `expression1` returns `true` evaluate `expression2` ,
otherwise evaluate `expression3` , |

### Operator Precedence Top

The table below shows the order of precedence that is invoked when Java interprets operator symbols. The list includes the special operators and all the operators are discussed thoroughly in the java section of the site. When using multiple operators in an expression it is always best practice to use parentheses to explicitly state the order of precedence for clarity and readability. The table lists order of precedence from highest to lowest.

Precedence | Operators | Operation | Associativity |
---|---|---|---|

1 | `()` | method call | left |

`[ ]` | array index | ||

`.` | method access | ||

2 | `+` | unary plus | right |

`-` | unary minus | ||

`++` | prefix/postfix increment | ||

`--` | prefix/postfix decrement | ||

`!` | boolean logical NOT | ||

`~` | bitwise NOT | ||

`(type)` | type cast | ||

`new` | object creation | ||

3 | `/` | division | left |

`*` | mutiplication | ||

`%` | modulus | ||

4 | `+` | addition or string concatenation | left |

`-` | subtraction | ||

5 | `<<` | bitwise left shift | left |

`>>` | bitwise right shift | ||

`>>>` | bitwise signed right shift | ||

6 | `<` | less than | left |

`<=` | less than or equal to | ||

`>` | greater than | ||

`>=` | greater than or equal to | ||

`instanceof` | reference test | ||

7 | `==` | equal to | left |

`!-` | not equal to | ||

8 | `&` | boolean logical AND or bitwise AND | left |

9 | `^` | boolean logical XOR or bitwise XOR | left |

10 | `|` | boolean logical OR or bitwise OR | left |

11 | `&&` | short-cricuit boolean logical AND | left |

12 | `||` | short-cricuit boolean logical OR | left |

13 | `? :` | conditional | right |

14 | `=` | assignment | right |

`+=` | shorthand addition | ||

`-=` | shorthand subtraction | ||

`/=` | shorthand division | ||

`*=` | shorthand mutiplication | ||

`%=` | shorthand modulus | ||

`&=` | shorthand boolean logical AND or bitwise AND | ||

`|=` | shorthand boolean logical OR or bitwise OR | ||

`^=` | shorthand boolean logical XOR or bitwise XOR | ||

`<<=` | shorthand bitwise left shift | ||

`>>=` | shorthand bitwise right shift | ||

`>>>=` | shorthand bitwise signed right shift |