Operatosr
Operators are special
symbols (characters) that carry out operations on operands (variables and
values). For example, + is an operator that performs addition. Operator in java is a symbol that is used to perform
operations. For example: +, -, *, / etc.
There are many
types of operators in java which are given below:
- Unary Operator
- Arithmetic Operator
- Shift Operator
- Relational Operator
- Bitwise Operator
- Logical Operator
- Ternary Operator
- Assignment Operator.
Java Operator Precedence
operater in java |
Unary
Operators- The Java unary operators require only one operands.
Unary operators are used to perform various operations.
·
Incrementing/decrementing a value by one
·
Negative an expression
·
Inverting the value of a boolean
·
Operator
|
Meaning
|
+
|
Unary
plus (not necessary to use since numbers are positive without using it)
|
-
|
Unary
minus; inverts the sign of an expression
|
++
|
Increment
operator; increments value by 1
|
--
|
decrement
operator; decrements value by 1
|
!
|
Logical
complement operator; inverts the value of a boolean
|
Example
class Operator{
public static void
main(String agrs[])
{
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}}
Output
10
12
12
10
Arithmetic
Operators- Arithmetic operators are used to perform
mathematical operations like addition, subtraction, multiplication etc.
Operator
|
Meaning
|
+
|
Addition
(also used for string concatenation)
|
-
|
Subtraction
Operator
|
*
|
Multiplication
Operator
|
/
|
Division
Operator
|
%
|
Remainder
Operator
|
Example
class Operator{
public static void
main(String agrs[]){
int x=10;
int y=5;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
System.out.println(x%y);
}}
Output
15
5
50
2
0
Shift
Operator- The java left shift operators << is used to shift
all of the bits in value to the left side
and the right shift operators >> is used to move left operands
value to right by the number of bits to specified number of times.
Operator
|
Description
|
<<
|
Left Shift
|
>>
|
Right Shift
|
>>>
|
Unsigned Right Shift
|
Example
class Operator{
public static void
main(String agrs[])
{
System.out.println(10<<2);
System.out.println(10<<3);
System.out.println(10>>2);
System.out.println(10>>3);
}}
Output
40
80
2
5
Relational Operators- The equality and relational operators
determines the relationship between two operands. It checks if an operand is
greater than, less than, equal to, not equal to and so on. Depending on the
relationship, it results to either true or false.
Operator
|
Description
|
Example
|
==
|
equal
to
|
5
== 3 is evaluated to false
|
!=
|
not
equal to
|
5
!= 3 is evaluated to true
|
>
|
greater
than
|
5
> 3 is evaluated to true
|
<
|
less
than
|
5
< 3 is evaluated to false
|
>=
|
greater
than or equal to
|
5
>= 5 is evaluated to true
|
<=
|
less
then or equal to
|
5
<= 5 is evaluated to true
|
Java Equality and
Relational Operators
|
Example
class Operator{
public static void
main(String agrs[]){
int x=10;
int y=20;
System.out.println(“x>y-->”+(x>y));
}}
Output
x>y - -> true
Bitwise Operator- To perform bitwise and bit
shift operators in Java, these operators are used.
Operator
|
Description
|
~
|
Bitwise
Complement
|
<<
|
Left
Shift
|
>>
|
Right
Shift
|
>>>
|
Unsigned
Right Shift
|
&
|
Bitwise
AND
|
^
|
Bitwise
exclusive OR
|
|
|
Bitwise
inclusive OR
|
Java Bitwise and Bit
Shift Operators
|
|
Example
class Operator{
public static void
main(String agrs[]){
int x=10;
int y=5;
int z=20;
System.out.println(x<y&x++<z);
System.out.println(x);
System.out.println(x>y|x++<z);
System.out.println(x);
}}
Output
false
10
true
11
Logical Operator- The logical
operators || (conditional-OR)
and && (conditional-AND) operates on boolean expressions.
Here's how they work.
Operator
|
Description
|
Example
|
||
|
conditional-OR; true if
either of the boolean expression is true
|
false
|| true is evaluated to true
|
&&
|
conditional-AND;
true if all boolean expressions are true
|
false
&& true is evaluated to false
|
Java Logical Operators
|
Example
class Operator{
public static void
main(String agrs[]){
int x=10;
int y=5;
int z=20;
System.out.println(x<y&&x++<z);
System.out.println(x);
System.out.println(x>y||x++<z);
System.out.println(x);
}}
Output
false
10
true
Ternary Operators- The conditional operator or ternary
operator ?: is shorthand for if-then-else statement. The
syntax of conditional operator is:
variable = Expression ?
expression1 : expression2
·
If
the Expression is true, expression1 is assigned
to variable.
·
If
the Expression is false, expression2 is assigned
to variable.
Example
class Operator{
public static void
main(String agrs[]){
int x=10;
int y=5;
int min=(x<y)?x:y;
System.out.println(min);
}}
Output
5
Assignment
Operators- Assignment
operators are used in Java to assign values to variables. For example,
int age;
age = 5;
The assignment operator
assigns the value on its right to the variable on its left.
Here, 5 is assigned to the variable age using = operator.
Example
class Operator{
public static void
main(String agrs[]){
int x=10;
int y=20;
a+=4;
b-=4;
System.out.println(x);
System.out.println(y);
}}
Output
14
16
No comments:
Post a Comment