Java Arithmetic Operators

Java Arithmetic Operators

In Java, there are several arithmetic operators that you can use to perform mathematical operations on numerical values.

Here is a list of the arithmetic operators in Java:

  • +: Addition operator. Adds two operands.
  • -: Subtraction operator. Subtracts the second operand from the first.
  • *: Multiplication operator. Multiplies two operands.
  • /: Division operator. Divides the first operand by the second.
  • %: Modulo operator. Returns the remainder of the division of the first operand by the second.

Here is an example of how to use these operators:

int a = 5;
int b = 2;

int sum = a + b;    // sum is 7
int difference = a - b;    // difference is 3
int product = a * b;    // product is 10
int quotient = a / b;    // quotient is 2
int remainder = a % b;    // remainder is 1
S‮‬ource:www.lautturi.com

In this example, the variables a and b are assigned the values 5 and 2, respectively. The arithmetic operators are then used to perform various mathematical operations on these values, and the results are assigned to the variables sum, difference, product, quotient, and remainder.

Note that the / operator performs integer division, which means that it truncates any fractional part of the result. To perform floating-point division, you can use the double or float data types instead of int.

Created Time:2017-11-03 00:14:36  Author:lautturi