Logo

Developer learning path

Java

Operators in Java

Operators

Arithmetic Operators

17

#description

In Java, arithmetic operators are used to perform mathematical computations.

The following are the arithmetic operators in Java:

  1. Addition (+): Adds two operands.

Example:

                    
int a = 5;
int b = 3;
int sum = a + b;
System.out.println("The sum of a and b is " + sum);
                  

Output:

                    
The sum of a and b is 8
                  
  1. Subtraction (-): Subtracts second operand from first.

Example:

                    
int a = 5;
int b = 3;
int diff = a - b;
System.out.println("The difference of a and b is " + diff);
                  

Output:

                    
The difference of a and b is 2
                  
  1. Multiplication (*): Multiplies two operands.

Example:

                    
int a = 5;
int b = 3;
int product = a * b;
System.out.println("The product of a and b is " + product);
                  

Output:

                    
The product of a and b is 15
                  
  1. Division (/): Divides first operand by second.

Example:

                    
int a = 5;
int b = 3;
int quotient = a / b;
System.out.println("The quotient of a and b is " + quotient);
                  

Output:

                    
The quotient of a and b is 1
                  
  1. Modulus (%): Gives remainder of first operand divided by second.

Example:

                    
int a = 5;
int b = 3;
int remainder = a % b;
System.out.println("The remainder of a and b is " + remainder);
                  

Output:

                    
The remainder of a and b is 2
                  

Note: When performing arithmetic operations with different data types, the result will be of the data type with the greatest precision. For example, when dividing an integer by a double, the result will be a double.

March 24, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.