Primitive Data Types
The Math Operators
:
Java provides a rich set of operators to use in manipulating variables. A value used on either side of an operator is called an operand.
For example, in the expression below, the numbers 6 and 3 are operands of the plus operator:
int x = 6 + 3;Java provides a rich set of operators to use in manipulating variables. A value used on either side of an operator is called an operand.
For example, in the expression below, the numbers 6 and 3 are operands of the plus operator:
Java arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulo
+ addition
- subtraction
* multiplication
/ division
% modulo
Addition
:
The + operator adds together two values, such as two constants, a constant and a variable, or a variable and a variable. Here are a few examples of addition:
The + operator adds together two values, such as two constants, a constant and a variable, or a variable and a variable. Here are a few examples of addition:
int sum1 = 50 + 10;
int sum2 = sum1 + 66;
int sum3 = sum2 + sum2;
int sum2 = sum1 + 66;
int sum3 = sum2 + sum2;
Subtraction :
The - operator subtracts one value from another.
int sum1 = 1000 - 10;
int sum2 = sum1 - 5;
int sum3 = sum1 - sum2;
Multiplication
:
The * operator multiplies two values.
int sum1 = 1000 * 2;
The * operator multiplies two values.
int sum2 = sum1 * 10;
int sum3 = sum1 * sum2;
Division :
The / operator divides one value by another.
int sum1 = 1000 / 5;
int sum2 = sum1 / 2;
int sum3 = sum1 / sum2;
Modulo
:
The modulo (or remainder) math operation performs an integer division of one value by another, and returns the remainder of that division.
The operator for the modulo operation is the percentage (%) character.
The modulo (or remainder) math operation performs an integer division of one value by another, and returns the remainder of that division.
The operator for the modulo operation is the percentage (%) character.
Example :
int value = 23;
int res = value % 6; // res is 5
int res = value % 6; // res is 5
Comments
Post a Comment