Java Operators

Welcome to The Coding College! In this guide, we’ll cover Java Operators, their types, and examples to help you master their usage. Operators are essential for performing operations on variables and values, making them fundamental in programming.

What Are Java Operators?

Operators are special symbols or keywords used to perform operations on variables or values. For example, + is used for addition, = for assignment, and && for logical operations.

Types of Java Operators

Java provides various operators grouped into the following categories:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Unary Operators
  6. Bitwise Operators
  7. Ternary Operator
  8. Shift Operators

1. Arithmetic Operators

Used for performing mathematical operations:

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

Example:

public class ArithmeticExample {
    public static void main(String[] args) {
        int a = 10, b = 3;
        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
        System.out.println("Modulus: " + (a % b));
    }
}

2. Relational (Comparison) Operators

Used to compare values and return a boolean (true or false).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

public class RelationalExample {
    public static void main(String[] args) {
        int x = 5, y = 10;
        System.out.println("x == y: " + (x == y));
        System.out.println("x != y: " + (x != y));
        System.out.println("x > y: " + (x > y));
        System.out.println("x < y: " + (x < y));
        System.out.println("x >= y: " + (x >= y));
        System.out.println("x <= y: " + (x <= y));
    }
}

3. Logical Operators

Used to combine multiple conditions or expressions.

OperatorDescriptionExample
&&Logical ANDa > b && a < c
``
!Logical NOT!(a > b)

Example:

public class LogicalExample {
    public static void main(String[] args) {
        int a = 10, b = 20, c = 30;
        System.out.println("a > b && a < c: " + (a > b && a < c));
        System.out.println("a > b || a < c: " + (a > b || a < c));
        System.out.println("!(a > b): " + !(a > b));
    }
}

4. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample
=Assigna = b
+=Add and assigna += b (a = a + b)
-=Subtract and assigna -= b (a = a – b)
*=Multiply and assigna *= b (a = a * b)
/=Divide and assigna /= b (a = a / b)

Example:

public class AssignmentExample {
    public static void main(String[] args) {
        int x = 5;
        x += 3;
        System.out.println("x += 3: " + x);
        x *= 2;
        System.out.println("x *= 2: " + x);
    }
}

5. Unary Operators

Operate on a single operand.

OperatorDescriptionExample
+Positive number+a
-Negative number-a
++Incrementa++ or ++a
--Decrementa-- or --a

Example:

public class UnaryExample {
    public static void main(String[] args) {
        int x = 10;
        System.out.println("x++: " + x++);
        System.out.println("++x: " + ++x);
        System.out.println("x--: " + x--);
        System.out.println("--x: " + --x);
    }
}

6. Bitwise Operators

Operate on bits and perform bit-by-bit operations.

OperatorDescriptionExample
&ANDa & b
``OR
^XORa ^ b
~Complement~a
<<Left shifta << b
>>Right shifta >> b

Example:

public class BitwiseExample {
    public static void main(String[] args) {
        int a = 5, b = 3;
        System.out.println("a & b: " + (a & b));
        System.out.println("a | b: " + (a | b));
        System.out.println("a ^ b: " + (a ^ b));
        System.out.println("~a: " + ~a);
    }
}

Practice Exercises

  1. Write a program to demonstrate the use of arithmetic and relational operators.
  2. Create a program to implement logical AND, OR, and NOT operations.
  3. Experiment with assignment operators and observe their effect on variable values.

Conclusion

Understanding Java operators is crucial for building logic in your programs. By mastering these operators, you’ll have a strong foundation for solving problems efficiently.

For more programming tutorials, visit The Coding College. Keep learning and coding! 🚀

Leave a Comment