Java Operators: Arithmetic, Relational, Logical, and more

07 Feb 2023
Beginner
341 Views

Java Operators: Arithmetic, Relational, Logical, and more

Are you a learner looking for a comprehensive guide on Java Operators? Look no further! In this article, we'll explore the different types of operators available in Java and give examples of how they can be used. We'll cover unary and binary operators, assignment and comparison operators, logical operators, increment/decrement operations, bitwise operations and much more - so get your programmer's hat on and let’s dig into some cool coding with operands!

What are Java Operators?

Operators in Java are being used as a symbol that performs various operations according to the code. Some Operators of JAVA are "+", "-", "*", "/" etc. The idea of using Operators has been taken from other languages so that it behaves expectedly.

Types of Operators in Java

There are various types of Operators in Java that are used for operating. These are "Arithmetic", "Relational", "Logical", "Bitwise", "Assignment", and "Unary”.

Arithmetic Operators in Java

Arithmetic Operators in Java are particularly used for performing arithmetic operations on given data or variables. There are various types of operators in Java, such as

Operators Operations
+ Addition
- Subtraction
x Multiplication
/ Division
% Modulus

Example

class Main
{
  public static void main(String[] args)
  {

    // declare variables
    int a = 15, b = 5;

    // addition operator
    System.out.println("a + b = " + (a + b));

    // subtraction operator
    System.out.println("a - b = " + (a - b));

    // multiplication operator
    System.out.println("a * b = " + (a * b));

    // division operator
    System.out.println("a / b = " + (a / b));

    // modulo operator
    System.out.println("a % b = " + (a % b));

  }
}

Output

a + b = 20

a - b = 10

a × b = 75

a ÷ b = 3

a % b = 7

Assignment Operators in Java

Assignment Operators mainly use to assign the values to the variable that is situated in Java programming. There are various assignment operators in Java, such as

Operators Examples Equivalent to
= X = Y; X = Y;
+= X += Y; X = X + Y;
-= X -= Y; X = X - Y;
*= X *= Y; X = X * Y;
/= X /= Y; X = X / Y;
%= X %= Y; X = X % Y;

Example

class Main
{
    public static void main(String[] args)
    {

      // create variables
      int a = 5;
      int var;

      // assign value using =
      var = a;

      System.out.println("var using =: " + var);

      // assign value using =+
      var += a;

      System.out.println("var using +=: " + var);

      // assign value using =*
      var *= a;

      System.out.println("var using *=: " + var);

    }
  }

Output

var using =: 5

var using +=: 10

var using *=: 50

Relational Operators in Java

Java relational operators are assigned for checking the relationships between two particular operators. There are various relational operators in Java, such as

Operators Description Example
== Is equal to 3 == 5 returns false
!= Not equal to 3 != 5 returns true
> Greater than 3 > 5 returns false
< Less than 3 < 5 returns true
>= Greater than or equal to 3 >= 5 returns false
<= Less than or equal to 3 <= 5 returns true

Example

class Main
{
    public static void main(String[] args)
    {

      // create variables
      int a = 7, b = 11;

      // value of a and b
      System.out.println("a is " + a + " and b is " + b);

      // == operator
      System.out.println(a == b); // false

      // != operator
      System.out.println(a != b); // true

      // > operator
      System.out.println(a > b); // false

      // < operator
      System.out.println(a < b); // true

      // >= operator
      System.out.println(a >= b); // false

      // <= operator
      System.out.println(a <= b); // true

    }
  }

Output

a is 7 and b is 11
falsetrue
false
true
false
true

Logical Operators in Java

Logical Operators in Java are used for checking whether the expression is true or false. It is generally used for making any decisions in Java programming. There are various types of logical operators in Java, such as 

Operators Example Meaning
&& [ logical AND ] expression1 && expression2 (true) only if both of the expressions are true
|| [ logical OR ] expression1 || expression2 (true) if one of the expressions in true
! [ logical NOT ] !expression (true) if the expression is false and vice-versa

Example

class Main
  {
    public static void main(String[] args)
    {

      // && operator
      System.out.println((6 > 3) && (8 > 6)); // true
      System.out.println((6 > 3) && (8 < 6)); // false

      // || operator
      System.out.println((6 < 3) || (8 > 6)); // true
      System.out.println((6 > 3) || (8 < 6)); // true
      System.out.println((6 < 3) || (8 < 6)); // false

      // ! operator
      System.out.println(!(6 == 3)); // true
      System.out.println(!(6 > 3)); // false

    }
  }

Output

  • (6 > 3) && (8 > 6) returns true because both (6 > 3) and (8 > 6) are true.
  • (6 > 3) && (8 > 6) returns false because the expression (8 < 6) is false
  • (6 < 3) || (8 > 6) returns true because the expression (8 > 6) is true
  • (6 > 3) || (8 < 6) returns true because the expression (6 > 3) is true
  • (6 < 3) || (8 < 6) returns false because both (6 < 3) and (8 < 6) are false
  • ! (6 == 3) returns true because 6 == 3 is false
  • ! (6 > 3) returns false because 6 > 3 is true

Unary Operators in Java

Unary Operators in Java are used in only one operand. There are various types of Unary Operators in Java, such as 

Operators Description
+ Unary Plus
- Unary Minus
++ Increment operator
-- Decrement Operator
! Logical complement operator

Example

class Main
{
    public static void main(String[] args)
    {

      // declare variables
      int a = 13, b = 13;
      int result1, result2;

      // original value
      System.out.println("Value of a: " + a);

      // increment operator
      result1 = ++a;
      System.out.println("After increment: " + result1);

      System.out.println("Value of b: " + b);

      // decrement operator
      result2 = --b;

      System.out.println("After decrement: " + result2);

    }
  }

Output

Value of a: 13

After increment: 14

Value of b: 13

After decrement: 12

Bitwise Operators in Java

Bitwise Operators in Java are used to assist the performance of the operations on individual bits. There are various types of Bitwise Operators in Java, such as

Operators Descriptions
~ Bitwise Complement
<< Left shift
>> Right shift
>>> Unsigned Right shift
& Bitwise AND
^ Bitwise exclusive OR

Example

class Main
  {
    public static void main(String[] args)
    {

      String str = "ScholarHat";
      boolean result;

      // checks if str is an instance of
      // the String class
      result = str instanceof String;

      System.out.println("Is str an object of String? " + result);
    }
  }

Output

Is str an object of String? true

Operator Precedence in Java

Summary

Wow, what a journey we just took together exploring Operators in java with examples! To wrap up, Java operators are an extremely helpful tool to have in your programming arsenal. After reading this article, you should be more familiar with how each of these operators works and understand the examples provided. With patience and hard work, you'll soon become a pro at utilizing all the various operators Java has to offer! So go forth, get your hands dirty, and code away!

Accept cookies & close this