Java Operators Notes
Java Operators Notes
2 ---------------
3 C=A+B
4 C,A,B are the operands
5 + is the operator
6 = is the assignment operator
7 Operators are of three types based on the number of operands.
8 Operator
9 |
10 -----------------------------------------------------------
11 | | |
12 Unary(one operand) Binary(Two operands) Ternary(2 or more operands)
13 | | |
14 --- Unary + --- Arithmetic ?: (ternary)
15 --- Unary - --- Shorthand
16 --- Not !(Logical) --- Relational
17 --- Postfix --- Logical
18 --- Prefix --- Bitwise (Class 11 & 12)
19
20 Unary operator: Suppose A=5, B=-6
21 ---------------
22 1) Unary + C = + A => C=+5 => C=5
23 C = + B => C=+(-6)=> C=-6
24
25 2) Unary - C = - A => C=-5
26 C = - B => C=-(-6) => C=6
27
28 3) Postfix (***keep in mind that A++ / ++A both means A is increaseed by 1)
29 (similarly A-- / --A both means A is decreased by 1)
30
31 C = A++ (here the value of A will be copied to C and then A will be
32 increased by 1)
33 C = 5,A = 6
34
35 C = A-- (here the value of A will be copied to C and then A will be
36 decreased by 1)
37 C = 5,A = 4
38
39 4)Prefix C = ++A (here the value of A will be increased by 1 and then copied to C)
40 A = 6, C = 6
41
42 C = --A (here the value of A will be decreased by 1 and then copied to C)
43 A = 4, C = 4
44
45 Binary
46 ------
47
48 (a) Arithmetic Operator: A=5,B=2,C=6,D=2.0
49 ---------------------
50 (i) + (Addition) E=A+B=>E=5+2=>E=7 E=A+D=> E=5+2.0=>E=7.0
51 (ii) - (Subtraction) E=A-B=>E=5-2=>E=3 E=A-D=> E=5-2.0=>E=3.0
52 (iii) * (Multiplication) E=A*B=>E=5*2=>E=10 E=A*D=>E=5*2.0=>E=10.0
53 (iv) / (Division)
54 (If both numbers are integers that, numbers which do not have decimal point
place)
55 E=A/B => E=5/2 =>E=2 (quotient)
56 E=A/C => E=5/6 => E=0 (L<R,/,0)
57 L R
58 (Now if any one of the values is having a decimal point place, then perform
59 normal division)
60 E=A/D => E=5/2.0 => E=2.5
61 E=D/A => E=2.0/5 => E=0.4
62 (v) % (Modulus or Remainder)
63 E=A%B => E=5%2 => E=1
64 E=B%C => E=2%6 => E=2 (L<R,%,L)
65 L R
66
67 (b) Relational Operator: A=5,B=2,C=6,D=5
68 --------------------
69 (i) less than < , A<C,True, A<B,False, A<D,False
70 (ii) greater than > A>C, False, A>B,True, A>D,False
71 (iii) less than equal to <= A<=C,True, A<=D, True, A<=B,False
72 (iv) greater than equal to >= A>=C,False, A>=D,True,
73 (v) equal to == A==D,True, A==C,False (same true, different false)
74 (vi) not equal to != A!=D,False, A!=C,True (same false, different true)
75
76 (c) Logical Operator: A=5,B=2,C=6,D=5
77 -----------------
78 (i) and ( && ): If all the conditions are true then only the final answer
79 is true, otherwise false.
80 Examples:
81 (a) A>B && A==D
82 5>2 && 5==5
83 True && True => True
84
85 (b) A!=C && A==D && B>C
86 5!=6 && 5==5 && 2>6
87 True && True && False, => False
88
89 (ii) or ( || ) : If any one of the conditions are true then the final answer
90 is true, if all the conditions are false then the final
91 answer is false.
92 Examples:
93 (a) A!=C || A==D || B>C
94 True || True || False, => True
95
96 (b) A==C || A!=D || B>C
97 False || False || False , => False
98
99 (iii) not ! : It converts true to false and vice versa.
100 Examples:
101 (a) !(A==C && B>C), !(False && False), !(False), True
102 (b) !(A!=C && B<C), !(True && True) , !(True), False
103
104 (d) Shorthand Operator : A=5,B=2
105 --------------------
106 (i) A+=B => A=A+B, A=5+2=7
107 (ii) A-=B => A=A-B, A=5-2=3
108 (iii) A*=B => A=A*B, A=5*2=10
109 (iv) A/=B => A=A/B, A=5/2 = 2
110 (v) A%=B => A=A%B, A=5%2=1
111