# Java Part 3: Operators and Expressions in Java

**Please**Subscribe Youtube| Like Facebook | Follow Twitter

## Operators and Expressions in Java

In this article, we’ll cover the most commonly used operators and expressions in Java, along with their syntax, examples, and outputs.

Java is a powerful object-oriented programming language that offers a wide range of operators and expressions to perform various operations. In this article, we’ll cover the most commonly used operators and expressions in Java, along with their syntax, examples, and outputs.

## Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations in Java. Here’s a table showing the arithmetic operators in Java and their meanings:

Operator | Title | Description |
---|---|---|

+ | Addition | Adds two operands together |

– | Subtraction | Subtracts one operand from another |

* | Multiplication | Multiplies two operands together |

/ | Division | Divides one operand by another |

% | Modulus | Returns the remainder of the division operation |

++ | Increment | Increases the value of the operand by 1 |

— | Decrement | Decreases the value of the operand by 1 |

Here’s an example that demonstrates the use of arithmetic operators in Java:

```
public class ArithmeticOperatorsExample {
public static void main(String[] args) {
int x = 10;
int y = 5;
// Addition
int result1 = x + y;
System.out.println("Addition: " + result1); // Output: 15
// Subtraction
int result2 = x - y;
System.out.println("Subtraction: " + result2); // Output: 5
// Multiplication
int result3 = x * y;
System.out.println("Multiplication: " + result3); // Output: 50
// Division
int result4 = x / y;
System.out.println("Division: " + result4); // Output: 2
// Modulus
int result5 = x % y;
System.out.println("Modulus: " + result5); // Output: 0
// Increment
x++;
System.out.println("Increment: " + x); // Output: 11
// Decrement
y--;
System.out.println("Decrement: " + y); // Output: 4
}
}
```

### Output

Addition: 15

Subtraction: 5

Multiplication: 50

Division: 2

Modulus: 0

Increment: 11

Decrement: 4

## Comparison Operators

In Java, comparison operators are used to compare two operands and return a Boolean value (true or false) based on the result of the comparison. Here’s a table showing the different comparison operators in Java, along with their meanings:

Operator | Title | Description |
---|---|---|

== | Equal to | Returns true if the two operands are equal |

!= | Not equal to | Returns true if the two operands are not equal |

> | Greater than | Returns true if the left operand is greater than the right operand |

< | Less than | Returns true if the left operand is less than the right operand |

>= | Greater than or equal to | Returns true if the left operand is greater than or equal to the right operand |

<= | Less than or equal to | Returns true if the left operand is less than or equal to the right operand |

These operators are used in conditional statements, loops, and other programming constructs to make decisions based on the values of the operands.

Here’s an example that demonstrates the use of comparison operators in Java:

```
public class ComparisonOperators {
public static void main(String[] args) {
int x = 10;
int y = 5;
// Greater than
if (x > y) {
System.out.println("x is greater than y");
}
// Less than
if (x < y) {
System.out.println("x is less than y");
}
// Equal to
if (x == y) {
System.out.println("x is equal to y");
}
}
}
```

### Output

x is greater than y

## Logical Operators

Logical operators are used to perform logical operations such as AND, OR, and NOT. These operators are used to manipulate Boolean values, which can only be true or false. The following table shows the logical operators in Java:

Operator | Title | Description |
---|---|---|

&& | Logical AND | Performs a logical AND operation between two boolean expressions. If both expressions are true, the result is true. Otherwise, the result is false. |

|| | Logical OR | Performs a logical OR operation between two boolean expressions. If either expression is true, the result is true. Otherwise, the result is false. |

! | Logical NOT | Performs a logical NOT operation on a boolean expression. If the expression is true, the result is false. If the expression is false, the result is true. |

These operators are commonly used in conditional statements and loops to evaluate Boolean expressions.

Here’s an example that demonstrates the use of logical operators in Java:

```
public class LogicalOperators {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
// Logical AND
if (a && b) {
System.out.println("Both a and b are true");
} else {
System.out.println("At least one of a and b is false");
}
// Logical OR
if (a || b) {
System.out.println("At least one of a and b is true");
} else {
System.out.println("Both a and b are false");
}
// Logical NOT
if (!a) {
System.out.println("a is false");
} else {
System.out.println("a is true");
}
}
}
```

### Output

```
At least one of a and b is false
At least one of a and b is true
a is true
```

### Conclusion

In this article, we have discussed the different types of operators and expressions in Java, including arithmetic, comparison, and logical operators. We have provided examples of how to use each type of operator and explained their meaning and functionality. It is important to understand these operators and expressions in order to write efficient and effective Java code.

**Java Beginner Tutorial Series**

- Java Part 1: Setup And Introduction
- Java Part 2: Understanding Basic Data Types And Variables In Java
- Java Part 3: Operators And Expressions In Java
- Java Part 4: Control Flow Statements In Java
- Java Part 5: Methods In Java
- Java Part 6: Arrays In Java
- Java Part 7: String Manipulation In Java
- Java Part 8: Object-Oriented Programming In Java (Classes And Objects)
- Java Part 9: Object-oriented Programming In Java (OOP Pillars)
- Java Part 10: Exception Handling In Java

**Please**Subscribe Youtube| Like Facebook | Follow Twitter