# Arithmetic Operators

## Java Collection

### Java Arithmetic Operators:

In Java, arithmetic operators are used to perform basic mathematical operations on numerical data types. These operators allow you to add, subtract, multiply, divide, and perform other arithmetic operations on variables and constants. They act as basic mathematical operations.

#### Example:

```public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}```

#### Output:

30

-10

200

0

10

Here are the primary arithmetic operators in Java:

Example:

```int sum = 5 + 3; // sum will be 8
```

Subtraction (-): Subtracts the right operand from the left operand.

Example:

```int difference = 10 - 6; // difference will be 4
```

Multiplication (*): Multiplies two operands together.

Example:

```int product = 3 * 4; // product will be 12
```

Division (/): Divides the left operand by the right operand. If both operands are integers, the result will be an integer. If either operand is a floating-point number (e.g., float or double), the result will be a floating-point number.

Example:

```int quotient1 = 10 / 3; // quotient1 will be 3 (integer division)
double quotient2 = 10.0 / 3; // quotient2 will be 3.3333333333333335
```

Modulus (%): Returns the remainder of the division of the left operand by the right operand. It is often used to determine if a number is even or odd (by checking if the remainder is 0 or 1).

Example:

```int remainder = 10 % 3; // remainder will be 1
```

Increment (++) and Decrement (–):

These are unary operators used to increase or decrease the value of a variable by 1, respectively.

Example:

```int count = 5;
count++; // count is now 6
count--; // count is now 5 again
All operators example```

It’s also important to note that arithmetic operations follow the standard rules of precedence. Parentheses can be used to change the order of evaluation, just like in standard mathematics. For example:

```int result = (22 + 8) * 6; // result will be 180 (22 + 8 is evaluated first, then multiplied by 6)
```

Keep in mind that arithmetic operations are only valid for numeric data types, such as int, float, double, long, etc. Trying to perform arithmetic operations on non-numeric types will result in a compilation error.

Visited 1 times, 1 visit(s) today