Java GlossaryS2C Home « Java Glossary

Glossary Term Description
OperatorsRecognized java values and their ranges.
Operator PrecedenceOperator symbols order of preference.

OperatorsTop

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 OperatorsUsed for mathematic calculations+, -, /, *, %, ++, --
Relational OperatorsUsed for comparison==, !=, <, <=, >, >=
Logical OperatorsUsed for boolean comparison&, &&, |, ||, ^, !
Assignment OperatorsUsed for assignment=
Shorthand Assignment OperatorsUsed for shorthand assignment+=, -=, /=, *=, %=, &=, |=, ^=
Bitwise Logical OperatorsUsed for bitwise comparison&, |, ^, ~
Bitwise Shift OperatorsUsed for bitwise shifting<<, >>, >>>
Bitwise Shorthand Assignment OperatorsUsed for bitwise shorthand assignment&=, |=, ^=, <<=, >>=, >>>=
Type CastingAutomatic type Conversion(type)
The new OperatorObject creationnew
The instanceof OperatorReference testinstanceof
The ? : OperatorTenary conditional? :

Arithmetic OperatorsTop

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
+Additionint b = 5; b = b + 5;10
-Subtractionint b = 5; b = b - 5;0
/Divisionint b = 7, b = b / 22;3When used with an integer type, any remainder will be truncated.
*Multiplicationint b = 5; b = b * 5;25
%Modulusint b = 5; b = b % 2;1Holds the remainder value of a division.
++Incrementint b = 5; b++;6See below.
--Decrementint b = 5; b--;4See below.

Relational OperatorsTop

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 toint a = 5; int b = 5;
if (a == b) {..}
trueAll types can be compared for equality
!=Not Equal toint a = 5; int b = 5;
if (a != b) {..}
falseAll types can be compared for inequality
<Less thanint a = 5; int b = 5;
if (a < b) {..}
falseCan be used with all numeric types and the char type.
<=Less than or equal toint a = 5; int b = 5;
if (a <= b) {..}
trueCan be used with all numeric types and the char type.
>Greater thanint a = 5; int b = 5;
if (a > b) {..}
falseCan be used with all numeric types and the char type.
>=Greater than or equal toint a = 5; int b = 5;
if (a >= b) {..}
trueCan be used with all numeric types and the char type.

Logical OperatorsTop

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
&ANDboolean a = false; boolean b = false;
if (a & b) {..}

boolean a = false; boolean b = true;
if (a & b) {..}

boolean a = true; boolean b = false;
if (a & b) {..}

boolean a = true; boolean b = true;
if (a & b) {..}

false

false

false

true
Will check both operands for true values, even if the first operand is false.
&&Short-circuit ANDif (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.
|ORboolean a = false; boolean b = false;
if (a | b) {..}

boolean a = false; boolean b = true;
if (a | b) {..}

boolean a = true; boolean b = false;
if (a | b) {..}

boolean a = true; boolean b = true;
if (a | b) {..}

false

true

true

true
Will check both operands for true values, even if the first operand is true.
||Short-circuit ORif (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;
if (a ^ b) {..}

boolean a = false; boolean b = true;
if (a ^ b) {..}

boolean a = true; boolean b = false;
if (a ^ b) {..}

boolean a = true; boolean b = true;
if (a ^ b) {..}

false

true

true

false
Will check both operands and return true if they have different boolean values.
!NOTboolean a = false;
if (!a) {..}

boolean a = true;
if (!a) {..}

true

false
Will check if operand is not true.

Assignment OperatorsTop

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 OperatorsTop

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

Operator Meaning Example Result Notes
+=Additionint b = 5; b += 5;10
-=Subtractionint b = 5; b -= 5;0
/=Divisionint b = 7, b /= 22;3When used with an integer type, any remainder will be truncated.
*=Multiplicationint b = 5; b *= 5;25
%=Modulusint b = 5; b %= 2;1Holds the remainder value of a division.
&=ANDboolean a = false; boolean b = false;
if (a &= b) {..}

boolean a = false; boolean b = true;
if (a &= b) {..}

boolean a = true; boolean b = false;
if (a &= b) {..}

boolean a = true; boolean b = true;
if (a &= b) {..}

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.
|=ORboolean a = false; boolean b = false;
if (a |= b) {..}

boolean a = false; boolean b = true;
if (a |= b) {..}

boolean a = true; boolean b = false;
if (a |= b) {..}

boolean a = true; boolean b = true;
if (a |= b) {..}

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.
^=XORboolean a = false; boolean b = false;
if (a ^= b) {..}

boolean a = false; boolean b = true;
if (a ^= b) {..}

boolean a = true; boolean b = false;
if (a ^= b) {..}

boolean a = true; boolean b = true;
if (a ^= b) {..}

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 OperatorsTop

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
&ANDbit 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.
|ORbit 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.
~NOTbit a = 0
~a
bit a = 1
~a

1

0
Useful for switching bits to show the compliment of the number.

Bitwise Shift OperatorsTop

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 shiftint a = 1234; a = a << 2;4936Will shift the value by the specified number of bits to the left.

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

value >> numberOfBits.
>>>Signed right shiftint a = -12345; >>> 4;-772Will 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 OperatorsTop

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 ANDbyte a = 74; a &= 124;72Will give the Bitwise AND result of 74 and 124.
|=Bitwise ORbyte a = 74; a |= 124;126Will give the Bitwise OR result of 74 and 124.
^=Bitwise XORbyte a = 74; a ^= 124;54Will give the Bitwise XOR result of 74 and 124.
<<=Left shiftshort a = 1234; a <<= 2;4936Left shift 1234 by 2 bits.
>>=Right shiftshort a = 1234; a >>= 3;154Right shift 1234 by 3 bits.
>>>=Signed right shiftshort a = -12345; a >>>= 4;-772Signed right shift -12345 by 4 bits.

Automatic Type ConversionTop

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 = NONONONONONONO
char = NONONONONONONO
byte = NONONONONONONO
short = NONOYESNONONONO
int = NOYESYESYESNONONO
long = NOYESYESYESYESNONO
float = NOYESYESYESYESYESNO
double = NOYESYESYESYESYESYES

The new OperatorTop

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 OperatorTop

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 ? : OperatorTop

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) {
    var = expression1;
} else {
    var = expression2;
}

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 PrecedenceTop

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 callleft
[ ]array index
.method access
2+unary plusright
-unary minus
++prefix/postfix increment
--prefix/postfix decrement
!boolean logical NOT
~bitwise NOT
(type)type cast
newobject creation
3/divisionleft
*mutiplication
%modulus
4+addition or string concatenationleft
-subtraction
5<<bitwise left shiftleft
>>bitwise right shift
>>>bitwise signed right shift
6<less thanleft
<=less than or equal to
>greater than
>=greater than or equal to
instanceofreference test
7==equal toleft
!-not equal to
8&boolean logical AND or bitwise ANDleft
9^boolean logical XOR or bitwise XORleft
10|boolean logical OR or bitwise ORleft
11&&short-cricuit boolean logical ANDleft
12||short-cricuit boolean logical ORleft
13? :conditionalright
14=assignmentright
+=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

go to home page Homepage go to top of page Top