Operators in Java

In this tutorial, we will learn about Operators in Java. Operators are special symbols that we use to perform special operations like the addition of two numbers.

Java provides us with a wide range of operators which help us to perform special operations. In Java, all the operators are divided into different categories

All the operators are categorized according to their functionality like operators which perform mathematical operations like +, -, /, * that is in a different category and which perform relational operations are in different category like > or < operators

Types of Operators: –

  • Unary Operator
  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Conditional Operator

Difference Between operators and Operands: –

Before going further let’s check out the difference between operators and operands. Operators are symbols which tell to the compiler to what kind of operation will perform and operands are values on which operation will perform. Operators and operands are together known as Expressions

Operands may be in the form of Values like 10, 15 or in the form of variables such as a + b.

Unary Operator: –

Unary Operators are those who work with only one operand. There are two main unary operators in java which are increment(++) and decrement(–), operator.


class Operators{
    public static void main(String args[])
	{
	    int a=10;
		int b=20;
        int c=10;
        int d=20;
		
		System.out.println("Pre Inc result "+(++a));
		System.out.println("Post Inc result "+(b++));
		System.out.println("Pre dec result "+(--c));
		System.out.println("Pre dec result "+(d--));
	}
}


Arithmetic Operator: –

Arithmetic Operators are those which help us to perform the mathematical operation there are five Arithmetic operators available in java. They are +, -, /, *, %.


class Operators{
    public static void main(String args[])
	{
	    int a=10;
		int b=20;
		
		System.out.println("Airthmetic + Opeartor: "+(a+b));
		System.out.println("Airthmetic - Opeartor: "+(a-b));
		System.out.println("Airthmetic / Opeartor: "+(a/b));
		System.out.println("Airthmetic % Opeartor: "+(a%b));
		System.out.println("Airthmetic * Opeartor: "+(a*b));
	}
}


Relational Operator: –

Relational operators help us to make compression between two operands there are six Relational operators available in java. They are <, >, <=, >=, ==, != .


class Operators{
    public static void main(String args[])
	{
	    int a=10;
		int b=20;
		
		System.out.println("Result of > Opeartor: "+(a>b));
		System.out.println("Result of >= Opeartor: "+(a>=b));
		System.out.println("Result of < Opeartor:"+(a<b));
		System.out.println("Result of <= Opeartor: "+(a<=b));
		System.out.println("Result of == Opeartor:"+(a==b));
        System.out.println("Result of != Opeartor:"+(a!=b));
	}
}


Logical Operator: –

Instead of Operands, logical operators work on expression. There is a total of three logical operators which are && (Logical and), ||(logical or),! (logical not)

Logical AND(&&): –

Logical AND only returns true if have true operands on both side. other wise it returns false

Operand 1 Operand 2 Result
true true true
true false false
false true false
false false false

class Operators{
    public static void main(String args[])
	{
	    boolean a=true;
		boolean b=false;
		System.out.println("Logical AND Result"+(a&&b));
	}
}


Logical OR(||): –

It returns true if any single or both operands are true other wise it returns false.

Operand 1 Operand 2 Result
true true true
true false true
false true true
false false false

class Operators{
    public static void main(String args[])
	{
	    boolean a=true;
		boolean b=false;
		System.out.println("Logical OR Result"+(a||b));
	}
}


Logical NOT(!): –

It returns complement of given operands true for false and false for true:

Operand Result
true false
false true

class Operators{
    public static void main(String args[])
	{
	    boolean a=true;
		boolean b=false;
		System.out.println("Logical NOT Result"+(!a));
        System.out.println("Logical NOT Result"+(!b));
	}
}


Conditional Operator: –

The conditional operator is also known as utility and ternary operator is work as an if-else condition. Format to write conditional operator.


condition ? if true : if false



class Operator {
   public static void main(String[] args) {  
      int number = 10;
      String result;
      result = (number%2 > 0) ? "Even" : "Odd";
      System.out.println(number + " is " + result);
   }
}


Spread the love
Scroll to Top