Java Operators

In this tutorial, we will learn about what is an operator, types of operators and importance of operators along with working examples of each operator type.

Operators

Operator is a symbol, which perform a specific operation on one or more operands. Operand can be a value or a variable or a constant or a method result.

Operators in java divided into five types.

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Bitwise operators

Arithmetic operators

Operators which are used to perform arithmetic operations called as arithmetic operators.

Following are of the arithmetic operators.

OperatorDescription
+Addition operator
-Subtraction operator
*Multiplication operator
/Division operator
%Remainder operator

Example of arithmetic operator:

CopiedCopy Code
public class ArthimeticOperators {
	public static void main(String[] args) {
		int x=100,y=100; // x and y are operands
		System.out.println("Addition Operator Result :"+(x+y));
		System.out.println("Subtraction Operator Result :"+(x-y));
		System.out.println("Multiplication Operator Result :"+(x*y));
		System.out.println("Division Operator Result :"+(x/y));
		System.out.println("Remainder Operator Result :"+(x%y));
	}
}

Output:

Addition Operator Result :200
Subtraction Operator Result :0
Multiplication Operator Result :10000
Division Operator Result :1
Remainder Operator Result :0

Note: Addition operator can be used for adding two or more strings.

String str = "Hello" +" Java World";
String str = "Learning" +" Java from" +" GKindex";

Assignment operators

Operators which are used to assign a value, value can be a constant or a variable or method result.

'=' will be used for assigning a value the variable.

int x = 1; // 1 value assigned to x
String str = "First String" // First String assigned to str

'=' can be used for assigning a reference of an Objects.

Object obj = new Object(); // Object class reference assigned to the obj

Comparison operators

Operators which are used to compare two or more operands referred as compare operators.

Example of Comparison Operator:

CopiedCopy Code
public class Student {
	public static void main(String[] args) {
		int x=100,y=100;
		System.out.println("== Operator :"+(x==y));
		System.out.println("!= Operator :"+(x!=y));
		System.out.println("< Operator  :"+(x<y));
		System.out.println("<= Operator :"+(x<=y));
		System.out.println("> Operator  :"+(x>y));
		System.out.println(">= Operator :"+(x>=y));
	}
}

Output:

== Operator :true
!= Operator :false
< Operator  :false
<= Operator :true
> Operator  :false
>= Operator :true

Logical operators

Logical operators are the operators used evaluate more than one condition or statement, all the logical operators returns either true or false.

The following are the logical operators.

  1. Logical AND (&&) Operator
  2. Logical OR (||) Operator
  3. Logical NOT (!) Operator

AND (&&) Operator:

AND operator returns true only when all the conditions or statements are true otherwise returns false.

Assume an example, where need to a find number whose value should be in the range of 100 to 200.

int x=150; // Need to check 150 comes in the range of 100 to 200.
boolean range = (x>100 && x <200);
System.out.println(range);

In the above example x value is 150 which is greater than 100 condition result will be true.

Need to check x should be less than 200. x value is 150 which is less than 200 condition result will be true.

Both conditions are true and logical AND operator return true.

Consider x value as 250, first condition x is greater than 100 returns true but not less than 250 return false for second condition. Both not returned true as a result, logical AND operator result will be false.

int x=250; 
boolean range = (x>100 && x <200);
System.out.println(range);

Syntax:

Condition1 && Condition2

OR (||) Operator:

OR operator returns true if any one of the conditions is true otherwise returns false.

Assume the following example, to apply a job seeker should meet any of the following conditions. Should have secured more than 90% of marks in graduation or more than 2 years of working experience.

int  percentageMarks = 92;
int workExperience = 0;
boolean shortList = (percentageMarks>90 || workExperience > 2);

In the above code, seeker have secured 92% of marks although work experience is zero logical OR condition returns true as a result.

Syntax:

Condition1 || Condition2

NOT (!) Operator:

It will inverse the result, returns false in case of true result and true for false result.

Syntax:

!(condition)