In this section we will read about operators in Java. We will illustrate the use of operators in Java using a simple example.

Java provides various operators to manipulate the operations in Java. Operators are denoted by a symbols these symbols are the special symbols which are used for performing operations on the operands. Operators can perform operations on one, two or three operands. Operators in Java follows the precedence for evaluating. Each operator has its own precedence either it is higher, lower or equal. In an operation a higher precedence operator evaluated first from the relative operator with lower precedence. Operators with equal precedence is evaluated using a rule. Evaluation of binary operators except the assignment operator (evaluates from right to left) is from left to right. There are various operators in Java that are used to perform operations on operand :

**Assignment Operators :**

**= :**This operator is used to assign the value of its right operand to its left operand.

**Arithmetic Operators :**Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.

**+ :**This operator is used to perform the add operation on the two numeric operands. This operator can also be used for concatenating two string opearnds.

**- :**This operator is used to perform the subtract operation on the two numeric operands.

*** :**This operator is used to perform the multiply operation on the two numeric operands.

**/ :**This operator is used to perform the divide operation on the two numeric operands.

**% :**This operator is called as mod or remainder operator and is used for finding the remainder value after dividing two numeric values. This operator can also be applied with the two numeric operands.

**Unary Operators :**Unary operators are perform operations on a single operand. These operations can be increment/decrement, invert a boolean value, etc.

**+ :**This is a unary plus operator. This operator is used to show a value positive. Number without any sign is positive.

**- :**This is a unary minus operator. This operator is used to show a value negative. This operator negates an expression.

**++ :**This is a unary increment operator that increments the value of numeric operand by 1.

**-- :**This is a unary decrement operator that decrements the value of numeric operand by 1.

**! :**This is a unary not operator that inverts the value of a boolean.

**Equality And Relation Operators :**Equality And Relation Operators are generally used to perform the operations where there is a need of comparison between two operands.

**> :**A greater than operator that compares whether the left side operand is greater than the right side operand or not.

**< :**A less than operator that compares whether the left side operand is less than the right side operand or not.

**== :**An equal to operator that compares whether the left side operand is equal to the right side operand or not.

**!= :**A not equal to operator that compares whether the left side operand is not equal to the right side operand or not.

**>= :**A greater than or equal to operator that compares whether the left side operand is greater than or equal to the right side operand or not.

**<= :**A less than or equal to operator that compares whether the left side operand is less than or equal to the right side operand or not.

**The Conditional Operators :**Conditional operators are used to perform operations when a specified condition is satisfied.

**&& :**This is a conditional AND operator that specifies the next statement of program will be evaluated only if both of the operands return true.

**| | :**This is a conditional OR operator that specifies the next statement of program will be evaluated if either of the operand returns true.

**Type Comparison Operator :**Type comparison operator are used to compare the object.

**instanceof :**This operator is used to compare the object whether it is an instance of a class, subclass, or a class that implements the interface.

**Bitwise And Bit Shift Operators :**

**~ :**A unary bitwise complement operator that applied to any of the integral type and inverts a bit pattern i.e. inverts every 1 to 0 and 0 to 1.

**<< :**A signed left shift operator changes the position of a bit pattern to the left.

**>> :**A signed right shift operator changes the position of a bit pattern to the right.

**>>> :**An unsigned right shift operator positioned a zero in the leftmost position.

**& :**This operator is used to do the bitwise AND operation.

**^ :**This operator is used to do the bitwise exclusive or operation.

**| :**This operator is used to do the bitwise inclusive or operation.

**Example**

In this example we are giving a simple example which will demonstrate you about how to use the operators in Java. In this example we will create a Java class where we will use the various operators. We will see how the operands are evaluated with the operators.

**OperatorInJavaExample.java**

public class OperatorsInJavaExample { int a, b, c; public void arithmeticOpeator() { a = 5; b = 3; System.out.print("Add : "); System.out.println(a+b); System.out.print("Subtract :"); System.out.println(a-b); System.out.print("Remainder of a/b : "); System.out.println(a%b); } public void unaryOpeator() { a = 5; b = 3; System.out.print("Unary Plus : "); System.out.println(""+a+b); System.out.print("PostIncrement : "); System.out.println(a++); System.out.print("PreIncrement : "); System.out.println(++b); } public void equalityAndRelationalOperator() { a = 5; b= 3; System.out.print("Is "+a+" equal to "+ b+" : "); System.out.println(a == b); System.out.print("Is "+a+" >= "+ b+" "); System.out.println(a >= b); } public void conditionalOperator() { a = 5; b = 3; if(a == 5 && b == 3) { System.out.println("Conditional AND operator"); } if(a == 5 || b == 5) { System.out.println("Conditional OR operator"); } } public static void main(String args[]) { OperatorsInJavaExample o = new OperatorsInJavaExample(); o.arithmeticOpeator(); o.unaryOpeator(); o.equalityAndRelationalOperator(); o.conditionalOperator(); } }

**Output :**

When you will compile and execute the above example you will get the output as follows :

**For reading more in detail you may go through the following link
http://www.roseindia.net/java/java-operators.shtml
**

Advertisements

Posted on: July 6, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms. Connect Me on Google+

Comments:0

Discuss: Operators In JavaPost your Comment