[go: up one dir, main page]

0% found this document useful (0 votes)
8 views6 pages

Operators and Expression

The document explains the differences between operators and operands in Java, detailing various types of operators including unary, binary, and ternary operators. It also covers arithmetic, logical, relational, and assignment operators, along with their precedence and examples. Additionally, it discusses the distinction between prefix and postfix operators, including their applications and effects on variable values.

Uploaded by

mesomabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Operators and Expression

The document explains the differences between operators and operands in Java, detailing various types of operators including unary, binary, and ternary operators. It also covers arithmetic, logical, relational, and assignment operators, along with their precedence and examples. Additionally, it discusses the distinction between prefix and postfix operators, including their applications and effects on variable values.

Uploaded by

mesomabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Operators and Expression

 State the difference between operator and operand.


Operator Operand
Operators are symbols used in Operands are the data values or
Java program to specify the variables on which the operator is
operation. applied to perform the task.
E.g. - a++ , a--, a + b E.g. - a + b, a++

All are operators All are operands

Types of Operators :- In Java there are basically three types of


operators:
i. Unary Operator
ii. Binary Operator
iii. Ternary Operator
 Difference between Unary, Binary and Ternary operators.
Unary Binary Ternary
Deals with one Deals with two Deals with three
operand. operands. operands.
The operator is placed The operator is placed There are two symbols
either before the between the operands. ? and : used as ternary
operand or after the E.g. – a+b, a>b, a-b operator. It is generally
operand. used as a conditional
E.g. – a++, ++a, a--, --a operator.
E.g. – m=(a>b)?a:b;

Arithmetic Operator :- In Java there are five different arithmetic operators


as follows:
+,-,*,/,%
Modulus Operator :- It is used to calculate the remainder after division. %
symbol used as modulus operator.
E.g.

Values a/b a%b


a=5 , b=2 2 1
a=-5 , b=2 -2 -1
a=5 , b=-2 -2 1
a=-5 , b=-2 2 -1

Operator Precedence :-
1. *, / , %
2. + , -
The precedence of (* , / , %) is higher than (+, -) but the precedence of
(*, / , %) or (+ , -) are similar. According to the position of that symbol in an
expression it is evaluated left to right direction.
E.g. –
2*3/5+6–2
[here * and / both are same precedence so from left to right direction
which symbol comes first it will be evaluated. Here * comes first so only
2*3 will be evaluated and rest of things as same as it is.]
= 6 / 5 + 6 – 2 [ in this line / has the highest precedence ]
= 1 + 6 – 2 [ here + and – are same precedence so we follow the rule left to
right as we did in the first line]
=7–2
=5 (Ans.)
5 * 4 / (-3) – 6 % 3
= 20 / (-3) – 6 % 3
= -6 – 6 % 3
= -6 – 0
= -6 (Ans.)
Logical Operator :- In Java program the logical operators are used to join two
or more conditions and also reverse the condition. There are three logical
operators –
i. Logical AND (&&)
ii. Logical OR (||)
iii. Logical NOT (!)

Logical AND :- In case of Logical AND operator the condition is true if both the
conditions are correct.
E.g. if( a>b && a>c )
m=a;
Logical OR :- In case of Logical OR operator the condition is true if any one of
the condition is correct or both.
E.g. if ( a>0 || a>=1)
System.out.println(“Positive number”);
Logical NOT :- Logical NOT operator is used to reverse the condition. If it is
true initially after using Logical NOT operator it becomes false or vice-versa.
E.g. int a=10,b=5;
If ! ( a>b )

Relational Operator :- Relational operators are used to make the relation


between operands. In Java there are several relational operators-
i. >
ii. <
iii. >=
iv. <=
v. == (is equal)
vi. != (not equal)
Assignment Operator :- In Java equal to(=) symbol is termed as assignment
operator. It is used to assign the value of any variable and in some cases it is
also used as a shorthand operator.
E.g. a+=b; a=a+b;
a-=b; a=a-b;
a*=b; a=a*b;
a/=b; a=a/b;
a%=b; a=a%b;
 Difference between ! and != .
! !=
It is logical NOT operator. It is relational NOT equal operator.
It is used to reverse the It is used to check both the
condition. operands are dissimilar or not. If
yes, condition is true otherwise
false.

 Difference between = and == .


= ==
It is an assignment operator. It is relational is equal operator.
This operator is used to assign the This operator is used to compare
value of any variable. the equality between two
E.g. int a=5; operands.
E.g. if( a==b )
{
-----------
-----------
}

Unary Increment Operator :- ++ is an example Unary increment operator it


implies the variable value is increased by 1 only.
E.g. a++ , ++a . So if a=5 then a++ is 6 and same ++a is also 6.
Unary Increment Operator :- -- is an example Unary decrement operator and
it implies the variable value is decreased by 1 only.
E.g. a-- , --a. So if a=6 then a-- is 5 and same --a is also 5.
In case of Unary operator as per their application there are two categories -
i. Prefix Operator
ii. Postfix Operator

i. Prefix Operator :- In case of prefix operator the operator is placed


before the operand. It follows change before action. So the value of a
variable first changed and then it is assigned to a variable.
E.g. ++a , --a
int a=6;
x= ++a ; x= --a;
x= 7 and a= 7 x=5 and a= 5
ii. Postfix Operator :- In case of postfix operator the operator is placed
after the operand. It follows change after action. So the value of
variable assigned first then it is changed.
E.g. a++ , a--
int a=6;
y= a++; y=a--;
y=6 and a=7 y=6 and a=5

Application of Prefix and Postfix operator :-


int a=3,b=3; int A=3,B=-3; int A=3;
x= (a++) +b +a; A= A++ + B; A = A++ + ++A +A++
= 3+3+4 = 3 + (-3) =3+5+5
= 10 (Ans.) =3–3 = 13 (Ans.)
y= --b + (++a) + (b++) = 0 (Ans.)
=2+4+2 B= --B + ++A - B++
= 8 (Ans.) = -4 + 4 – (-4)
= 4 (Ans.)
 Difference between Prefix and Postfix operator.
Prefix Postfix
Prefix operator is placed always Postfix operator is placed always
before the operands. after the operands.
In case of prefix operation it In case of postfix operation it
follows change before action i.e. follows change after action i.e.
the value of the variable is the value of the variable is
changed first after that it is assigned into the another variable
assigned to the another variable. first after that it is changed.
E.g. ++a, --a E.g. a++, a--

You might also like