Skip to main content

Arithmetic Operators

Java is a powerful programming language that offers a wide range of operators to perform different types of operations. One of the basic types of operators that you'll use often in your programming journey is 'Arithmetic Operators'.

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. Java supports five basic arithmetic operators. They are:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Modulus (%)

Let's understand each one of these in detail.

Addition Operator (+)

The addition operator (+) adds two values together. It can be used with both integers and floating-point numbers. Here's how it works:

int a = 5;
int b = 10;
int sum = a + b; // sum will hold the value 15

Subtraction Operator (-)

The subtraction operator (-) subtracts one value from another. Like the addition operator, it can be used with both integers and floating-point numbers. Here's an example:

int a = 20;
int b = 10;
int difference = a - b; // difference will hold the value 10

Multiplication Operator (*)

The multiplication operator (*) multiplies two values. It can be used with both integers and floating-point numbers. Here's how you can use it:

int a = 5;
int b = 4;
int product = a * b; // product will hold the value 20

Division Operator (/)

The division operator (/) divides one value by another. It can be used with both integers and floating-point numbers. However, it's important to note that when used with integers, it will return the quotient of the division. Any remainder will be discarded. Here's an example:

int a = 20;
int b = 3;
int quotient = a / b; // quotient will hold the value 6, not 6.67

Modulus Operator (%)

The modulus operator (%) returns the remainder of a division operation. It can be used with both integers and floating-point numbers. Here's an example:

int a = 20;
int b = 3;
int remainder = a % b; // remainder will hold the value 2

Now that you know about the basic arithmetic operators in Java, you can start using them in your programs to perform mathematical operations. Remember, understanding these operations is fundamental to mastering more complex concepts in Java. So, take your time and practice writing programs that use these operators. Happy coding!