OPERATORS AND EXPRESSIONS IN JAVA

Operators are the symbols that tell the computer what to do with a piece of data in java. An operator is used to manipulate data and give it certain properties, such as causing an expression to return a different value or performing operations on several pieces of data at once.

Operators in Java are used to perform operations on values (such as int, float, etc.) and variables. This article explains Java operators and how they are used.

OPERATORS IN JAVA

You often need to perform some arithmetical or logical operations in Java programming In such circumstances, you need some mathematical symbols to perform these tasks. Thus an operator is basically a symbol or token, which performs arithmetical or logical operations and gives meaningful results. The values, which are involved in the operation, are termed as operands.

Types of Operators

Basically, there are three types of operators in Java, which are as follows:

  • Arithmetic
  • Relational
  • Logical

Arithmetical Operators In Java

The operators, which are applied to perform arithmetical calculations in a program, are known as arithmetical operators. Some basic calculations like addition, subtraction, multiplication, division, and modulus are often needed while programming. You can apply arithmetical operators like +,-,*,/, and % respectively to carry out these calculations.

Difference between arithmetical expression and arithmetical statement.

Arithmetical ExpressionArithmetical Statement
An arithmetical expression may contain variables, constants, and arithmetical operators together to produce a meaningful result.
For statement. example, x+y, m-15, b*b-4*a*c.
If an arithmetical expression is assigned to a variable then it is known as an arithmetical Statement.
Syntax:
Variable Arithmetical Expression, z=x+y, c=m-15, b=b*b-4*a*c
Difference between arithmetical expression and arithmetical statement.

Expressions in Java

When you write a program in Java, it is necessary to modify the arithmetical expressions into Java forms. A few examples are given below to illustrate how mathematical expressions are written in Java.

Mathematical ExpressionsJava Expressions
abca*b*c
ab+bc+cda*b+b*c+c*d
a2-b2a*a-b*b
2(l+b)2*(1+b)
prt/100p*r*t/100
1/3ab+1/2cd1/3*a*b+1/2*c*d
Expressions in Java

TYPE OF ARITHMETIC OPERATORS IN JAVA

There are three types of operators in Java, which are as follows:

  1. Unary Operator
  2. Binary Operator
  3. Ternary Operator

Unary Operators

An arithmetical operator, which is applied with a single operand is called a unary operator For example. +,-,++,–.

Unary (+) Operators

This operator is applied before the operand. It is just applied as a pointer to a variable which results in the same value as a variable.

For example,

if a=8, then a will result in 8,

if a=-10, then a will result in -10

Unary (-) Operators

This operator is used in the same way as unary plus (+) It is also applied before the operand Unary minus (-) reverts the sign of an operand. For example.

if a=4, then -a will result in -4,

if a=-3.6, then -a will result in -3.6.

Unary Increment and Decrement Operators

Unary increment operators (++) increase the value of an operant by one. The unary decrement operator (–) decreases the value of an operand by one.

For example,

  • x=x+1 By applying the increment operator, it can be written as x++ or ++x.
  • p=p-1 By applying the decrement operator, it can be written as p– or –p.
Prefix

When increment or decrement operators are applied before the operand it is known as prefix operators. These operators work on the principle “CHANGE BEFORE ACTION” It means the value of the variable changes before the operation takes place.

Example of prefix increment:

p=5;

p=++p *4;

After the operation p will result in 24.

Postfix

This unary operator is used after an operand whose value is to be increased or decreased by one. This works on the principle of “CHANGE AFTER THE ACTION”. This means that the operand will be affected after the operation is performed.

Example of postfix increment:

p=5;

p=p++*4;

After the operation p will result in 21.

Note: Both prefix and postfix increment operators increase the value of an operand by 1, before the action and after the action respectively. Prefix and postfix decrement operators work vice-versa.

The precedence of the postfix operator is higher than the prefix operator i.e. if in an expression both prefix and postfix operators are applied then the postfix is operated before the prefix.

Binary Arithmetic Operators

An arithmetic operator, which deals with two operands, is known as a binary arithmetic operator.

For example, +,-,*,/, and %.

A table is given below to illustrate Binary Arithmetic Operators:

OperatorsSymbolsFormatDescriptionResults: if a 25; b=8;
Addition+a+bReturns the sum33
Subtractiona-bReturns the difference17
Multiplication*a*bReturns the product200
Division/a/bReturns an integral part3
Modulus/
Remainder
%a%bReturns the remainder1
Binary Arithmetic Operators

Ternary Operators

Ternary operator is used in java programming to replace if conditionals with a one-liner. The ternary operator evaluates its first operand as either true or false and then makes out the second operand accordingly.

The ternary operator in Java separates computer code into three sections. It is purely used to perform an operation based on a given condition. The condition can be true or false, depending on whether a given value satisfies it. Let’s see an example of ternary operator which uses Boolean values (true and false) and a variable:

Syntax:

variable = Expression1 ? Expression2: Expression3;

If operates similarly to that of the if-else statement as in Exression2 is executed if Expression1 is true else Expression3 is executed.

if(Expression1)
{
    variable = Expression2;
}
else
{
    variable = Expression3;
}

Example:

a = 10;
b = 20;

c=(a>b) ? (a+b):(a-b);

/* Since num1<num2 
the second operation is performed
c = a-b = -10
*/

You can use a conditional operator (ternary operator) in nested form as shown below Program snippet to find the maximum among three numbers:

int a-5, b-12, c-3;

max=(a>b) ? (a>c)?a:c (b>c)? b:c; 

The test condition a>b is false. Hence, it will operate expression 2. In expression 2, the test condition b>c is true which enables value 12 to be stored in max: Hence, max=12.

Relational Operators

These operators are used to show the relationship among the operands. Relational operators compare the values of the variables and result in terms of True or False” (i.e. 0 or 1). The table shows different types of relational operators as follows:

SymbolMeaningFormatResults If a=10; b=6;
<Less thana<bfalse
>Greater thana>btrue
<=Less than or equal to a<=bfalse
>=Greater than or equal toa>=btrue
==Equal toa==bfalse
!=Not Equal toa!=btrue
Relational Operators

Logical Operators

Java uses logical operators AND(&&), OR(||) or NOT(!). These operators yield 1 or 0 depending upon the outcome of different expressions. The types of logical operators in Java with their formats are shown below:

Logical operatorsSymbolFormat
AND&&(a>b)&&(a>c)
OR||(a>b)||(a==c)
NOT!!(a==b)
Logical Operators

The precedence of logical operators is NOT (!) AND (&&) and OR (||) Le. If a statement contains all three logical operators then NOT will be performed before any other operator.

Logical Or (| |)

This operator is used to combine two conditional expressions. It will result in true if either of two conditions (expression) is true otherwise false.

For example,

5>4 || 8>12: It will result in true because 5>4 is true.

3>7 || 5<=4: It will result in a false because both expressions are false.

2<0||2<12: It will result in true because the second expression is true.

Logical And (& &)

The AND operator will result in true if both the expressions (comprising its operands) are true.

5>3 && 3<5: It will result in true because both the expressions are true.

6==6 && 3>0: It will result in true as both the expressions are true.

5!=5 && 4==4: It will result in a false as the first expression is false.

Logical Not (!)

An operator is applied when you want to reverse the outcome of an expression. It is a unary operator because it uses a single operand.

For example,

! (8>3): False, because 8>3 is true.

! (5<7): False, as 5<7 is true.

! (3<0): True as 3<0 is false.

Note: Relational operators have higher precedence over logical operators AND and OR. Hence, while using AND and OR operators you need not enclose the operands within parenthesis NOT (!) operator has the highest precedence, so it is required to enclose the operand under parenthesis.

BITWISE OPERATORS IN JAVA

In Java programming, you can use some special types of operators, which perform operations on the bit level of the operands. These operators use only byte, short int, and long type operands. Float and double types are not allowed. Bitwise operators are binary operators.

OperatorsMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
Bitwise complement
<<Left shift
>>Right shift
>>>Zero fill right shift
<<=Left shift assignment
>>=Right shift assignment
>>>=Zero fill right shift assignment
BITWISE OPERATORS IN JAVA

Bitwise Operators With Example

ABA&BA|B!AA^B
000010
010111
100101
111100
Bitwise Operators With Example

An illustrated Java program to show bitwise operators:

public class Main
{
public static void main(String args[])
{
int a, b;

a=12; 
b=10;

System.out.println("(a & b) = "+(a&b)); 
System.out.println("(a | b) = "+ (a|b)); 
System.out.println("(a^b) = "+ (a^b));
System.out.println("a<<2) = "+ (a<<2)); 
System.out.println("(12>>2) = "+(12>>2));
System.out.println("(14>>>2) = "+(14>>>2));
}
}

Output:

(a & b) = 8
(a | b) = 14
(a^b) = 6
a<<2) = 48
(12>>2) = 3
(14>>>2) = 3

HIERARCHY OF BINARY OPERATORS IN JAVA

The computer uses the precedence of binary operators in the same manner as followed in Mathematics. In mathematics, you use the BODMAS system of hierarchy but the computer applies BEDMAS.

  1. BRACKET
  2. EXPONENT
  3. DIVISION/ MULTIPLICATION
  4. ADDITION/SUBTRACTION

Note: Multiplication and division have the same precedence. Anyone can be operated first which appears first from the left side of an expression. Similarly, addition or subtraction is performed.

Shorthand Operations

Java, C, and C++ languages allow the use of shorthand binary operations i.e. an expression can be written in short form. For example,

A shorthand expression can be formed only when a variable is used as an accumulator in the expression i.e. same variable is to be used after and before the assignment sign.

ExpressionShorthand form
a=a+ba+=b
 c=c-d c-=d
m=m*10m*=10
d=d/2d/=2
x=x % 10x =% 10
Shorthand Expression And Its Form

Conclusion

Operators in Java are very useful and play an important role in the development of complex programs. Without the use of operators, it would be difficult to write a simple program that calculates a salary based on certain parameters.

Share your love