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
}
Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

JAVA TUTORIALS

Recent Posts

RELATED POSTS

Convert List to Array Using ToArray With Param

There is an overloaded version of the toArray method. This accepts an array as a parameter and returns a result array that is of the same...

Java Tutorial #7 Part 1 – Classes & Objects

Classes are a very important concept in Java. Java code always needs to be written in a class. In this article, we will be...

Concatenate Strings Using String Joiner

Java 8 has added a new class called StringJoiner. This concatenates Strings using a delimiter as well as adds a prefix and suffix to...

Rounding Decimal Number With Java DecimalFormat

The DecimalFormat class has a method called setRoundingMode() which can be used for setting the rounding mode for DecimalFormat object. The setRoundingMode() accepts RoundingMode...

Â

CHECKOUT 'HOW-TOs'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string...

MORE ON CODEX

MORE IN THIS CATEGORY

Finding Web Elements with Selenium

I'm going to explain in this tutorial about the usage of the findElement and findElements method of Selenium Webdriver on the Chrome web browser....

How To Sort List in Java

Introduction A List is an interface in the Java collection framework. It can be used to store objects. Programmers often encounter scenarios where they need...

Convert String to Date Using java.util.Calendar

The java.util.Calendar class also encapsulates a date. The Calendar class has some more features than the java.util.Date class. It provides the ability to extract...

Convert List to Array Using ToArray With Param

There is an overloaded version of the toArray method. This accepts an array as a parameter and returns a result array that is of the same...

OTHER TUTORIALS

Desired Capabilities in Selenium Web Driver

1. Desired Capabilities in Selenium The performance of a Web application may vary according to different browsers and operating systems....
- Advertisement -spot_img