Java Operators – Mastering Arithmetic, Logical, and Relational Operations



Java Operators – Mastering Arithmetic, Logical, and Relational Operations

Introduction

  • What are Operators in Java?
    • Explanation of operators as symbols that perform operations on variables and values.
    • Why understanding operators is crucial in Java programming.

1. Arithmetic Operators in Java

  • Overview

    • Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, and division.
  • List of Arithmetic Operators

    OperatorDescriptionExample
    +Additiona + b
    -Subtractiona - b
    *Multiplicationa * b
    /Divisiona / b
    %Modulus (remainder)a % b
  • Examples:

    java

    int a = 10; int b = 3; System.out.println("Addition: " + (a + b)); // 13 System.out.println("Subtraction: " + (a - b)); // 7 System.out.println("Multiplication: " + (a * b)); // 30 System.out.println("Division: " + (a / b)); // 3 System.out.println("Modulus: " + (a % b)); // 1
    • Explanation: Shows the result of each arithmetic operation.

2. Logical Operators

  • Overview

    • Logical operators are used to combine multiple conditions, often in decision-making (if statements).
  • List of Logical Operators

    OperatorDescriptionExample
    &&Logical AND(a > b) && (a < c)
    ``
    !Logical NOT!(a > b)
  • Examples:

    java

    boolean x = true; boolean y = false; System.out.println("Logical AND: " + (x && y)); // false System.out.println("Logical OR: " + (x || y)); // true System.out.println("Logical NOT: " + !x); // false
    • Explanation: Demonstrates how &&, ||, and ! work with Boolean values.

3. Relational Operators

  • Overview

    • Relational operators are used to compare two values and return a boolean result (true or false).
  • List of Relational Operators

    OperatorDescriptionExample
    ==Equal toa == b
    !=Not equal toa != b
    >Greater thana > b
    <Less thana < b
    >=Greater than or equala >= b
    <=Less than or equala <= b
  • Examples:

    java

    int a = 5; int b = 3; System.out.println("Equal to: " + (a == b)); // false System.out.println("Not equal to: " + (a != b)); // true System.out.println("Greater than: " + (a > b)); // true System.out.println("Less than: " + (a < b)); // false
    • Explanation: Shows the results of comparing two numbers with each relational operator.

4. Assignment Operators

  • Overview

    • Assignment operators assign values to variables. = is the most basic, but there are compound operators for combined arithmetic and assignment.
  • List of Assignment Operators

    OperatorDescriptionExample
    =Simple assignmenta = 10
    +=Add and assigna += 5
    -=Subtract and assigna -= 3
    *=Multiply and assigna *= 2
    /=Divide and assigna /= 4
    %=Modulus and assigna %= 3
  • Examples:

    java

    int a = 10; a += 5; // a = a + 5; a is now 15 a -= 3; // a = a - 3; a is now 12 a *= 2; // a = a * 2; a is now 24 a /= 4; // a = a / 4; a is now 6 a %= 3; // a = a % 3; a is now 0
    • Explanation: Demonstrates each assignment operation, modifying a step-by-step.

5. Ternary Operator

  • Overview

    • A shorthand for if-else statements, the ternary operator allows quick condition evaluation.
  • Syntax

    java

    variable = (condition) ? expressionIfTrue : expressionIfFalse;
  • Example:

    java

    int a = 5; int b = 3; String result = (a > b) ? "a is greater" : "b is greater or equal"; System.out.println(result); // Output: a is greater
    • Explanation: This example evaluates the condition a > b. If true, it assigns "a is greater" to result; otherwise, it assigns "b is greater or equal".
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.