Sitemap

Operators in Java

4 min readSep 8, 2024

--

Press enter or click to view image in full size
Operators in Java

Various operators are available in Java. Operators used in Java are arithmetic operator(+,-,/,*,%), assignment operator(=), logical operator(&&, ||) and Comparison operators(==, !=, >, <, >=, <=).

Arithmetic Operators:

Arithmetic operators are :

  1. Addition (+): Gives addition of two operands
int a = 7;
int b = 77;
int c = a+b;//output 84

2. Subtraction (-):

int a  7; 
int b = 77;
int c = b-a;//output 70

3. Multiplication (*): Multiply two operands

int a = 7;
int b = 8;
int c = a*b; //output 56

4. Division(/): Divide first operand by second operand

int a = 7;
int b = 77;
int c = b/a; //output 11

5. Modulus(%): Gives reminder when first operand in divided by second operand.

int a = 10;
int b = 3;
int c = a%b; //output 1

6. Increment(++): It increments operand by 1.

int a = 11;
a++;// a = 12;

There are two types of increment operators in Java post-increment (a++) and pre-increment (++a). Lets take an example understand difference:

int a =7;
int b = a++;// b = 7
a = 7;
int c = ++a;// c = 8

In case of post increment first value is fetched and then incremented and in case of pre increment first value is incremented and then incremented value in fetched.

7. Decrement ( — ): Decrements operand by 1.

int a = 7;
a--; //a = 6

Similar to increment operator, decrement operator also have two types post decrement and pre decrement which are similar to post increment and pre increment respectively.

int a =7;
int b = a--;// b = 7
a = 7;
int c = --a;// c = 6

Assignment Operator

Assignment operator (=) it is used assign value to variable.

Example:

//Test.java
class Test
{
public static void main(String args[])
{
int a = 7; //assign value 7 to variable a
int b = a; // assign value in variable a to variable b
}
}

Assignment operator can be used with arithmetic operator to perform respective operation and assign value to variable. Consider following example:

//Test.java
class Test
{
public static void main(String args[])
{
int a = 7;
a += 10;
System.out.print(a); // output 17
}
}

a = a + 7; if equivalent to a+=7;

Get Raj R. Sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

Similarly, -=, *=, /=, %= can be used.

Comparison Operator:

Comparison operators returns true or false depending on operands and operator used.

Various comparison operators are :

  1. Greater than(>): Returns true if first operand is greaten than second operand other wise false.
  2. Less than(<): Returns true if first operand is less than second operand otherwise false.
  3. Greater than or equal to (>=): Returns true if first operand is greater than or equal to second operand otherwise false.
  4. Less than or equal to (<=): Returns true if first operand is less than or equal to second operand otherwise false.
  5. Equal to (==): Returns true if first operand is equal to second operand otherwise false.
  6. Not equal to (!=): Returns true if first operand is not equal to second operand otherwise false.

Consider following example:

//Test.java
class Test
{
public static void main(String args[])
{
int a = 10;
int b = 11;
System.out.print(a<b);//output true
}
}

Logical Operators:

Logical operators returns true or false depending on operator used and operands.

Following are logical operators:

  1. AND (&):

And operator returns true only if both operands are true other wise it returns false. Short circuit and operator can used i.e. &&. Using && operator if first operand if false then it will not check second operator as output will be false irrespective of second operand.

Example :

//Test.java
class Test
{
public static void main(String args[])
{
int a = 10;
int b = 11;
System.out.print(a>b && a<b);//output false
}
}

2. OR(|):

OR operator will return false only if both operands are false otherwise it will return true. Similar to AND short circuited OR operator (||) can be used. If we use || operator then if first operator if true then it will return true and will not check for second operator, as output will be true irrespective of second operand.

Example:

//Test.java
class Test
{
public static void main(String args[])
{
int a = 10;
int b = 11;
System.out.print(a>b ||a<b);//output true
}
}

3. NOT(!):

Not will return false if operand is true and returns true if operand if false.

Example:

//Test.java
class Test
{
public static void main(String args[])
{
int a = 10;
int b = 11;
System.out.print(!(a>b));//output true
}
}

So, in this article we have seen different operands in Java. There is much more to explore in Java.

Keep Coding…

--

--

Raj R. Sharma
Raj R. Sharma

Written by Raj R. Sharma

Passionate Computer Engineering Student exploring the world of software. LinkedIn: https://www.linkedin.com/in/raj-r-sharma/