Java Programming/Basic Arithmetic
From Wikibooks, the open-content textbooks collection
| Navigate Basic Arithmetic topic:
|
| Beginners Topics: |
[edit] Using Arithmetic Within Java !
In order to do arithmetic in Java, one must first declare at least one integer. Typically one declares an integer and assigns it a value before any arithmetic is done. Here's an example of declaring an integer variable:
int x = 5;
After creating a variable, one can manipulate its value by using Java's operators (the examples use our previously declared x variable):
| Operator | Function | Example input | Example output |
|---|---|---|---|
| + | Addition | x + 2 | 7 |
| - | Subtraction | x - 4 | 1 |
| * | Multiplication | x * 3 | 15 |
| / | Division | x / 2 | 2 |
| % | Modulus | x % 2 | 1 |
| ++ | Increment by one | x++ | 6 |
| -- | Decrement by one | x-- | 4 |
Note that by inserting these example inputs into Java source code, no output would be produced. To print the results of a math function, you would use:
System.out.println(x + 2);
When using several operators in the same expression, one must consider Java's order of precedence. Java uses the standard PEMDAS (Parenthesis, Exponents, Multiplication and Division, Addition and Subtraction) order. When there are multiple instances of the same precedence, Java reads from left to right. Consider what the output of the following code would be:
System.out.println(10 * 5 + 100 / 10 - 5 + 7 % 2);
The following chart shows how Java would compute this expression:
10 * 5 + 100 / 10 - 5 + 7 % 2
\ /
50 + 100 / 10 - 5 + 7 % 2
\ /
50 + 10 - 5 + 7 % 2
\ /
50 + 10 - 5 + 1
\ /
60 - 5 + 1
\ /
\ /
55 + 1
\ /
\ /
\ /
56
The output would be "56"
Besides performing mathematic functions, there are also operators to assign numbers to variable (each example again uses the variable initialized as x=5):
| Operator | Function | Example input | Example output |
|---|---|---|---|
| = | Assignment | x = 3 | 3 |
| += | Assignment x to itself plus another integer |
x += 5 | 10 |
| -= | Assignment x to itself minus another integer |
x -= 4 | 1 |
| *= | Assignment x to itself multiplied by another integer |
x *= 6 | 30 |
| /= | Assignment x to itself divided by another integer |
x /= 5 | 1 |
A third kind of operator does not return or assign numbers. The following are Java's evaluation operators, which either return "true" or "false.":
| Operator | Function | Example input | Example output |
|---|---|---|---|
| < | Less than | x < 7 | true |
| > | Greater than | x > 5 | false |
| <= | Less than or equal to | x <= 5 | true |
| >= | Greater than or equal to | x >= 1 | true |
| == | Equal to | x == 5 | true |
| != | Not equal to | x != 5 | false |
These statements come in handy when using if-else, statements and for and while loops. What would the output of the following code be?
public class Test {
public static void main(String[] args) {
int x = 4;
int y = 4;
int z = 6;
x++;
y *= 2;
z += x;
if (x <= y) {
System.out.println("x is less than or equal to y.");
}
if (z <= y) {
System.out.println("z is less than y.");
}
if (x != z) {
System.out.println("x is not equal to z");
}
}
}
This code would print:
x is less than or equal to y. x is not equal to z

