Operators
Operators are symbols that do some kind of operations (actions) on one or more operands.
E.g.: a = 5. Here = is an operator. It assigns (stores) value 5 to the variable a.
Its name is assignment operator.
Operands
Operands are variables or values on which an operation is carried out.
E.g.: In a = 5 the a and 5 are operands. The = operator operates on variable a and 5.
Expression
Expression is a valid combination of operator and operands (variable and value).
E.g.: a+5 The + is an operator. The a and 5 are operands.
The 5=a is not an expression because it is not a valid combination. The valid expression is a=5
Statement
Statement is a valid combination of tokens. It can contians all tokens or some of them.
The data type (keyword like int) is not necessary, but the ; (punctuator) is essential.
E.g.1: int a=7; (All tokens are included in it)
E.g.2: a++; This also a statement because there is ; Its meaning is a=a+1. There is = also in it.
Without ; a++ is an expression. In an expression there will not a data type and ;
Do Yourself
State whether the following are: Operator or Operand or Expression or Statement:
(1) m%2
(2) m
(3) 2
(4) m=a*b;
(5) %
(6) a/b
(7) +
(8) a=a+b;
Answers
(1) Expression
(2) Operand
(3) Operand
(4) Statement
(5) Operator
(6) Expression
(7) Operator
(8) Statement
1. Assignment Operator
The = is assignment operator; it is used to assign (store) a value to a variable. E.g.: a=5;
Right side of the = is executed first
E.g.1: a=3+4;
The 3+4 is executed first. It is 7. This 7 is stored to a.
So the value of a is 7.
E.g.2: a=b=3+4;
The 3+4 is executed first. It is 7. This 7 is stored to b. Next the 7 in b is
stored in a.
So the values of a and b are 7.
Only one value can be stored in a variable at a time
When another value is stored in the variable then the first value is erased.
E.g.: int a=3; System.out.println("a="+a);
a=5; System.out.println("a="+a);
The output is
a=3
a=5
Do Yourself
a) Write output:
int x,y;
x=y=7+2;
System.out.println("x="+x);
System.out.println("y="+y);
b) Correct errors if any:
int m=5, n=3, s;
m+n=s;
System.out.println("Sum="+s);
c) Write output:
int a=5, b=3;
a=b;
b=a;
System.out.println("a="+a);
System.out.println("b="+b);
d) Write output:
int a=5, b=3, t; //t means temp
t=a;
a=b;
b=t;
System.out.println("a="+a);
System.out.println("b="+b);
a=3
b=5
Note: Above are the statements to interchange values in two variables
2. Sign Operators (Unary Plus and Unary Minus)
Operators to indicate whether a number is positive and negative. E.g.: +a and -a
Write the output:
int a=3, b=-4;
int c=-a, d=-b;
System.out.print(a+" "+b);
System.out.print(c+" "+d);
Output:
3 -4
-3 4
Write Output
int m=-4, n=6;
int x=-m*-m, y=-n*n;
System.out.print(m+" "+n);
System.out.print(x+" "+y);
Operator Precedence and Associativity
Order of evaluation among various operators is Operator Precedence and Associativity.
1. Define operator precedence with an example.
Operator precedence refers to the order of evaluation among operators in an expression with different operators.
E.g.: Among operators = + /, the / has higher precedence, next + and last is =.
Write value of a
a = 6 + 8 / 2.
a = 6 + 8 / 2
a = 6 + 4
a = 10
Write value of x
x = 10 - 3 * 2.
2. Define operator associativity with an example.
Associativity refers to the order of evaluation among operators having same precedence. The evaluation can be either
from left to right or right to left. Operators * / % have same precedence. Associativity is left to right
i.e., first operator executes first second one secondly … last one lastly. The + - also have same precedence,
associatvity is left to right itself.
Example 1 of left to right:
Write value of a. a = 6 % 8 * 2 / 3 * 5.
a = 6 % 8 * 2 / 3 * 5
a = 6 * 2 / 3 * 5
a = 12 / 3 * 5
a = 4 * 5
a = 20
Write value of x
x = 6 % 8 * 2 / 3 * 5.
Example 2 of left to right:
Write value of a. a = 6 + 8 – 4 + 5
a = 6 + 8 - 4 + 5
a = 14 – 4 + 5
a = 10 + 5
a = 15
Write value of x
x = 8 - 6 + 5 - 4
Example of right to left:
Write value of a and b: a = b = 5+3
a = b = 5+3 At first 5+3. Then,
a = b = 8 Next b = 8. Then,
a = b i.e., a = 8, So,
a = 8, b = 8
Write value of x and y
x = y = 8-5
Use of ()
Operators inside () gets higher precedence.
Examples:
1) 4+6/2 is 7. 6/2 executes first. It is 3. Then 4+3 is 7.
2) (4+6)/2 is 5. 4+6 executes first. It is 10. Then 10/2 is 5.
Write value of m and n
m=10-6/2
n=(10-6)/2
Same or Lower Precedence Operators Before Higher Operator
When there are multiple same or lower precedence operators before higher precedence operator
then except the last one all other operators execute before the higher preceence operator.
Example:
3+5+6/3
3+5+6/3
8+6/3
8+6/3
8+2
10
Result of the following calculation also same:
3+5+6/3
3+5+6/3
3+5+2
8+2
10