Operator in JAVA.docx - Google Docs
Operator in JAVA.docx - Google Docs
Ans:-Java operators are symbols that are used to performoperations on variables and manipulate the
v alues of the operands. Each operator performs specific operations.eg:- 5+ 4 Here 5 and 4 are operands,
and + is operator.
Operators may be unary, binary or ternary. Unary operators operate on single operand. eg :- ++ and --.
Binary operators operate on two operands.eg:- all arithmetic operators except + and -.
. Arithmetic operators :-Those operator which areused for mathematical calculations are known as
1
arithmetic operator. The various arithmetic operator are :
(iv) ( / ) division
Example:-
class arith
{
{
int a=4,b=6,c,d,e,f,g;
c=a+b;
System.out.println(" Addition of a and b is " +c);
d=a-b;
e=a*b;
f=a/b;
g=a%b;
}
}
. Relational operators:-Relational operators areused to compare the values of operands of same data
2
type and return either True or False. Relational operator are binary operators. The relational operators
are:-
Example:-
class relation
{
{
int a=5,b=7;
}
}
. Logical Operators:-Logical operators are usedto combining two or more conditions or complement
3
the evaluation of the original condition under consideration. The logical Operators are:-
(i) ( && ) Logical AND:- Return true if both conditionare true, otherwise false.
Example:-
class logic
{
{
int a=4,b=7,c=11;
}
}
Example:-
class assign
{
{
int a =5;
}
}
. Increment /Decrement Operators:-Increment /Decrement Operators are unary operators. These are
5
also called auto- increment or auto-decrement operators. The increment operator adds 1 to the
operand. eg:- '++i' is equivalent to i=i+1 or i+=1.
The Decrement Operator subtracts 1 from the operand. eg:- '--i' is equivalent to i=i-1 or i-=1.
1.a)Post-increment operator:-
hen placed after the variable name, the value of the operand is incremented but the previous value is
W
retained temporarily until the execution of this statement and it gets updated before the execution of
the next statement.
Syntax:-
i++
).pre-increment operator:-When placed before thevariable name, the operand’s value is incremented
b
instantly.
Syntax:-
++i
Example:-
class PrePost
{
{
int i = 5;
i++;
System.out.println(i);
++i;
System.out.println(i);
System.out.println(++i);
System.out.println(i++);
System.out.println(i);
}
}
. a)Post-decrement operator:-When placed after thevariable name, the value of the operand is
2
decremented but the previous value is retained temporarily until the execution of this statement and it
gets updated before the execution of the next statement.
Syntax:-
i--
) pre- decrement operator:-When placed before thevariable name, the operand’s value is
b
decremented instantly.
Syntax:-
--i
Example:-
class PrePostDe
{
int i = 10;
i--;
System.out.println(i);
--i;
System.out.println(i);
System.out.println(--i);
System.out.println(i--);
System.out.println(i);
}
}
.Ternary Operator:-Ternary operator is also called conditional operator. This operator takes three
6
operands. The first operand should be an expression and returns boolean value. Second operand or
expression is executed when result of first operand is true, But if result of first operand is false then,
third operand or expression is executed.
Syntax:-
Exp1?Exp2:Exp3
Example:-
class ter
{
{
int a=5,b=7;
}
}
.Bitwise operator:-Bitwise Operators works on bitsand perform bit by bit operation. They can be used
7
with any of the integer types. They are used when performing update and query operations of the Binary
indexed trees. The bitwise Operators are:-
(i) >> Shift- right:- Inserts 0 from left andright most digit is removed( once).
(ii) << Shift- left:-Inserts 0 from right and left most digit is removed(once)
(iv) & Bitwise AND:- Bit wise AND operation isperformed. if both digits are 1s,it is 1.
( vi) ^ Bitwise Exclusive OR:-Bitwise ExclusiveOR operation is performed. if one bit is 0 and other is 1,
it is 1.
Example:-
class bitw
{
{
int a=15,b=13;
}
}
. 'instanceof' operator:-It is also called objectreference variables. The operator checks whether the
1
object belongs to a particular class or interface type.
Syntax:-
2.Dot operator (.):-It is used to access the instancrvariable or methods of the objects of a class.
Syntax:-
<instanceOperatorobject>.<instanceVariable>
OR
<instanceOperatorobject>.<methods>
Example:-
class main
{
int a;
{
Obj.a=5;
}
}