Chapter 2- Operators, Types of Operators & Expressions in Java



Operators are used to perform operations on variables and values.

7 + 11 = 18

(Here, 7 and 11 are operand and "+" is the operator and 18 is the result)

Types of operators :

  1. Arithmetic Operators => [+, -, *, /, %, ++, --]
  2. Assignment Operators => [=, +=]
  3. Comparison Operators => [==, >=, <=]
  4. Logical Operators => [&&, ||, !]
  5. Bitwise Operators => [&, |] (operates bitwise)

Arithmetic operators cannot work with Booleans.

% operator can work on floats and doubles.


Precedence of operators

The operators are applied and evaluated based on precedence. For example (+, -) has less precedence compared to (*, /). Hence * and / are evaluated first.

In case we like to change this order, we use parenthesis ().


Code as Described in the Video 

package com.company;

public class CWH_Ch2_Operators {
public static void main(String[] args) {
// 1. Arithmetic Operators
int a = 4;
// int b = 6 % a; // Modulo Operator
// 4.8%1.1 --> Returns Decimal Remainder

// 2. Assignment Operators
int b = 9;
b *= 3;
System.out.println(b);

// 3. Comparison Operators
// System.out.println(64<6 4.="" logical="" operators="" system.out.println="">5 && 64>98);
System.out.println(64>5 || 64>98);

// 5. Bitwise Operators
System.out.println(2&3);
// 10
// 11
// ----
// 10
}
}

Aryadeep

Welcome to CodeWithARYA. It's My Personal Reference Website. Where You Can Get Amazing Codes. Have a Look.

Post a Comment

Previous Post Next Post