- An operator is a symbol that tells the computer to perform certain mathematical or logical calculations.
- Operators are used in programs to manipulate data and variables.
- Java provides the following operators:
Arithmetic operators:
- Java provides all the basic arithmetic operators.
Operator | Meaning |
+ | Addition or unary plus |
– | Subtraction or unary minus |
* | Multiplication |
/ | Division |
% | Modulo division |
- Integer Arithmetic
- When both the operands in a single arithmetic expression such as a+b are integers, the expression is called an integer expression, and the operation is called integer arithmetic.
- Integer arithmetic always produces integer result.
- Real Arithmetic
- An arithmetic operation involving only real operands is called real arithmetic.
- A real operand may assume values either in decimal or exponential notation.
- Modulus operator % can be applied to the floating point data as well.
- Mixed-mode Arithmetic
- When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression and result will be in real.
class floatDemo { public static void main(String args[]) { float a=35.5f, b=5.2f; System.out.println("a="+a); System.out.println("b="+b); System.out.println("a+b="+(a+b)); System.out.println("a-b="+(a-b)); System.out.println("a*b="+(a*b)); System.out.println("a/b="+(a/b)); System.out.println("a%b="+(a%b)); } }
Relational Operators
- Relational operators are used for comparing two operands.
- We can compare operands for their relational equality.
Operator | Meaning |
< | Is less than |
<= | Is less than or equal to |
> | Is greater than |
>= | Is greater than or equal to |
== | Is equal to |
!= | Is not equal to |
class RelationalDemo { public static void main(String args[]) { float a=15.0f,b=20.75f,c=15.0f; System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); System.out.println("a<b is "+(a<b)); System.out.println("a>b is "+(a>b)); System.out.println("a==c is "+(a==c)); System.out.println("a<=c is "+(a<=c)); System.out.println("a>=b is "+(a>=b)); System.out.println("b!=c is "+(b!=c)); System.out.println("b==a+c is "+(b==a+c)); } }
Logical Operator
- Java has following logical operators:
Operator | Meaning |
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
class LogicOprDemo { public static void main(String args[]) { int a=15,b=30,c=15,d=30; if (a==c && b==d) System.out.println("Equal"); else System.out.println("Not Equal"); if (a<b || a<c) System.out.println("A is smaller than B & C"); else System.out.println("A is greater than B & C"); if(a!=b) System.out.println("A & B are not equal"); else System.out.println("The value of a and b are equal"); } }
Assignment Operator
- Assignment operator is used to assign a value of an expression to a variable.
- We can also use ‘shorthand’ operators for the same purpose as follow:Var_name opr = <exp>;
- For example, x = x+ (y+1) is equivalent to x+=y+1;
Simple assignment operators | Shorthand operator |
x=x+1 | x+=1 |
x=x*1 | x*=1 |
x=x*(n+1) | x*=n+1 |
x=x/(n+1) | x/=n+1 |
x=x%y | x%=y |
Increment/ Decrement Operator
- Java supports two increment and decrement operators: ++ and – –
- The operator ++ increments value by 1 to the operand while – subtracts by 1.
- Both operators can be used in the following format:
- ++X or X++;
- –X or X–;
- Where, ++X is equivalent to X=X+1 and –X is equivalent to X=X-1;
- When operator comes first before the operand then it is called prefix operator and if operator comes after operand, it is called postfix operator or notation
class incDecDemo { public static void main(String args[]) { int x=10, y=20;
System.out.println(“X=” + x);
System.out.println(“Y=” + y);
System.out.println(“++X=” + ++x);
System.out.println(“Y++” + y++);
System.out.println(“X=” + x);
System.out.println(“Y=” + y);
}
}
Conditional Operators
- Java also supports special operator “? :” known as ternary operator.
- It is used to construct conditional expressions similar to if statement
- It has the following form:
<exp1> ? <exp2> : <exp3>
- Consider the following example:
int a=10,b=15; if (a>b) x=a; else x=b;
- We can generate same result using ternary operator as follow:
x=(a>b) ? a: b;
Bitwise Operators
- Java supports another special kind of operators known as bitwise operators for manipulation of data at bit level.
- These operators are used for testing the bits, or shifting them to the right or left.
- It may not be applied to float or double.
Operators | Meaning |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise Exclusive OR |
~ | Bitwise Unary NOT |
<< | Shift left |
<< | Shift right |
- AND (if Both operand are 1 then result is 1 else 0):
- 0101 AND 0011 = 0001
- OR(if Both operand are 0 then result is 0 else 1):
- 0101 OR 0011 = 0111
- XOR(if no. of 1 is odd then result is 1and 0 if even):
- 0101 XOR 0011 = 0110
- NOT(Complement )
- NOT 0111 = 1000
- 0111 LEFT-SHIFT = 1110 (Shifts all bits to the left)
- 0111 RIGHT-SHIFT = 0011(Shifts all bits to the right)
Logical Bitwise Operations
A | B | OR (A|B) | AND (A&B) | XOR (A^B) | ~A |
0 | 0 | 0 | 0 | 0 | 1 |
1 | 0 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 | 0 |
- x=12;y=10;
z = x & y; // z is 8 - It works as follow:
0000 1100
& 0000 1010
————–
0000 1000 = 8 (decimal)
Special Operator
- Java supports some special operators such as instanceof operator and member selection operator(.)
Instanceof Operator:
- It is an object reference operator and returns true if the object on the left-hand side is an instance of the class given on the right-hand side.
- It helps to determine whether the object belongs to a particular class or not.
- For example, person instanceof student
is true if the object person belongs to the class student; otherwise false.
Dot operator:
- It is used to access the instance variables and methods of class objects.
- For example:
- roll_no //reference to the variable roll_no
- countPer() //reference to the method countPer()
- It can also be used to access classes and sub-packages form a package.