Java Operators.
In Java, sometimes we have
to perform lots of operations like addition, subtraction, multiplication,
division etc on variables as well as on values. These operations can be
performed with the help of Operators in Java language.
Following are different types
of operators in Java
1.
Arithmetic
2.
Assignment
3.
Relational
4.
Logical
5.
Unary
6.
Bitwise
Arithmetic
If you want to perform some
mathematical or arithmetic operations on your variables or on values, then we
have this category . We have lots of operators in this category like +,-,*,/,%
etc
For Example
int a=10, b=2;
then
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
Assignment
In Java, we have an operator
which is used to assign some value to a variable. This operator is known as
assignment operator.
For Example
int a = 10;
or
int a;
a = 10;
Here, “=” is the assignment
operator and it is assigning the value 10 to variable a
Other assignment operators
in Java can be
+=, -=, *=, /= etc
For Example
int a = 2;
int b;
b = a;
System.out.println(b); //
Ans 2
int a = 2;
a+=2;
System.out.println(a); //
Ans 4
int a=2;
a*=2;
System.out.println(a); //
Ans 4
int a=2;
a/=2;
System.out.println(a); //
Ans 1
Relational
These operators are used to
check the relation of operands. For example, one operand is greater than second
operand.
int a=10;
int b=20;
a>b
so here “>” is the
operator
We have following Relational
operators in Java
·
== (Equal)
·
! = (Not equal)
·
> (Greater)
·
< (Less)
·
>= (Greater than
or Equal)
·
<= (Less than or
Equal)

No comments:
Post a Comment