Java Operators: Arithmetic, Logical, Conditional and more

Let us start with one of most important topics in Java Operators and Expressions in Java. Though it is not very difficult but it forms the foundation element of Java programming.

Operators:

I hope you must be aware of this term- Operators. It is the same as the ones you have learnt in Mathematics in the primary classes.

In programming, you’ll need to perform some operations as well. These operations can be arithmetic, logical or relational depending on the problem. To solve such problems, you’ll need various kinds of operators.

Operands:

Operands are basically the variables on which operations are performed using the operators.

The entire combination of various operands and operators is called expression.

Types of Operators:

Arithmetical Operators:

Operators that are used to perform mathematical operations are called Mathematical Operators. Such operators can be used to perform arithmetical calculations in a program.

Arithmetical Operators are in turn of three kinds, classified on the basis of the number of operands they perform addition on:

Unary Operator:

An arithmetical operator that performs the operation on a single operand is called a unary operator.

Example:  +, -, ++, — ; a++, a + b , b–, -a

Increment and Decrement Operator:

The ‘++’ is called Unary Increment operator and is used to increment the value of an operand by one.

E.g.: If a=2, then executing a++ or ++a will increase the value of a to 3.

Similarly the ‘–’ is called Unary Decrement operator and is used to increment the value of an operand by one.

E.g.: If a=5, then executing a – – or – – a will decrease the value of a to 4.

Prefix Operator:

When increment or decrement operator is applied before the operand, it is called prefix operator. The operator is based on the principle of, ‘CHANGE BEFORE ACTION’. It means the value of a variable will change before the operation takes place.

Example:

int x=4, t=6;

int z =++x * 4+ (--t); 

System.out.println (z);

Output:

100 //because the value of x changes instantly increments by 1(x=5) and t decrements by 1(t=5). So z=5*4*5 which is 100.

Postfix Operator:

When increment or decrement operator is applied after the operand, it is called postfix operator.

The operator is based on the principle of, ‘CHANGE AFTER ACTION’. It means the value of the variable will change after the operation takes place.

Example:  

int x=4, t=6;

int z =x++* 4 + (t--); 

System.out.println (z);

Output:

96 //because first the value of z will be evaluated by the original values of x and t. Thus the change will be done only after the execution of the statement. So z=4*4*6 which is 96.

Binary Operator:

An arithmetical operator that performs operation on two operands at a time is called a binary operator.

Operators Function
+ Performs addition and returns sum.
Performs subtraction and returns difference.
* Performs multiplication and return product.
/ Performs division and returns quotient
% Performs division and returns remainder. (called modulus or mod operator)

Relational Operators:

Operators that are used to perform relational operations are called relational operators. They are used to show the relationship between operands.

Symbol Meaning
< Less than.
<= Less than or equals to.
> Greater than.
>= Greater than or equals to.
== Equals to
!= Not Equals to

Logical Operators:

Operators that are used to perform logical operations between operands are called logical operators. They are used to solve logical problems. The three logical operators are: AND, OR and NOT.

Operator Symbol
AND &&
OR | |
NOT !

Let us understand how logical operator works:

Suppose you want to hire an employee in your company and the following are the requirements:

  • Should be an Indian Citizen and a Passport Holder
  • Can be Male or Female.
  • Should not be less than 18 years of age.

In the first requirement, the candidate should be both an Indian Citizen as well as a Passport holder. If both the conditions are not satisfied at the same time, the application will be rejected. This signifies AND operation between ‘Indian Citizen’ and ‘Passport holder’.

In the second requirement, the candidate can be a male or a female. If the candidate is female and not a male candidate, it will not result in her rejection because one of the condition is satisfied at least. This signifies OR operation between ‘Male Candidate’ and ‘Female Candidate’.

In the third requirement, the candidate should not be less than 18 years. If the candidate has met all of the above conditions but is less than 18 years of age, his candidature is rejected. This signifies NOT operation on ‘Less than 18 years’.

Now compare this to Java’s Language.

Logical AND (&&) and Logical OR (| |):

Proposition AND Result Proposition OR Result
(10>5) && (15>5) TRUE (10>5) || (15>5) TRUE
(10>5) && (15>=5) FALSE (10>5) || (15>=5) TRUE
(8>10) && (8<4) FALSE (8>10) || (8<4) FALSE
(10<=10) && (16>4) TRUE (10<=10) || (16>4) TRUE
Java Operators: Arithmetic, Logical, Conditional and more