0
52
• 24
• 34
• 14
• 12
•
•
•
• 10
•
•
94
Shares

 Operators in Java

Operators in Java:

An operator takes one or more arguments and produces a new value. The arguments are in little different from ordinary method calls but results the same. All operators produces a value from their operand. An operator can change the value of an operand. This is called a side effect.The most common use for operators that modify their operand is to generate the side effect.But the value produced is available for our use as in operators without side effect.

In general operations mainly classified into two categories:

1. Transitive
2. Non Transitive

When x(operator)y(operator)z=z(operator)y(operator)x then the operation is transitive operation. Otherwise it is a non transitive operation.

``````
//for x=2,y=4,z=8 and operator=+ then
x+y+z=2+4+8=14
//is equal to
x+y+x=8+4+2=14
//hence it is a transitive operation.
//in case operator1 =+ and operator2=-
x+y-z=2+4-8=-2
//is not equal to
z+y-x=8+2-2=8
//hence it is non transitive operation.
``````

There are the below types of operators available in java

1. Arithmetic operator
2. Concatenation operator
3. Assignment operator
4. Unary operator
5. Relational operator
6. Ternary operator
7. Conditional operator
8. Type comparison operator
9. Bitwise operator
10. Shift operator
11. Other Operator

Arithmetic Operator

Mixed Mode Arithmetic

when one of the operands is real and other in other type,the expression is called a mixed mode arithmetic expression.Compiler will check for higher type of the operands and converts the lower type to higher type and carry out the operation.

``````
=20/10 //will produce 2 and
=20/10.0 // will produce 2.0
``````
Operator Expression Example Comment
Substraction a-b Subtracts right from left
Multiplication * a*b multiplies left with right
Divition / a/b
Modulas % a%b applicable for integer data

Concatenation operator:

only operator that supports operator overloading in java. In normal context ‘+’ works as addition but in String context,it works as a concatenate.If an expression begins with a String,then all operands that follow must be String.

``````
public class MyStringTest{
public static void main(String []args){
int i=1;
int j=2;
int k=3;
String myString="i j k ";
System.out.println(myString+i+j+k);
System.out.println(i+j+k+myString);
}
}
``````

Output of the code:
\$javac MyStringTest.java
\$java -Xmx128M -Xms16M MyStringTest
i j k 123
6 i j k

As the expression begins with String compiler will convert i,j,k into String and concatenate instead of addition.

Operator Expression Example Comment
Concatenation + “Java”+” “+”Love” String context it is concatenation

Assignment operator

They are mainly classified as:

• Direct assignment
• Shorthand assignment
Operator Expression Example Comment
assignment = c=a+b assigns the result to the left
subtract AND -= a-=b it is equivalent to a=a-b
add AND += a+=b it is equivalent to a=a+b
divide AND /= a/=b it is equivalent to a=a/b
multiply AND *= a*=b it is equivalent to a=a*b

No space between op and =will be allowed and generic syntax is identifier op=expression.It is also called short hand operator, as it follows short hand notations.

1. What appears on the left hand side need not be repeated and therefore it becomes easier to write.
2. The statement is more concise and easier to read.
3. This results a more efficient code.

Unary operator

The unary plus and minus operator are same as binary plus and minus.The unary minus produces the negative of the value.Unary plus provides symmetry with unary plus

``````
x=-a;
x=a*-b;
x=a*(-b);
``````
Operator Expression Example Comment
unary plus + Indicates positive values
unary minus negates an expression
increment ++ a++ Increments the value by one
decrement a– decrement the value by one

++ and — is also called Auto increment and Auto decrements operators. They have two flavors of it.

• pre increment and post increment
• pre decrement and post decrement
``````
int i=1;
++i;//(i=2) pre increment and then assignment
i++;(i=2 first assignment and then increment)
//final value i=1
//similarly
--i;(i=2 predecrement and assignment)
i--;(i=2 assignment and then decrement)
//final value i=1
``````

Relational operator

Relational operator generates a boolean result.They evaluate the relationship between the values of the operands.A relational expression produces true if the relationship is true and false otherwise.Equivalence and non equivalence works well with all build in data types but other comparisons won’t work with type boolean.

Operator Expression Example Comment
not ! !a logical compliment operator inverts the value of a returns boolean
equals == a==b checks equality, returns boolean
not equal to != a!=b checks if a is not equal to b, returns boolean
greater than > a>b checks if a is greater than b, returns boolean
less than < a<b checks if a is less than b returns boolean
greater than equal to >= a>=b checks if a is greater than or equals to b, returns boolean
less than equal to <= a<=b checks if a is less than or equals to b, returns boolean

These operators can be applied to any type of data and only returns true or false.when arithmetic expressions are used on either side of a rational operator,the arithmetic expression will be evaluated first then the result is compared. Thus Arithmetic operators will have higher priority over rational operators.

``````
a+b==c+d //is true if both side are equal.
``````

Relatational operators are used in decision statements such as if,while etc to decide the next course of action.

Compound Operator:

++ and — are called compound operator and can not have space in between.

Conditional operator or Logical Operator:

They are also called logical operators.It produces a boolean result as true or false based on the logical relationship of its arguments.They can be used in the boolean context not for other data types.They are used when we want to form a compound conditions by combining two or more relations.

Operator Expression Example Comment
conditional AND   &&   a&&b
conditional OR || a||b

Short circuiting:

Short circuiting means that the expression will be evaluated until the truth or falsehood of the entire expression can be unambiguously determined.As a result all the parts of a logical expression may not get evaluated.Like-

``````
if(test1()&&test2()&&test3())
``````

In this example if test1() function returns true,control will try to execute test2() function,if test2() returns true then test3() will be executed. Now if any of these function generate a false output,the whole expression becomes false,irrespective of the yield of the other functions.Java will not carry out the execution of the other function if it detects any false statement or false yield from any of the functions while going from left to right.This leads to potential performance increase if all the parts of the logical expression do not need to be executed.
They follow the below truth table:

Operator -1 Operator-2 Operator1&&operator2 Operator1||operator2
true true true true
true false false true
false true false true
false false false false

Ternary operator

Don't miss out!

Receive top technical news, lesson ideas, travel tips and more!

Give it a try. You can unsubscribe at any time.