You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/operators.md
+38-69Lines changed: 38 additions & 69 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ description: "Learn java"
7
7
---
8
8
9
9
Operators are symbols that tell the compiler to perform some specfic operations.
10
-
In C++, there are many operators, and they are grouped into several categories.
10
+
In JAVA, there are many operators, and they are grouped into several categories.
11
11
We are going to learn some of the important operators only. You will see these operators in most of the section and this will cover almost every problem you will encounter in your programming journey.
12
12
13
13
Ok, So there are basically 6 categories of operators:
@@ -31,9 +31,9 @@ Arithmetric operators are used to perform arithmetic operations. They are used t
31
31
32
32
Ok so let's learn about the incrementer and decrementer operators now because they are very important.
33
33
34
-
### Incrementer and Decrementer Operators
34
+
### Incrementer and Decrementer Operators/ Unary Operators
35
35
36
-
In C++ there are two types of incrementer and decrementer operators:
36
+
In Java there are two types of incrementer and decrementer operators:
37
37
38
38
-**Incrementer Operator**: used to increment the value of the operand by 1.
39
39
-**Decrementer Operator**: used to decrement the value of the operand by 1.
@@ -77,25 +77,25 @@ Logical operators are used to perform logical operations. They are used to perfo
77
77
78
78
let's see an example:
79
79
80
-
```cpp
80
+
```Java
81
81
// program to find whether a number is divible by both 3 and 5
82
82
// or divisible by only one of them
83
-
84
-
#include<iostream>
85
-
usingnamespacestd;
86
-
87
-
intmain() {
88
-
int a;
89
-
cout<<"Enter a numbers: ";
90
-
cin>>a;
91
-
if(a%3==0 && a%5==0)
92
-
cout<<"the number is divisible by both 3 and 5";
93
-
else if(a%3==0 || a%5==0)
94
-
cout<<"the numbers is divisible by only one of them";
95
-
else
96
-
cout<<"the numbers is not divisible by any of them";
97
-
return 0;
98
-
}
83
+
importjava.util.Scanner;
84
+
publicclassMain {
85
+
publicstaticvoidmain(String[] args)
86
+
{
87
+
Scanner sc =newScanner(System.in);
88
+
System.out.println("Enter a number: ");
89
+
int a = sc.nextInt();
90
+
91
+
if (a %3==0&& a%5==0)
92
+
System.out.println("The number is divisible by both 3 and 5");
93
+
elseif (a %3==0|| a%5==0)
94
+
System.out.println("The numbers is divisible by only one of them");
95
+
else
96
+
System.out.println("The numbers is not divisible by any of them");
97
+
}
98
+
}
99
99
```
100
100
101
101
let's see the explanation:
@@ -174,7 +174,7 @@ eg: let 8 >> 1 == ?
174
174
175
175
here one thing to keep in mind that `>>` operator shifts the bits to the right and `<<` shifts the bits to the left. there is a way to find right shift and left shift of a number.
176
176
177
-
```cpp
177
+
```java
178
178
Right Shift --> a >> n ==> a *2^n
179
179
Left Shift --> a << n ==> a /2^n
180
180
```
@@ -183,65 +183,34 @@ Left Shift --> a << n ==> a / 2^n
183
183
184
184
This category contains the following operators:
185
185
186
-
- `sizof()`: it returns the size of the operand in bytes.
187
-
188
-
```cpp
189
-
int a=5;
190
-
cout<<"the size of a is "<<sizof(a)<<" bytes";
191
-
```
192
-
193
-
The next one is:
194
-
195
186
- `condition ? true_expression : false_expression`: it evaluates the true_expression if the condition is true and false_expression if the condition is false.
(a > b) ? cout<<"a is greater than b"<<endl : cout<<"a is less than b";
204
-
return0;
205
-
}
206
-
```
207
-
208
-
- `cast`: it converts the value of the operand to the type of the right operand.
209
-
210
-
```cpp
211
-
eg: int ch ='a';
212
-
int ch = (int)'a';
213
-
```
214
-
215
-
-`comma(,)`: causes a series of operations to occur in the order they are written.
216
-
217
-
```cpp
218
-
intmain() {
219
-
int a=5, b=6;
220
-
(a, b) = (b, a);
221
-
return 0;
192
+
publicclassMain {
193
+
publicstaticvoidmain(String[] args) {
194
+
int num =15;
195
+
String msg = num >10?"Number is greater than 10":"Number is less than or equal to 10";
196
+
System.out.println(msg);
222
197
}
223
-
```
198
+
}
224
199
225
-
- `Reference(&) operator`: it is used to get the address of the operand.
226
-
227
-
```cpp
228
-
int main() {
229
-
int a=5;
230
-
cout<<"the address of a is "<<&a<<endl;
231
-
return 0;
232
-
}
233
200
```
234
201
235
-
-`dereference(*) or pointer`: it is used to get the value of the operand. it is also known as \* pointer. Pointer is used to access the memory location of the variable.
236
-
237
-
```cpp
238
-
intmain() {
239
-
int a=5;
240
-
cout<<"the value of a is "<<*&a<<endl;
241
-
return 0;
202
+
- `typecasting`: it converts the value of the operand to the type of the right operand.
203
+
eg:
204
+
```java
205
+
publicclassMain {
206
+
publicstaticvoidmain(String[] args) {
207
+
char ch ='a';
208
+
int typecasted_character = (int) 'a';
209
+
System.out.println(ch);
210
+
System.out.println(typecasted_character);
242
211
}
212
+
}
243
213
```
244
-
245
214
## **Operators Precedence**
246
215
247
216
Operators Precedence is the order in which the operators are evaluated. Below is the precedence of the operators.
0 commit comments