Operators
•
An operator is a
symbol that tells the compiler to perform specific mathematical or logical
manipulations
•
Most of the operators are binary operators i.e. these operators
require two operands to perform an operation. Example: +, -, *
• Few operators like ++ (increment) operator are the unary operator which means they operate on one operand only. These binary and unary operators are included in Arithmetic operators.
•
There is also a ternary operator called
Conditional Operator (?:) which takes three operands.
Types of operators
- · Arithmetic operators
- · Assignment operators
- · Relational operators
- · Logical operators
- · Bitwise operators
Arithmetic Operators
Arithmetic operators are
used for performing basic mathematical operations on operands.
Operator |
Name |
Description |
Example |
+ |
Addition |
Adds together two values |
x + y |
- |
Subtraction |
Subtracts one value from another |
x - y |
* |
Multiplication |
Multiplies two values |
x * y |
/ |
Division |
Divides one value by another |
x / y |
% |
Modulus |
Returns the division remainder |
x % y |
++ |
Increment |
Increases
the value of a variable by 1 |
++x |
-- |
Decrement |
Decreases the value of a variable by
1 |
--x |
package Operators;
//Arithmetic Operators
public class Arithmetic {
public static void main(String args[])
{
int a=123,b=10;
System.out.println("Addition : "+(a+b)); //133
System.out.println("Subtraction : "+(a-b));//113
System.out.println("Multiplication : "+(a*b));//1230
System.out.println("Division : "+(a/b));//12
System.out.println("Modulus : "+(a%b));//3
}
}
package Operators;
public class Unary {
public static void main(String args[])
{
//Unary Operators in Java ++ --
int a=10;
System.out.println(a);//
//a++; //a=a+1
System.out.println(a++);//
System.out.println(a);//
System.out.println(++a);//
System.out.println(a--);//
System.out.println(a);//
System.out.println(--a);//
}
}
Relational Operators
•
Relational or comparison
operators are used to compare two operands.
•
The result of the
evaluation is either true or false.
Operator |
Name |
Example |
== |
Equal to |
x == y |
!= |
Not equal |
x != y |
> |
Greater than |
x > y |
< |
Less than |
x < y |
>= |
Greater than or equal to |
x >= y |
<= |
Less than or equal to |
x <= y |
package Operators;
public class relational {
public static void main(String args[])
{
int a=100,b=50;
System.out.println("Equal to : "+(a==b));
System.out.println("Not Equal to : "+(a!=b));
System.out.println("Greater than : "+(a>b));
System.out.println("Less than : "+(a<b));
System.out.println("Greater than or equal to : "+(a>=b));
System.out.println("Less than or equal to : "+(a<=b));
}
}
•
Logical operators are used
for evaluating a combination of conditions/constraints to get a resultant
value.
•
The result of the evaluation of a Boolean
expression is Boolean which is either true or false.
Operator |
Name |
Description |
Example |
&&
|
Logical
and |
Returns
true if both statements are true |
x
< 5 && x < 10 |
||
|
Logical
or |
Returns
true if one of the statements is true |
x
< 5 || x < 4 |
! |
Logical
not |
Reverse
the result, returns false if the result is true |
!(x
< 5 && x < 10) |
package Operators;
public class Logical {
public static void main(String args[])
{
int m1=25,m2=75;
System.out.println("And && : "+(m1>=35 && m2>=35));
System.out.println("Or || : "+(m1>=35 || m2>=35));
}
}
Assignment operators
•
Assignment operators are
used to assign value to a variable.
Operator |
Example |
Same
As |
= |
x
= 5 |
x
= 5 |
+= |
x
+= 3 |
x
= x + 3 |
-= |
x
-= 3 |
x
= x - 3 |
*= |
x
*= 3 |
x
= x * 3 |
/= |
x
/= 3 |
x
= x / 3 |
%= |
x
%= 3 |
x
= x % 3 |
&= |
x
&= 3 |
x
= x & 3 |
|= |
x
|= 3 |
x
= x | 3 |
^= |
x
^= 3 |
x
= x ^ 3 |
>>= |
x
>>= 3 |
x
= x >> 3 |
<<= |
x
<<= 3 |
x
= x << 3 |
package Operators;
public class Assignment
{
public static void main(String args[])
{
int a=123;
System.out.println(a);
a+=10;
System.out.println(a);
a-=10;//a=a-10
System.out.println(a);
a*=10;
System.out.println(a);
a/=10;
System.out.println(a);
a%=10;
System.out.println(a);
}
}
Conditional Operators:
package Operators;
public class Conditional {
public static void main(String args[])
{
//Conditional or Ternary Operators in Java ?:
int a=45,b=35,c;
c=a>b?a:b;
System.out.println("The Greatest Number is : "+c);
}
}
Bitwise Operators
Bitwise operators perform operations on
integer data at the individual bit-level.
&
(bitwise and)
|
(bitwise or)
<<
(bitwise shift left)
>>
(bitwise shift right)
Example Program:
package Operators;
public class Bitwise {
public static void main(String args[])
{
//Bitwise & Shift Operators in Java
int a=25,b=45;
System.out.println("Bitwise And : "+(a&b));
System.out.println("Bitwise Or : "+(a|b));
System.out.println("Bitwise Xor : "+(a^b));
System.out.println("Bitwise Not : "+(~a));
}
}
0 Comments