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

Java Tutorial #4 – Control Statements

Introduction Control statements are used to change the flow of execution based on changes to certain variables in the code. One of the types of...

Java Tutorial #3 – Java Arrays

Table of Contents One Dimensional Array Declaring Array Allocting Memory to Array Accessing Array Elements Initializing Array Length of Array Multi Dimensional Array Creating 3...

Setting Max/Min Digits For Decimal Numbers With Java DecimalFormat

The DecimalFormat class offers the following four such methods which can be used to easily set the maximum and/or minimum digits for decimal numbers....

How To Create Maven Project in Eclipse Without Archetype

Maven is a very popular build and dependency management tool. Eclipse is an IDE that helps developers write and run Java code easily. In...

Â

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

VBS Part 1 – Introduction and Background

At the upfront, this theoretical article might look boring to most of us but still, it is advisable to know about our scripting friend...

VBS Part 2 – Fundamentals and Concepts

Having gone through the Introductory part, it is time to look at some crucial fundamentals and concepts. This article is to refresh some of...

Java Tutorial #6 – Jump Statements

Introduction The break statement is used to stop further execution. It can be used either in a loop or within a switch statement. Break Statement The break...

Concatenate Strings Using String.join() for Collections

There is an overloaded version of the String.join method that accepts as parameter an Iterable implementation. Since all Collection classes implement the Iterable interface,...

OTHER TUTORIALS

Introduction to Apache Maven

1. What is Maven When you write a software application, there are many steps in it like adding the necessary...
- Advertisement -spot_img