-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperators.java
More file actions
156 lines (109 loc) · 4.1 KB
/
Operators.java
File metadata and controls
156 lines (109 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Operators are symbols that are used to perform operations
public class Operators {
public static void main(String[] args) {
relational();
unary();
arithmetic();
shift();
logical();
ternary();
assignment();
}
public static void relational() {
// Relational operators are used to compare two values
// Comparison
int a = 10;
int b = 20;
System.out.println(a>b); // false
// Equality
System.out.println(a==b); // false
System.out.println(a!=b); // true
}
public static void unary() {
// Unary operators are used for:
// 1. Incrementing/Decrementing a value by one
// 2. Negating an expression
// 3. Inverting the value of a boolean
// Prefix operators - appear before a value i.e., --a, ++b, !b
Boolean a = false;
System.out.println(!a); // true
int b = 10;
System.out.println(--b); // 9
System.out.println(++b); // 10
int c = 11;
System.out.println(~c); // -12 -> Minus of total positive value
int d = -11;
System.out.println(~d); // 10 -> Positive of total minus value
// Postfix operators - appear after a value i.e., c++,c--
int e = 15;
System.out.println(e++); // 15
System.out.println(e--); // 16
}
public static void arithmetic() {
// Arithmetic operators act as basic mathematic operations
int a = 15;
int b = 10;
// Addition
System.out.println(a + b); // 25
// Subtraction
System.out.println(a - b); // 5
// Multiplication
System.out.println(a * b); // 150
// Division
System.out.println(a/b); // 1
// Modulus
System.out.println(15%10); // 5
}
public static void shift() {
// Shift operators are used to shift all the bits of a value to either left/right side
// Left shift operator
// Shifts all the bits in a value to the left side in a specified number of times
int a = 10;
System.out.println(a<<2); // 40 -> 10 * 2 ^ 2
System.out.println(a<<3); // 80 -> 10 * 2 ^ 3
// Right shift operator
// Shifts all the bits in a value to the right side in a specified number of times
int b = 10;
System.out.println(b>>2); // 2 -> 10 / 2 ^ 2
System.out.println(b>>3); // 1 -> 10 / 2 ^ 3
// >> vs >>>
// For positive values, they act the same. But for negative number >>> changes parity bit (MSB) to 0
int c = -20;
System.out.println(c>>2); // -5 -> -20 / 2 ^ 2
System.out.println(c>>>2); // 1073741819 -> -20 / 2 ^ 2
}
public static void logical() {
// Checks if conditions matches
int a = 10;
int b = 15;
int c = 20;
// Logical operators - Operate on boolean expressions
// bitwise operators - perform operations at the bit level
// Logical && (AND)
// checks the second condition if the first condition is true
System.out.println(a<b && b<c); // true -> true && true
// Bitwise &
System.out.println(a>b & a<b); // false -> false & true
// Logical || (OR)
// checks the second condition whether the first condition is true or false
System.out.println(a>b || c>b); // true -> false || true
// Bitwise |
System.out.println(a>b | c>b); // true -> false || true
}
public static void ternary() {
// one line operator that replaces if...else statement
// Takes three operands
Boolean isColored = false;
String b = isColored ? "The string is colored" : "The string is not colored";
System.out.println(b);
}
public static void assignment() {
// Assignment operators are used to assign value to a variable i.e., a = 10, b += 20
int a = 20;
System.out.println(a); // 20
a += 10;
System.out.println(a); // 30
a -= 10;
System.out.println(a); // 20
}
}