Chapter 2: Operators and Expressions

Introduction to Operators

Operators are used to perform operations on variables and values.

7 + 8 = 15
7 operand
+ operator
8 operand
= equals
15 result

Types of Operators

1. Arithmetic Operators

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement

Note:

  • Arithmetic operators cannot work with booleans
  • % operator can work on floats & doubles
  • 2. Assignment Operators

    = Simple Assignment
    + = Add and Assign
    - = Subtract and Assign
    * = Multiply and Assign
    / = Divide and Assign
    % = Modulus and Assign

    3. Comparison Operators

    == Equal to
    != Not equal to
    > Greater than
    < Less than
    >= Greater than or equal
    <= Less than or equal

    Note: '=' is used for assignment whereas '==' is used for equality check.

    4. Logical Operators

    && AND
    || OR
    ! NOT

    AND Operator (&&)

    Evaluates to true if both conditions are true

    true && true=true
    true && false=false
    false && true=false
    false && false=false

    OR Operator (||)

    Evaluates to true when at least one condition is true

    true || true=true
    true || false=true
    false || true=true
    false || false=false

    NOT Operator (!)

    Negates the given logic

    !true=false
    !false=true

    Increment and Decrement Operators

    Pre-increment (++a)

    First increment the value, then use it

    PreIncrement.java
    int a = 5;
    int b = ++a; // a becomes 6, then b gets 6
    System.out.println(a); // Output: 6
    System.out.println(b); // Output: 6

    Post-increment (a++)

    First use the value, then increment

    PostIncrement.java
    int a = 5;
    int b = a++; // b gets 5, then a becomes 6
    System.out.println(a); // Output: 6
    System.out.println(b); // Output: 5

    Note: These operators work on all data types except booleans. Data type remains the same.

    Precedence of Operators

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

    In case we like to change this order, we use parentheses.

    Operator Precedence (High to Low)

    Precedence Operators Associativity
    1 ++, -- Right to Left
    2 *, /, % Left to Right
    3 +, - Left to Right
    4 <, <=, >, >= Left to Right
    5 ==, != Left to Right
    6 && Left to Right
    7 || Left to Right
    8 =, +=, -=, *=, /=, %= Right to Left

    Practice Set

    Quick Quiz 1:

    What will be the value of x?

    Quiz1.java
    int Y = 7;  
                                int X = ++Y * 8;
    // Value of X?

    Answer: x = 64 (y becomes 8 first, then 8 * 8 = 64)

    Quick Quiz 2:

    What will be the result of the following expression?

    Quiz2.java
    float a = 7/4*9/2;

    Answer: a = 4.0 (Integer division: 7/4=1, 1*9=9, 9/2=4)

    Chapter 2 - Practice Set

    1. Expression Result

    What will be the result of the following expression: float a = 7/4*9/2

    2. Encryption Program

    Write a Java program to encrypt a grade by adding 8 to it. Decrypt it to show the correct grade.

    3. Comparison Operators

    Use comparison operators to find out whether a given number is greater than the user entered number or not.

    4. Mathematical Expression

    Write the following expression in a Java program: v² = u² + 2as

    5. Expression Value

    Find the value of the following expression: int x=7; int a = 7+49/7+35/7; Value of a?

    ← Previous: Variables Next: Strings →