 28 Jan 2020

# ﻿﻿﻿﻿Mathematical Operators

Mathematical operations are used everywhere whether to calculate a company's payroll or the employees' number. The structure of an operation is composed of operands and operators and usually The result is assigned to variables. the statement below is an example of an operation.

int a=1 + 2

• 1 and 2 are the operands
• The sign + is the Addition operator.
• The sign=is the Assignment operator

Java supports other operators for different purposes :

• Relational and equality operators
• Logical operators
• Ternary operators
• Unary operators
• Binary operators

We'll see each one in the next sections .

# ﻿﻿﻿﻿Arithmetic operators

Java supports using the following classical arithmetic operators

• Multiplication *
• Division /
• Modulus %
• Subtraction -

# ﻿﻿﻿﻿Assignment operators

These operators are usually used to assign Mathematical operations results to variables .The most-used assignment operator is the equal operator "=", which simply assigns operations' result to variables.

Sometimes, You'll encounter Compound Assignment operators which always have the equal sign on the right side beside other operators +=, -=, *=, /=, %=, &=, ^=, !=, <<=,>>=, >>>=

This way of writing operators behaves as a shortcut that uses the previous value of the statement's variable as an operand in the whole operation.

# Relational and Equality operators

Relational operators are used to evaluate a test case before entering a protected block of code

• less than <
• greater than >
• less than or equal to<=
• greater than or equal to>=
• instanceof
• equal to ==
• not equal to !=

# Short-circuit/logical/operators

These operators are used to verify a set of tests either

• Collectively using the Conditional-And, implying that if one test fails, the whole evaluation fails. (in code we write &&)
• Or individually with the Conditional-or, implying that only one test evaluated to True is needed, the other tests aren't passed by. (in code we write ||)

# Ternary operators

Ternary operators are used to compact conditional blocks of code into one line. These conditional blocks must treat only two cases, for instance, this example

• if(test){First case;}else{Second case;}

becomes :

• Test First case : Second case

If the Test evaluates to true, the first case block is entered, otherwise, the second.

# Unary operators

These kind of operators are involved with just one expression, they don't need more than one operand unlike arithmetic operators.

• Post-unary operators expression++,expression--
• Pre-unary operators ++expression, --expression
• The unary plus +
• The unary minus: -
• The logical not: !

### Shift and Logical operators

These operators are less commonly used, They perform bits' operations :

• The signed left shift <<
• The signed right shift >>
• The unsigned right shift >>>
• Bitwise AND &
• Bitwise exclusive OR
• Bitwise inclusive OR
• Bitwise complement ~