Java Basics Operators

In Java, operators are symbols or keywords that perform operations on variables or values. Here are some of the basic operators in Java:

1. Arithmetic operators: These are used to perform mathematical operations on numeric values. The arithmetic operators in Java include:

– `+`: addition
– `-`: subtraction
– `*`: multiplication
– `/`: division
– `%`: modulus (returns the remainder of a division)

For example:

int x = 10;
int y = 3;
int result = x % y; // result is 1

2. Assignment operators: These are used to assign values to variables. The assignment operator in Java is `=`. For example:

int x = 10;
x += 5; // equivalent to x = x + 5

3. Comparison operators: These are used to compare values. The comparison operators in Java include:

– `==`: equal to
– `!=`: not equal to
– `<`: less than - `>`: greater than
– `<=`: less than or equal to - `>=`: greater than or equal to

For example:

int x = 10;
int y = 5;
boolean result = x > y; // result is true

4. Logical operators: These are used to combine boolean expressions. The logical operators in Java include:

– `&&`: logical AND (returns true if both expressions are true)
– `||`: logical OR (returns true if at least one expression is true)
– `!`: logical NOT (negates the expression)

For example:

int x = 10;
int y = 5;
boolean result = x > y && x < 20; // result is true

5. Increment and decrement operators: These are used to increment or decrement numeric values by 1. The increment operator is `++`, and the decrement operator is `--`. For example:

int x = 10;
x++; // equivalent to x = x + 1

6. Conditional operator: This operator is used to perform a conditional operation based on a boolean expression. The conditional operator in Java is `? :`. For example:

int x = 10;
int y = 5;
int result = x > y ? x : y; // result is 10

This assigns the value of `x` to `result` if `x` is greater than `y`, and the value of `y` to `result` otherwise.