0% found this document useful (0 votes)
7 views2 pages

Java Operators

Java operators are special symbols that perform operations on operands and return results, categorized into arithmetic, assignment, comparison, logical, bitwise, and ternary operators. Each type serves a specific purpose, such as performing calculations, assigning values, comparing data, and controlling program flow. Mastering these operators and understanding their precedence and associativity is essential for writing efficient and error-free Java programs.

Uploaded by

abhi_1991
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Java Operators

Java operators are special symbols that perform operations on operands and return results, categorized into arithmetic, assignment, comparison, logical, bitwise, and ternary operators. Each type serves a specific purpose, such as performing calculations, assigning values, comparing data, and controlling program flow. Mastering these operators and understanding their precedence and associativity is essential for writing efficient and error-free Java programs.

Uploaded by

abhi_1991
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Operators

Introduction
Operators in Java are special symbols that perform specific operations on one, two, or three
operands, and then return a result. They are fundamental building blocks of any programming
language and enable developers to manipulate data, perform calculations, make comparisons, and
control program flow. Java provides a rich set of operators categorized into different types based on
their functionality.

1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. These include addition (+),
subtraction (-), multiplication (*), division (/), and modulus (%). The modulus operator returns the
remainder of a division operation. Java also provides increment (++) and decrement (--) operators for
adding or subtracting 1 from a variable.
int a = 10, b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1

2. Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operator is the
equals sign (=). Java also provides compound assignment operators that combine arithmetic
operations with assignment, such as +=, -=, *=, /=, and %=. These operators provide a shorthand
way to update variable values.
int x = 5;
x += 3; // equivalent to x = x + 3, result: 8
x -= 2; // equivalent to x = x - 2, result: 6
x *= 4; // equivalent to x = x * 4, result: 24

3. Comparison (Relational) Operators


Comparison operators compare two values and return a boolean result (true or false). These include
equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less
than or equal to (<=). They are commonly used in conditional statements and loops to make
decisions.
int num1 = 10, num2 = 20;
boolean isEqual = (num1 == num2); // false
boolean isGreater = (num1 > num2); // false
boolean isLessOrEqual = (num1 <= num2); // true
4. Logical Operators
Logical operators are used to combine multiple boolean expressions or values. The logical AND (&&)
returns true only if both operands are true. The logical OR (||) returns true if at least one operand is
true. The logical NOT (!) inverts the boolean value. These operators support short-circuit evaluation,
meaning they stop evaluating as soon as the result is determined.
boolean a = true, b = false;
boolean result1 = a && b; // false (AND)
boolean result2 = a || b; // true (OR)
boolean result3 = !a; // false (NOT)

5. Bitwise Operators
Bitwise operators perform operations on individual bits of integer values. These include bitwise AND
(&), bitwise OR (|), bitwise XOR (^), bitwise complement (~), left shift (<<), right shift (>>), and
unsigned right shift (>>>). They are useful for low-level programming, optimization, and working with
binary data.
int x = 5; // binary: 0101
int y = 3; // binary: 0011
int andResult = x & y; // 1 (0001)
int orResult = x | y; // 7 (0111)
int leftShift = x << 1; // 10 (1010)

6. Ternary Operator
The ternary operator (?:) is a shorthand for the if-else statement. It takes three operands: a condition,
a value to return if the condition is true, and a value to return if the condition is false. It's useful for
writing concise conditional assignments.
int age = 18;
String status = (age >= 18) ? "Adult" : "Minor";
// status will be "Adult"

Understanding and effectively using operators is crucial for writing efficient Java programs. Each
operator type serves a specific purpose, and mastering them enables developers to write cleaner,
more expressive code. Operator precedence and associativity rules determine the order in which
operations are performed in complex expressions, making it important to understand these concepts
for avoiding logical errors.

You might also like