Site icon Automation Dojos

Java Tutorial #2 – Operators in Java

Table of Contents

Introduction

Operators are an essential part of any programming language. In-fact it is not possible not to write a decent piece of code without making use of ‘Operators’. Java operators can be categorized mainly into arithmetic, relational and logical operators. In addition, there is a special assignment operator that is used to assign values to variables. Each of these is covered in a separate section below.

1. Assignment Operator

The “=” sign is known as the assignment operator.  It assigns whatever is to its right to the variable on its left. The variable type must be compatible with the value being assigned. The following code demonstrates this:

int a = 10; //assigns the value 10 to the variable a

int b = 5+8; // assigns the value 13 to the variable b

2. Arithmetic Operators

Arithmetic operators are those that are used in ordinary mathematics, that is addition, subtraction, multiplication and division. These can only be applied to numerical operands. The Arithmetic operators can be further broken down into Basic Operators, Compound Assignment Operators and Increment/Decrement operators.

Symbol Name Description Example
+ Addition Adds the value on the left to the value on the right int a=30, b=20;

 

System.out.println(a+b); //prints 50

Subtraction Subtracts value on the right from the value on the left int a=30, b=20;

 

System.out.println(a-b);

//prints 10

* Multiplication Multiplies the value on the left to the value on the right int a=30, b=2;

 

System.out.println(a*b);

//prints 60

/ Division Divides the value on the left with the value on the Right int a=30, b=10;

 

System.out.println(a/b);

//prints 3

% Modulus Returns the remainder of dividing the value on the left with the value on the right int a=35, b=10;

 

System.out.println(a%b);

//prints 5

3. Compound Assignment

Java provides special operators which can be used to combine an operation with an assignment.  These are known as compound assignment operators. The compound operators are just shortcut operators which can be used when you want to change the value of a particular variable. Java supports compound assignment operators for each of the arithmetic operators as listed below:

Symbol Name Description Example
+= Addition assignment Adds the value on the right to the variable on the left and assigns the result to the variable on the left int a=30;

 

a += 10; //same as a=a+10;

-= Subtraction assignment Subtracts the value on the right from the variable on the left and assigns the result to the variable on the left int a=30;

 

a -= 20; // same as a=a-20;

*= Multiplication assignment Multiplies the value on the right to the variable on the left and assigns the result to the variable on the left int a=30;

 

a *= 2; // same as a=a*2;

/= Division assignment Divides the variable on right with the value on the left and assigns the result to the variable on the left int a=30, b=10;

 

a/=b; same as a=a/10;

%= Modulus assignment Obtains the remainder of dividing the variable on the left with the value on the right and assigns the result to the variable on the left int a=35, b=10;

 

int a %= b; // same as a=a%10;

4. Increment & Decrement Operators

Java provides two additional operators known as increment and decrement operators. These basically increment or decrement the variable on which they are invoked.

Symbol Name Description Example
+ + Increment Increments the value on which it is invoked by 1 int a = 5;

 

a++; // same as a=a+1

– – Decrement Decrements the value on which it is invoked by 1 int b = 5;

 

b–; // same as

b=b-1;

Both operators come in two variants, prefix and postfix. In the prefix form the operator precedes the operand and the operation is performed before the assignment. In the postfix form, the operator succeeds the operand and the operation is performed after the assignment. The following code demonstrates this:

int i = 5;
//postfix form, will print i, then increment, so will output 5
System.out.println(i++); 
int j = 10;
//prefix form, will decrement j first then print it, so will output 9
System.out.println(--j);

5. Relational Operators

Relational operators are used to determine the relationship between operands. They return a boolean value. Java has the following relationship operators:

Symbol Name Description Example
> Greater than Returns true if the value on the left is greater than the value on the right, otherwise returns false int a=30, b=20;

 

boolean flag = a > b; // flag=true

< Less than Returns true if the value on the left is less than the value on the right, otherwise returns false int a=30, b=20;

 

boolean flag = a < b; // flag=false

== Equal To Returns true if the value on the left is equal to the value on the right, otherwise returns false int a=30, b=20;

 

boolean flag = a == b; // flag=false

!= Not Equal To Returns true if the value on the left is not equal to the value on the right, otherwise returns false int a=30, b=20;

 

boolean flag = a != b; // flag=true

>= Greater than or Equal to Returns true if the value on the left is greater than or equal to the value on the right, otherwise returns false int a=30, b=20;

 

boolean flag = a >= b; // flag=true

<= Less than or Equal to Returns true if the value on the left is less than or equal to the value on the right, otherwise returns false int a=20, b=20;

 

boolean flag = a <= b; // flag=true

6. Logical Operators

Logical operators operate on boolean values and are used to combine relational comparisons. Java has the following logical operators:

Symbol Name Description Example
&& Logical And Returns true if both its operands are true boolean a = true,b=false;

 

boolean c = a && b;

// c=false

|| Logical Or Returns true if any one of its operands are true boolean a = true,b=false;

 

boolean c = a || b;

// c=true

! Not Reverses its operand boolean a = true;

 

boolean c = !a;

//c=false

Logical operators can be used to combine relational comparisons as follows:

int a=30, b=20, c=10;
if(a > b && b > c){
// do something
}
Exit mobile version