|
| 1 | +# Java Tutorial: Operators, Types of Operators & Expressions in Java |
| 2 | +- An operator is a symbol that the compiler to perform a specific operation on operands. |
| 3 | +- Example : a + b = c |
| 4 | +- In the above example, 'a' and 'b' are operands on which the '+' operator is applied. |
| 5 | + |
| 6 | +## Types of operators : |
| 7 | +### 1. Arithmetic Operators : |
| 8 | +- Arithmetic operators are used to perform mathematical operations such as addition, division, etc on expressions. |
| 9 | +- Arithmetic operators cannot work with Booleans. |
| 10 | +- % operator can work on floats and doubles. |
| 11 | +- Let x=7 and y=2 |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +### 2. Comparison Operators : |
| 16 | +- As the name suggests, these operators are used to compare two operands. |
| 17 | +- Let x=7 and y=2 |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +### 3. Logical Operators : |
| 22 | +- These operators determine the logic in an expression containing two or more values or variables. |
| 23 | +- Let x = 8 and y =2 |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +### 4. Bitwise Operators : |
| 28 | +- These operators perform the operations on every bit of a number. |
| 29 | +- Let x =2 and y=3. So 2 in binary is 100, and 3 is 011. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +## Precedence of operators |
| 34 | +- The operators are applied and evaluated based on precedence. For example, (+, -) has less precedence compared to (*, /). Hence '*' and / are evaluated first. |
| 35 | +- In case we like to change this order, we use parenthesis () |
| 36 | + |
| 37 | +## Code as Described in the Video |
| 38 | +``` |
| 39 | +package com.company; |
| 40 | +
|
| 41 | +public class CWH_Ch2_Operators { |
| 42 | + public static void main(String[] args) { |
| 43 | + // 1. Arithmetic Operators |
| 44 | + int a = 4; |
| 45 | + // int b = 6 % a; // Modulo Operator |
| 46 | + // 4.8%1.1 --> Returns Decimal Remainder |
| 47 | +
|
| 48 | + // 2. Assignment Operators |
| 49 | + int b = 9; |
| 50 | + b *= 3; |
| 51 | + System.out.println(b); |
| 52 | +
|
| 53 | + // 3. Comparison Operators |
| 54 | + // System.out.println(64<6); |
| 55 | +
|
| 56 | + // 4. Logical Operators |
| 57 | + // System.out.println(64>5 && 64>98); |
| 58 | + System.out.println(64>5 || 64>98); |
| 59 | +
|
| 60 | + // 5. Bitwise Operators |
| 61 | + System.out.println(2&3); |
| 62 | + // 10 |
| 63 | + // 11 |
| 64 | + // ---- |
| 65 | + // 10 |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-8/Chapter2.pdf) |
| 71 | + |
| 72 | +### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-8/UltimateJavaCheatSheet.pdf) |
0 commit comments