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

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...

Configuring Maven Compiler Plugin (Java)

1. What is Maven Compiler Plugin The ‘Maven Compiler Plugin’ or more commonly known as 'Maven Java Compiler' is used to compile the source code...

Convert List to Array Using For-Loop

The simplest way to convert a List to an array is to use a for-loop. The following code snippet demonstrates this method. First, the code...

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...

Â

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

Convert String to Date Using SimpleDateFormat Class

Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to...

Maven Common Commands Reference

Maven offers a good set of commands and CLI Options to carry out wide range of Dev tasks. Most of these commands are in...

Working with Selenium WebElements

Table of Contents 1. Selenium WebElements 2. WebElement Locators 3. Working With Text Box 4. Working With Buttons 5. Checkboxes and Radio Buttons 6....

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....

OTHER TUTORIALS

How To Create The Right Software QA Strategy

In today’s highly competitive world, businesses do not get much time to test a product before releasing it into...

JMeter Framework Project

- Advertisement -spot_img