Java Part 3: Operators and Expressions in Java

Java Part 3: Operators and Expressions in Java


Please Subscribe Youtube| Like Facebook | Follow Twitter

Operators and Expressions in Java

In this article, we’ll cover the most commonly used operators and expressions in Java, along with their syntax, examples, and outputs.

Java is a powerful object-oriented programming language that offers a wide range of operators and expressions to perform various operations. In this article, we’ll cover the most commonly used operators and expressions in Java, along with their syntax, examples, and outputs.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations in Java. Here’s a table showing the arithmetic operators in Java and their meanings:

OperatorTitleDescription
+AdditionAdds two operands together
SubtractionSubtracts one operand from another
*MultiplicationMultiplies two operands together
/DivisionDivides one operand by another
%ModulusReturns the remainder of the division operation
++IncrementIncreases the value of the operand by 1
DecrementDecreases the value of the operand by 1

Here’s an example that demonstrates the use of arithmetic operators in Java:

public class ArithmeticOperatorsExample {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;

        // Addition
        int result1 = x + y;
        System.out.println("Addition: " + result1); // Output: 15

        // Subtraction
        int result2 = x - y;
        System.out.println("Subtraction: " + result2); // Output: 5

        // Multiplication
        int result3 = x * y;
        System.out.println("Multiplication: " + result3); // Output: 50

        // Division
        int result4 = x / y;
        System.out.println("Division: " + result4); // Output: 2

        // Modulus
        int result5 = x % y;
        System.out.println("Modulus: " + result5); // Output: 0

        // Increment
        x++;
        System.out.println("Increment: " + x); // Output: 11

        // Decrement
        y--;
        System.out.println("Decrement: " + y); // Output: 4
    }
}

Output

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
Increment: 11
Decrement: 4

Comparison Operators

In Java, comparison operators are used to compare two operands and return a Boolean value (true or false) based on the result of the comparison. Here’s a table showing the different comparison operators in Java, along with their meanings:

OperatorTitleDescription
==Equal toReturns true if the two operands are equal
!=Not equal toReturns true if the two operands are not equal
>Greater thanReturns true if the left operand is greater than the right operand
<Less thanReturns true if the left operand is less than the right operand
>=Greater than or equal toReturns true if the left operand is greater than or equal to the right operand
<=Less than or equal toReturns true if the left operand is less than or equal to the right operand

These operators are used in conditional statements, loops, and other programming constructs to make decisions based on the values of the operands.

Here’s an example that demonstrates the use of comparison operators in Java:

public class ComparisonOperators {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;

        // Greater than
        if (x > y) {
            System.out.println("x is greater than y");
        }

        // Less than
        if (x < y) {
            System.out.println("x is less than y");
        }

        // Equal to
        if (x == y) {
            System.out.println("x is equal to y");
        }
    }
}

Output

x is greater than y

Logical Operators

Logical operators are used to perform logical operations such as AND, OR, and NOT. These operators are used to manipulate Boolean values, which can only be true or false. The following table shows the logical operators in Java:

OperatorTitleDescription
&&Logical ANDPerforms a logical AND operation between two boolean expressions. If both expressions are true, the result is true. Otherwise, the result is false.
||Logical ORPerforms a logical OR operation between two boolean expressions. If either expression is true, the result is true. Otherwise, the result is false.
!Logical NOTPerforms a logical NOT operation on a boolean expression. If the expression is true, the result is false. If the expression is false, the result is true.

These operators are commonly used in conditional statements and loops to evaluate Boolean expressions.

Here’s an example that demonstrates the use of logical operators in Java:

public class LogicalOperators {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;

        // Logical AND
        if (a && b) {
            System.out.println("Both a and b are true");
        } else {
            System.out.println("At least one of a and b is false");
        }

        // Logical OR
        if (a || b) {
            System.out.println("At least one of a and b is true");
        } else {
            System.out.println("Both a and b are false");
        }

        // Logical NOT
        if (!a) {
            System.out.println("a is false");
        } else {
            System.out.println("a is true");
        }
    }
}

Output

At least one of a and b is false
At least one of a and b is true
a is true

Conclusion

In this article, we have discussed the different types of operators and expressions in Java, including arithmetic, comparison, and logical operators. We have provided examples of how to use each type of operator and explained their meaning and functionality. It is important to understand these operators and expressions in order to write efficient and effective Java code.

Java Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *