From ecdb4cda66f825e3c02d82c743058603735a8826 Mon Sep 17 00:00:00 2001 From: Tanisha Gupta <73746588+tanishagupta1@users.noreply.github.com> Date: Mon, 4 Oct 2021 12:28:55 +0530 Subject: [PATCH 1/3] Added java explanatory code Added java code, removed cpp code, removed operators like comma, sizeof, reference and dereference which are not present in java --- lessons/operators.md | 105 ++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 66 deletions(-) diff --git a/lessons/operators.md b/lessons/operators.md index aaaf3ed2..7124a1a5 100644 --- a/lessons/operators.md +++ b/lessons/operators.md @@ -7,7 +7,7 @@ description: "Learn java" --- Operators are symbols that tell the compiler to perform some specfic operations. -In C++, there are many operators, and they are grouped into several categories. +In JAVA, there are many operators, and they are grouped into several categories. 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. 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 Ok so let's learn about the incrementer and decrementer operators now because they are very important. -### Incrementer and Decrementer Operators +### Incrementer and Decrementer Operators/ Unary Operators -In C++ there are two types of incrementer and decrementer operators: +In Java there are two types of incrementer and decrementer operators: - **Incrementer Operator**: used to increment the value of the operand by 1. - **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 let's see an example: -```cpp +```Java // program to find whether a number is divible by both 3 and 5 // or divisible by only one of them - - #include - using namespace std; - - int main() { - int a; - cout<<"Enter a numbers: "; - cin>>a; - if(a%3==0 && a%5==0) - cout<<"the number is divisible by both 3 and 5"; +import java.util.Scanner; +public class Main + { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("Enter a number: "); + int a=sc.nextInt(); + + if(a%3==0 && a%5==0) + System.out.println("The number is divisible by both 3 and 5"); else if(a%3==0 || a%5==0) - cout<<"the numbers is divisible by only one of them"; + System.out.println("The numbers is divisible by only one of them"); else - cout<<"the numbers is not divisible by any of them"; - return 0; + System.out.println("The numbers is not divisible by any of them"); } +} ``` let's see the explanation: @@ -174,7 +174,7 @@ eg: let 8 >> 1 == ? 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. -```cpp +```java Right Shift --> a >> n ==> a * 2^n Left Shift --> a << n ==> a / 2^n ``` @@ -183,65 +183,38 @@ Left Shift --> a << n ==> a / 2^n This category contains the following operators: -- `sizof()`: it returns the size of the operand in bytes. - -```cpp - int a=5; - cout<<"the size of a is "< b) ? cout<<"a is greater than b"< 10 + ? "Number is greater than 10" + : "Number is less than or equal to 10"; + System.out.println(msg); } -``` +} -- `Reference(&) operator`: it is used to get the address of the operand. - -```cpp - int main() { - int a=5; - cout<<"the address of a is "<<&a< Date: Tue, 5 Oct 2021 11:21:19 +0530 Subject: [PATCH 2/3] Update lessons/operators.md indentation resolved Co-authored-by: Utkarsh Mishra <76392681+Utkarsh1504@users.noreply.github.com> --- lessons/operators.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lessons/operators.md b/lessons/operators.md index 7124a1a5..250c994b 100644 --- a/lessons/operators.md +++ b/lessons/operators.md @@ -205,13 +205,12 @@ This category contains the following operators: - `typecasting`: it converts the value of the operand to the type of the right operand. eg: ```java -public class Main - { - public static void main(String[] args) { - char ch = 'a'; - int typecasted_character = (int)'a'; - System.out.println(ch); - System.out.println(typecasted_character); +public class Main { + public static void main(String[] args) { + char ch = 'a'; + int typecasted_character = (int) 'a'; + System.out.println(ch); + System.out.println(typecasted_character); } } ``` From f7b5f2810ada03705a2dbc281d626d136f9e7d0d Mon Sep 17 00:00:00 2001 From: Tanisha Gupta <73746588+tanishagupta1@users.noreply.github.com> Date: Tue, 5 Oct 2021 11:51:09 +0530 Subject: [PATCH 3/3] indentation resolved in code --- lessons/operators.md | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/lessons/operators.md b/lessons/operators.md index 250c994b..9c92ffd4 100644 --- a/lessons/operators.md +++ b/lessons/operators.md @@ -81,20 +81,20 @@ let's see an example: // program to find whether a number is divible by both 3 and 5 // or divisible by only one of them import java.util.Scanner; -public class Main - { - public static void main(String[] args) { - Scanner sc=new Scanner(System.in); - System.out.println("Enter a number: "); - int a=sc.nextInt(); - - if(a%3==0 && a%5==0) - System.out.println("The number is divisible by both 3 and 5"); - else if(a%3==0 || a%5==0) - System.out.println("The numbers is divisible by only one of them"); - else - System.out.println("The numbers is not divisible by any of them"); - } +public class Main { + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + System.out.println("Enter a number: "); + int a = sc.nextInt(); + + if (a % 3 == 0 && a % 5 == 0) + System.out.println("The number is divisible by both 3 and 5"); + else if (a % 3 == 0 || a % 5 == 0) + System.out.println("The numbers is divisible by only one of them"); + else + System.out.println("The numbers is not divisible by any of them"); + } } ``` @@ -189,14 +189,11 @@ This category contains the following operators: // syntax // variable = Expression1 ? Expression2 : Expression3; - public class Main - { - public static void main(String[] args) { - int num=15; - String msg = num > 10 - ? "Number is greater than 10" - : "Number is less than or equal to 10"; - System.out.println(msg); + public class Main { + public static void main(String[] args) { + int num = 15; + String msg = num > 10 ? "Number is greater than 10" : "Number is less than or equal to 10"; + System.out.println(msg); } }