Java Language – 4 – Operators

Java Fundamentals – Operators
Introduction

Operators are fundamental building blocks in Java that enable you to perform various operations on data, from basic arithmetic calculations to complex logical evaluations. In this guide, we’ll explore the most common Java operators and how they are used in everyday programming.

Arithmetic Operators

Java provides a set of arithmetic operators for performing mathematical calculations:


int x = 10;
int y = 5;

int sum = x + y; // Addition
int difference = x - y; // Subtraction
int product = x * y; // Multiplication
int quotient = x / y; // Division
int remainder = x % y; // Modulo (remainder)


These operators allow you to manipulate numeric data in your Java programs.

Comparison Operators

Comparison operators are used to compare two values and return a boolean result:


int a = 10;
int b = 20;

boolean isEqual = (a == b); // Equal to
boolean isNotEqual = (a != b); // Not equal to
boolean isGreaterThan = (a > b); // Greater than
boolean isLessThan = (a < b); // Less than
boolean isGreaterOrEqual = (a >= b); // Greater than or equal to
boolean isLessOrEqual = (a <= b); // Less than or equal to


These operators are essential for making decisions in your Java programs.

Logical Operators

Logical operators are used to perform logical operations and combine boolean values:


boolean p = true;
boolean q = false;

boolean andResult = p && q; // Logical AND
boolean orResult = p || q; // Logical OR
boolean notResult = !p; // Logical NOT


Logical operators are handy for creating complex conditions and making decisions in your code.

Assignment Operators

Assignment operators are used to assign values to variables and update their values:


int num = 10;
num += 5; // Equivalent to num = num + 5
num -= 3; // Equivalent to num = num - 3
num *= 2; // Equivalent to num = num * 2
num /= 4; // Equivalent to num = num / 4
num %= 3; // Equivalent to num = num % 3


Assignment operators make it easy to update variable values based on calculations.

Increment and Decrement Operators

Java provides increment and decrement operators to change the value of a variable by 1:


int count = 5;
count++; // Increment by 1 (count is now 6)
count--; // Decrement by 1 (count is now 5)


These operators are useful for loop control and keeping track of counts.

Conditional (Ternary) Operator

The conditional operator (also known as the ternary operator) provides a concise way to express conditional statements:


int x = 10;
int y = 5;

int result = (x > y) ? x : y;


In this example, ‘result’ will be assigned the value of ‘x’ if the condition is true, or ‘y’ if it’s false.

Bitwise Operators

Bitwise operators are used for manipulating individual bits within integer values. They are typically used in low-level operations:


int a = 5; // Binary: 0101
int b = 3; // Binary: 0011

int andResult = a & b; // Bitwise AND
int orResult = a | b; // Bitwise OR
int xorResult = a ^ b; // Bitwise XOR
int notResult = ~a; // Bitwise NOT
int leftShiftResult = a << 2; // Left shift by 2 bits
int rightShiftResult = a >> 1; // Right shift by 1 bit


Bitwise operators are typically used in scenarios where low-level bit manipulation is required.

Conclusion

Understanding the various operators in Java is crucial for performing a wide range of operations in your programs. Whether you’re doing simple arithmetic, making decisions, or performing low-level bit manipulation, Java operators are your tools for getting the job done.