Skip to content

Commit acf4622

Browse files
authored
Merge pull request #82 from tanishagupta1/operators
Added java explanatory code
2 parents fad217e + f7b5f28 commit acf4622

File tree

1 file changed

+38
-69
lines changed

1 file changed

+38
-69
lines changed

lessons/operators.md

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: "Learn java"
77
---
88

99
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.
1111
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.
1212

1313
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
3131

3232
Ok so let's learn about the incrementer and decrementer operators now because they are very important.
3333

34-
### Incrementer and Decrementer Operators
34+
### Incrementer and Decrementer Operators/ Unary Operators
3535

36-
In C++ there are two types of incrementer and decrementer operators:
36+
In Java there are two types of incrementer and decrementer operators:
3737

3838
- **Incrementer Operator**: used to increment the value of the operand by 1.
3939
- **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
7777

7878
let's see an example:
7979

80-
```cpp
80+
```Java
8181
// program to find whether a number is divible by both 3 and 5
8282
// or divisible by only one of them
83-
84-
#include <iostream>
85-
using namespace std;
86-
87-
int main() {
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+
import java.util.Scanner;
84+
public class Main {
85+
public static void main(String[] args)
86+
{
87+
Scanner sc = new Scanner(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+
else if (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+
}
9999
```
100100

101101
let's see the explanation:
@@ -174,7 +174,7 @@ eg: let 8 >> 1 == ?
174174
175175
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.
176176
177-
```cpp
177+
```java
178178
Right Shift --> a >> n ==> a * 2^n
179179
Left Shift --> a << n ==> a / 2^n
180180
```
@@ -183,65 +183,34 @@ Left Shift --> a << n ==> a / 2^n
183183
184184
This category contains the following operators:
185185
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-
195186
- `condition ? true_expression : false_expression`: it evaluates the true_expression if the condition is true and false_expression if the condition is false.
196187
197-
```cpp
188+
```java
198189
// syntax
199190
// variable = Expression1 ? Expression2 : Expression3;
200191

201-
int main() {
202-
int a=5, b=6;
203-
(a > b) ? cout<<"a is greater than b"<<endl : cout<<"a is less than b";
204-
return 0;
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-
int main() {
219-
int a=5, b=6;
220-
(a, b) = (b, a);
221-
return 0;
192+
public class Main {
193+
public static void main(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);
222197
}
223-
```
198+
}
224199

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-
}
233200
```
234201
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-
int main() {
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+
public class Main {
206+
public static void main(String[] args) {
207+
char ch = 'a';
208+
int typecasted_character = (int) 'a';
209+
System.out.println(ch);
210+
System.out.println(typecasted_character);
242211
}
212+
}
243213
```
244-
245214
## **Operators Precedence**
246215
247216
Operators Precedence is the order in which the operators are evaluated. Below is the precedence of the operators.

0 commit comments

Comments
 (0)