Operators | Description |
Addition(+) | Add value on either side of operator |
Subtraction(-) | Subtracts right-hand operand from left-hand operand. |
Multiplication(*) | Multiplies values on either side of the operator. |
Division(/) | Divides left-hand operand by right-hand operand. |
Increment(++) | Increases the value of operand by 1. |
Decrement(--) | Decreases the value of operand by 1. |
Modulus(%) | Divides left-hand operand by right-hand operand and returns remainder. |
Ternary Operator deal with three operands.It is also called conditional assignment statement because the value assigned to a variable depends upon a logical expression.
a=5,b=3;
max=(a>b)?a:b;
Here,the value 5 is stored in max as a>b is true
// Program using ternary operator
public class TernaryOperator {
public static void main(String args[]){
int num1 , num2;
num1 = 10;
num2 = (num1 == 1) ? 20: 30;
System.out.println( "Value of num2 is : " + num2 );
num2 = (num1 == 10) ? 20: 30;
System.out.println( "Value of num2 is : " + num2 );
}
}
Value of num2 is : 30
Value of num2 is : 20
These operator are used to show the relationship among the operands.Relational Operators compare the values of the variable and result in terms of 'True' or 'False'(i.e 0 or 1).
Operator | Description | Format | Result if a=10,b=6; |
< | Less than | a<b | false |
> | Greater than | a>b | true |
<= | Less than or equal to | a<=b | false |
>= | Greater than or equal to | a>=b | true |
== | Equal to | a==b | false |
!= | Not equal to | a!=b | true |
Java uses logical operators AND(&&),OR(||) or NOT(!).These operators yield 1 or 0 depending upon the outcome of different expressions.
Operator | Symbol | Format |
AND | && | (a>b)&&(a>c) |
OR | || | (a==b)||(a==c) |
NOT | ! | !(a==b) |
In java programming,you can use some special types of operators,which perform operations on bit level of the operands.These operators use byte,short int and long type operands.
Operator | Description |
| | Bitwise OR |
& | Bitwise AND |
~ | Bitwise Complement |
^ | Bitwise XOR |
<< | Left Shift |
>> | RIght Shift |
>>> | Unsigned Right Shift |
// Program to perform bitwise operation
public class bitwiseoperation{
public static void main(String args[])
{
int a,b;
a=12;b=10;
int n = 35,m;
m=~n;
System.out.println("complement is ="+m);
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("(12>>2)="+(12>>2));
System.out.println("(14>>>2)="+(14>>>2));
}
}
complement is =-36
(a&b)=8
(a|b)=14
(a^b)=6
(a<<b)=48
(a>>b)=3
(a>>>b)=3
Assignment operators are used in Java to assign values to variables.
Operator | Description | Example |
= | Assigns values from right side operands to left side operand. | a=b+c,a will assign addition of a and b |
+= | It adds right operand to the left operand and assign the result to left operand. | a+=b is equal to a=a+b |
-= | It subtracts right operand from the left operand and assign the result to left operand. | a-=b equal to a=a-b |
*= | It multiplies right operand with the left operand and assign the result to left operand. | a*=b, equal to a=a*b |
/= | It divides left operand with the right operand and assign the result to left operand. | a/=b,a/ is equal to a=a/b |
%= | It takes modulus using two operands and assign the result to left operand. | a%= b is equal to a =a %b |
<<= | Left shift AND assignment operator. | a <<= 2 is same as a = a<< 2 |
>>= | Right shift AND assignment operator. | a >>= 2 is same as a = a >> 2 |
&= | Bitwise AND assignment operator. | a &= 2 is same as a = a & 2 |
^= | Bitwise exclusive OR and assignment operator. | a ^= 2 is same as a=a ^ 2 |
|= | Bitwise inclusive OR and assignment operator. | a |= 2 is same as a = a | 2 |