Arithmetic Operators in Java
Alright, let's talk about Arithmetic Operators!
These special tools help us do math operations like adding, subtracting, multiplying, dividing, and finding remainders. Here's what we're going to explore in this lesson:
Addition Operator
Okay, let's talk about the + (Addition) operator!
This symbol helps us add values or variables together. Take a look at this example:
class Main {
public static void main(String[] args) {
int number1 = 50;
int number2 = 20;
// + adds the values of two variables
int sum = number1 + number2;
System.out.println("Sum: " + sum);
}
}
When we run this, it gives us:
Sum: 70
In this code, int sum = number1 + number2; adds the values of number1 and number2, and the result is stored in the sum variable.
We can also do the addition right inside the print statement, like this:
class Main {
public static void main(String[] args) {
int number1 = 50;
int number2 = 20;
System.out.println(number1 + number2);
}
}
Here, the + operator is used with the two variables number1 and number2. It adds their values together and then prints the result on the screen.
Remember, this is different from combining strings, which we'll explore in later lessons. If you find this confusing, don't worry! We'll clear things up as we delve deeper into the chapter about Strings.
QUIZ:
class Main {
public static void main(String[] argos) {
int x = -10;
int y = 1;
System.out.println(y + x);
}
}
Subtraction Operator
Now, let's talk about the - (subtraction) operator!
This handy symbol helps us subtract values or variables. Check out this example:
class Main {
public static void main(String[] args) {
int number1 = 50;
int number2 = 20;
// - subtracts values of two variables
int difference = number1 - number2;
System.out.println("Difference: " + difference);
}
}
When we run this, it gives us:
Difference: 30
In this code, int difference = number1 - number2; subtracts number2 from number1, and the result is stored in the difference variable.
Let's try another example:
class Main {
public static void main(String[] args) {
int number1 = 50;
int number2 = 20;
System.out.println("Difference: " + (number1 - number2));
}
}
Here, (number1 - number2) is calculated first, and then the result is combined with the string "Difference: ".
Multiplication Operator
Okay, let's talk about the * (Multiplication) operator!
This symbol helps us multiply values or variables together. Take a look at this example:
class Main {
public static void main(String[] args) {
int number1 = 50;
int number2 = 20;
// * multiplies the values of two variables
int multiply = number1 * number2;
System.out.println("Multiply: " + multiply);
}
}
When we run this, it gives us:
Multiply: 1000
In this code, int multiply = number1 * number2; multiplies the values of number1 and number2, and the result is stored in the multiply variable.
We can also do the multiplication right inside the print statement, like this:
class Main {
public static void main(String[] args) {
int number1 = 50;
int number2 = 20;
System.out.println(number1 * number2);
}
}
Division Operator
The / (division) operator works differently depending on whether it's used with integers or floating-point numbers.
- / with integers: When used with integers, the division operator gives the quotient as the result. Take a look at this example:
class Main {
public static void main(String[] args) {
// division with integers
int result = 20 / 8;
System.out.println(result);
}
}
Output
2
Here, when we divide 20 by 8, the result is 2 because the / symbol finds the quotient when dividing two integers.
- / with floating-point numbers: However, when used with floating-point numbers, the division operator gives the exact result. See this example:
class Main {
public static void main(String[] args) {
// division with floating point numbers
double result = 20.0 / 8.0;
System.out.println(result);
}
}
Output:
2.5
In this case, we have used the / operator with floating-point numbers 12.0 and 8.0. Since the result of the division is also a floating-point number, we have used the data type 'double' for the result as well.
Modulo Operator (%)
The % (modulo) operator is specifically used with integers. It divides two numbers and gives back the remainder after the division. Take a look at this example:
class Main {
public static void main(String[] args) {
// get the remainder
int remainder = 12 % 8;
System.out.println(remainder);
}
}
Output:
4
Here, the result is 4. This is because when we divide 12 by 8, the remainder is 4.
The % operator can be helpful when we want to find out what's left over after dividing two numbers.