Operators in Java - AsgarTech
Operators in Java - AsgarTech
In Java, an operator is a symbol or set of symbols that performs an operation on one or more operands. Operators are used to manipulate values and variables in expressions and statements to produce results.
Java has several types of operators
Arithmetic Operators:
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. They include the following:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulo (%)
Assignment Operators:
Assignment operators are used to assign a value to a variable. They include the following:
- Assignment (=)
- Addition assignment (+=)
- Subtraction assignment (-=)
- Multiplication assignment (*=)
- Division assignment (/=)
- Modulo assignment (%=)
- Bitwise AND assignment (&=)
- Bitwise OR assignment (|=)
- Bitwise XOR assignment (^=)
- Left shift assignment (<<=)
- Right shift assignment (>>=)
- Unsigned right shift assignment (>>>=)
Comparison Operators:
Comparison operators are used to compare two values or variables. They include the following:
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Greater than or equal to (>=)
- Less than (<)
- Less than or equal to (<=)
Logical Operators:
Logical operators are used to combine two or more conditions to produce a boolean result. They include the following:
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)
Bitwise Operators:
Bitwise operators are used to perform operations on the binary representation of values. They include the following:
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise complement (~)
- Left shift (<<)
- Right shift (>>)
- Unsigned right shift (>>>)
Conditional Operators:
Conditional operators are used to evaluate a condition and return one of two values depending on the result. They include the following:
- Ternary operator (?:)
These are the main types of operators in Java. Understanding how to use them and how they work is essential for programming in Java.
Here are some examples of operators in Java
int x = 5;
x += 3; // equivalent to x = x + 3;
x -= 2; // equivalent to x = x - 2;
x *= 4; // equivalent to x = x * 4;
x /= 2; // equivalent to x = x / 2;
x %= 3; // equivalent to x = x % 3;
No comments: