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

Format Decimal Numbers using Strings Within Pattern

As mentioned in earlier posts, the java.text.DecimalFormat class is used to format decimal numbers via predefined patterns specified as String. Apart from the decimal separator,...

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

How To Create Maven Project in Eclipse With Archetype

Maven Basics Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output...

How To Concatenate Strings in Java

Introduction In Java, a String is a sequence of characters. There are often programming situations where you will need to concatenate Strings. There are several...

Â

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

What is In-Proc and Out-Proc (COM) ?

The terms 'In-Proc' and 'Out-Proc' are to describe the kind of implementation of COM Servers. Before we begin, for those who are not quite...

Does VBScript Supports all COM Objects ?

Well the answer in one line is - No VBScript does not supports all COM Objects or Servers! VBScript works with only a subset of ‘Objects’...

Format Decimal Numbers using Strings Within Pattern

As mentioned in earlier posts, the java.text.DecimalFormat class is used to format decimal numbers via predefined patterns specified as String. Apart from the decimal separator,...

OTHER TUTORIALS

Remove Duplicates from List Using Set.addAll

The Set interface has a method called addAll. This accepts a 'Collection' as the parameter. So if you invoke...
- Advertisement -spot_img