Operator is a symbol that represents some operation that can be performed on data. The operators are applied to operands (onto which processing is desired). These operands can be literals, variables etc. The operators supported by Java are as follows:
We’ll be covering the following topics in this tutorial:
Operator Precedence
Every Java operator has an associated precedence. Following is a list of all the Java operators from highest to lowest precedence. In this list of operators, all the operators in a particular block have equal precedence. The precedence level of each row decreases from top to bottom. This means that the ++ operator has a higher precedence than the * operator, but the same precedence as the — operator.
The following is a mnemonic to help you remember the precedence order, from highest to lowest: PUMAS.
P Parentheses ( ), postfix a++ a–
U Unary operators (except postfix) + (positive) – (negative) ++a –a
M Multiplication, division and Modulus * / %
A Addition and subtraction + –
S Special assignments = += -= *= /= %=
We apply this precedence order to evaluate the values of expressions. The following example should help clarify this:
d = a * b + ++ c;
Adding parentheses to this statement will make it clear as to how it should be evaluated. The steps are as follows:
1. The ++operator is the most important of the operators in this expression. Add parentheses around b++:
d = a * (b++) + c;
2. Among the remaining operators *, +, and =, * has greater importance. Add parentheses around * and the two variables that use this operator:
d = (a * (b++)) + c;
3. The operator + has higher importance than =, so add parentheses as follows:
d = ((a * (b++)) + c);
4. The expression is now fully parenthesized. Next, the values in the inner parentheses must be evaluated before those in the outer parentheses. Assume that a = 5, b = 3, and c = 1; then the value of d is (5 * 3) + 1 = 16
First, you must parenthesize an expression fully using the operator precedence order. Then, evaluate the expression starting from the innermost parentheses and moving toward the outermost parentheses. If there are multiple operators with the same priority or importance in the-expression, evaluate these from left to right.
Arithmetic Operators
Arithmetic operators are used for simple arithmetic operations. The various operators are represented by the symbols:
Description Operator
Addition + 1 + 1 = 2
Subtraction – 2 – 1 = 1
Multiplication * 2 * 3 = 6 (same as 2 X 3 = 6)
Division / 6 / 2 = 3
Modulus % 7 % 2 = 1
The modulus operator gives the remainder when a division operation is performed.
When the operands of an arithmetic operator are of different types, they must be changed to the same type before the operation is performed. Type promotion automatically converts the data type of the narrower operand to the type of the wider operand. For example, if d and i are operands of type double and int, respectively, i is promoted to double in the following expression:
double v = d * i;
This example shows how the minus arithmetic operator is used. The program prompts the user to enter two numbers on the console and reads them in using the scanner. The second number is subtracted from the first, and the result is displayed on the console.
import java.util .Scanner;
publicclass Calculator {
publicstaticvoid main(String[] args){
Scanner scanner =newScanner(System.in);
System.out.print(“Enter a number: “);
double numberl = scanner.nextDouble();
System.out.print(“Enter another number:”);
double number2 = scanner.nextDouble();
System.out.println(“The result is:”+(numberl – number2));
}
}
The program output is:
Enter a number:10
Enter another number:30.5
The result is:-20.5
The output shows that the user entered the numbers 10 and 30.5, and the program displayed the result: -20.5.
The -+- operator can be used in a different context in print statements-to concatenate different types. For example, the following statement concatenates the string “The result is:” with the result of the subtraction number1 – number2:
“The result is:” + (number1 – number2)
You can also pass primitive types as arguments to the print and print.ln methods. Therefore, the following arguments are all valid:
int x = 10, Y = 20; String s = “30”;
System.out.println(x); // prints out 10
System.out.println(x + y + x); // prints out 40
System.out.println(s + x + x); // prints out 301010
In the last statement, the + operator performs a concatenation because the first argument s is of type String.
Modify the program so that it uses a different arithmetic operator and rerun it. Use different input values and check the program output.
Unary Operators
Whereas arithmetic operators take two operands, unary operators take a single operand. The unary operators are:
++ increment
— decrement
+ plus
– minus
The increment operator is used to increase the value of a variable by 1, and the decrement operator to decrease a variable’s value by 1.
This statement increases the value of alpha by 1:
++alpha; // increment operator used in prefix position
This is the same as writing:
alpha = alpha + 1;
When the operator appears before the variable name, it is known as prefix notation. The statement can also be written as follows:
alpha++; // increment operator used in postfix position
Conversely, when the operator is applied after the variable name, the notation is called postfix notation. In this case as well, the value of alpha is increased by 1.
The following statements use the decrement operator. Each statement decreases the value of variable beta by 1:
beta = beta – 1; // reduce the value of variable beta by 1
–beta; // decrement operator used in prefix position
beta–; // decrement operator used in postfix position
All three statements are equivalent.
When the decrement and increment operators are used in assignment statements, the position of the operator affects the value of the expression. For example, suppose that the variable 1ength has the increment operator placed in the postfix position:
float breadth = length++ – 5.0f;
This means that 1ength is incremented after the expression is evaluated using its original value. Hence, the preceding statement is equivalent to the following two statements:
float breadth = length – 5.0f;
length = length + 1;
When the operator appears in the prefix, position as in the following statement, it means that the variable 1ength is incremented first, and the value of the expression is evaluated using this incremented value. The statement shows the operator in prefix notation:
float breadth = ++length – 5.0f;
The preceding statement is equivalent to the following two statements:
length = length + 1;
float breadth = length – 5.0f;
The unary minus operator is prefixed before an expression to negate it:
int x = 10;
int y = -(x – 5);
The expression in parentheses is a single operand (x – 5) with the value 5. The value of y is – 5. This is the same as writing:
int x = 10;
int y = -x + 5;
The unary plus operator has no effect on an expression:
int x = 10;
int y = +(-(x – 5));
The value of y is still – 5.
Assignment Operators
Earlier you used the assignment operator =. This operator can be combined with the arithmetic operators +, -, *, /, and % to give a new set of assignment operators: +=, -=, *=, 1=, and %=. These new operators are used to represent assignment statements in an abbreviated form. For example, the following two statements are equivalent, and you can use either one of them:
length = length + 5;
length += 5;
The following two statements are also equivalent:
length = length * 10;
length *= 10; // multiply length by 10
The advantage of the second statement is that it is shorter because 1ength appears only once instead of twice. The other operators are used similarly:
radius /= 5.5; // radius = radius / 5.5;
x %= 10; // x = x % 10;
Note that if the variable on the right is different from the variable on the left, the abbreviated form cannot be used. The following statement uses two different variables:
size = breadth * 10;
This statement cannot be written as:
size *= 10; // incorrect, as it does not contain variable breadth
Short Circuit Logical Operators
When two logical expressions are concatenated with operators: AND and OR, the OR operator results in true when A is true, no matter what B is. Similarly, the AND operator results in false when A is false, no matter what B is. If we use the operators II and &&, instead of the operators I and &, Java will not bother to evaluate the right hand operand when the outcome of the expression can be determined by the left operand alone. This is very useful when the right hand operand depends on the left one being true or false in order to function properly.
if (physics >=60 && chemistry >=60)
Since the short circuit form of AND (&&) is used, control will just check first condition, Le. if physics marks >=60, it won’t check the marks of chemistry at all. If instead of &&, if we use single & version of AND, both sides would have to be evaluated Le. it will check that physics >=60 and also chemistry >=60.
Relational Operators
Relational operators are used to find out the relationship between two operands. Table shows you the different relational operators used in java programming.
Example of Program using relational operators
class ro {
public static void main(String args[ ]) {
int a=10, b=5;
System.out.println(“The a<b Value is:”+(a<b ));
System.out.println(“The a>b Value is:”+(a>b ));
System.out.println(“The a<=b Value is:”+(a<=b));
System.out.println(“The a>=b Value is:”+(a>=b));
System.out.println(“The a==b Value is:”+(a==b));
System.out.println(“The a!=b Value is:”+(a!=b ));
}
}