Operators In Java

In this tutorial we will learn various types of operators in Java language.

what are operators?

Operator – is a symbol that operates on a variable or value, where it’s used to perform certain operations such as logical, relation, arithmetical, etc. Therefore when you want to perform a mathematical operation then you use operators. There are various types of operators in Java that may include;

  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Arithmetic operators
  • Assignment operators

Let’s now discuss each type giving out relevant examples;

1. Arithmetic Operators

These operators are used to perform common mathematical operations such as addition, subtraction, division etc. As we discussed in our previous tutorial on variables we used + sign to add variables but in this case they are used as an addition operator. Let’s discuss some of the arithmetic operators as follows;

  • Addiction(+) – this operator adds two values. Eg, x+y
  • Subtraction() -this operator subtracts one value from another in a program. Eg, a-b
  • Division(/) -this operator is used to divide and give quotient of two values. Eg, a/b
  • Multiplication(*) -this operator is used to multiply two values. Eg, x*y
  • Modulus(%) -this operator returns the division remainder. Eg, a%b
  • Decrement() -this operator decreases the value of a variable by 1. Eg, –x
  • Increment(++) -this operator increases the value of a variable by 1. Eg, ++y

See this example here;

public class Demo{
 public static void main(String args[]){
 int a = 20;
 int b = 10;
 System.out.println(a+b); //It adds the two values and gives 30
 System.out.println(a/b); //It divides the two values and gives 2
 System.out.println(a++); //It adds the value of a by 1 and gives 21

2. Comparison Operators

Comparison operators are used to compare two values, they are also use to in conditions that compares one expression with another. There are various comparison operators in Java which may include;

  • Greater than(>)
  • Less than(<)
  • Equal(==)
  • Not Equal(!=)
  • Less than or Equal to(<=)
  • Greater than or Equal to(>=)

3. Logical Operators

Logical operator is a symbol or word that is used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. There are various types of logical operators which may include;

  • and(&&) – returns true if both statements are true. Eg, x<&&y>15
  • or(||) -it returns true if one of the expressions is true. Eg, y>9||x<17
  • not(!) -it reverses the result, for instance it gives if answer is true it outputs false. Eg, !(x>6&&y<12)

4. Assignment Operators

These operators assigns values to variables. There are several assignment operators in Java;

  • Add and assign(+=) -it adds and assign the result to the left operand. Eg, x+=7 it becomes x = x+7
  • simple assignment(=) -this is a simple assignment we uses = sign. Eg, y=9
  • subtract and assign(-=) -it subtracts and assign the result to the left operand. Eg, x-=7 it becomes x = x-7
  • multiply and assign(*=) -it multiplies and assign the result to the left operand. Eg, x*=7 it becomes x = x*7
  • division and assign(/=) -it divides and assign the result to the left operand. Eg, x/=7 it becomes x = x/7
  • modulus and assign(%=) -it calculates the modulus and assign the result to the left operand. Eg, x%=7 it becomes x = x%7
Thanks for reading through java operators, remember to subscribe to our YouTube Channel for live sessions and video tutorials. As we continue learning Java language, hoping to meet in our next tutorial.