# Operators - Ehsangood1/Programmign-2 GitHub Wiki

Operators in Java are special symbols or keywords that perform operations on variables and values. Java provides a wide range of operators that can be categorized into several types, including arithmetic, relational, logical, bitwise, assignment, and more. Each category has specific functions and uses in manipulating data and controlling program flow.

Here’s a comprehensive guide to the operators in Java:

## Arithmetic Operators

Num Operator Description Example
1 + Addition a + b
2 - Subtraction a - b
3 * Multiplication a * b
4 / Division a / b
5 % Modulus (Remainder) a % b
6 ++ Increment a++ or ++a
7 -- Decrement a-- or --a

Example

## Relational Operators

Relational operators are used to compare two values and return a boolean result.

NUM Operator Description Example
1 == Equal to a == b
2 != Not equal to a != b
3 > Greater than a > b
4 < Less than a < b
5 >= Greater than or equal to a >= b
6 <= Less than or equal to a <= b

Example

## Logical Operators

Logical operators are used to perform logical operations on boolean expressions.

Num Operator Description Example
1 && Logical AND a && b
2 || Logical OR a || b
3 ! Logical NOT !a

Exmple

## Bitwise Operators

Bitwise operators perform operations on the binary representations of integers.

NUM Operator Description Example
1 & Bitwise AND a & b
2 | Bitwise OR a | b
3 ^ Bitwise XOR a ^ b
4 ~ Bitwise NOT ~a
5 << Left shift a << 2
6 >> Right shift a >> 2
7 >>> Unsigned right shift a >>> 2

Example

## Unary Operators

Unary operators are operators that operate on a single operand to produce a new value. They are used to perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

NUM Operator Description Example
1 + Unary plus (positive) int result = +5; // result is 5
2 - Unary minus (negation) int result = -5; // result is -5
3 ++ Increment (post) int x = 5; x++; // x is 6
4 -- Decrement (post) int x = 5; x--; // x is 4
5 ! Logical complement (not) boolean flag = true; flag = !flag; // flag is false

Example

## Assignment Operators

Assignment operators in Java are used to assign values to variables. They can also be used to modify the current value of a variable by performing an operation on it and then assigning the result back to the variable.

NUM Operator Description Example
1 = Assignment int result = 5; // result is 5
3 -= Subtract and assign int result = 5; result -= 3; // result is 2
4 *= Multiply and assign int result = 5; result *= 3; // result is 15
5 /= Divide and assign int result = 5; result /= 3; // result is 1
6 %= Modulus and assign int result = 5; result %= 3; // result is 2
7 &= Bitwise AND and assign int result = 5; result &= 3; // result is 1
8 ` =` Bitwise OR and assign
9 ^= Bitwise XOR and assign int result = 5; result ^= 3; // result is 6
10 <<= Left shift and assign int result = 5; result <<= 1; // result is 10
11 >>= Right shift and assign int result = 5; result >>= 1; // result is 2
12 >>>= Unsigned right shift and assign int result = 5; result >>>= 1; // result is 2

Example

## Conditional (Ternary) Operator

The conditional (ternary) operator is a shorthand way to perform an if-else statement. It is the only operator in Java that takes three operands.

condition ? expression1 : expression2;

Operator Description Example
? : Ternary conditional int result = (5 > 3) ? 10 : 20; // result is 10

Example

## Instanceof Operator

NUM Operator Description Example
1 instanceof Checks if an object is an instance of a specific class or subclass boolean result = "Hello" instanceof String; // result is true

Example

## Type Cast Operator

Widening Casting (Implicit)

Automatically done by the Java compiler. Converting a smaller type to a larger type size. Examples: byte to short, short to int, int to long, long to float, float to double.

Narrowing Casting (Explicit)

Manually done by the programmer. Converting a larger type to a smaller size type. Examples: double to float, float to long, long to int, int to short, short to byte.

here is small project with all operator: