Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Mathematical Operations in Java | Basic Types, Operations
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

book
Mathematical Operations in Java

Operators

In the previous chapter, we observed that we used basic mathematical operators such as +, -, /, and *. These operators are familiar to us from calculators or mathematics; you can also use them in a code editor.

Let's go through the basics:

  • + – addition;
  • - – subtraction;
  • / – division;
  • * – multiplication.

You can use these four fundamental operators with numerical data types (byte, short, long, float, double).

Note

If you have gone through the previous chapter, you understand you can use these operators with char, but we won't focus on that now.

Let's explore the usage of these operators with an example code:

java

Main

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { int a = 10; // creating an int variable with value 10 int b = 17; // creating an int variable with value 17 int res = a + b; /* creating an int variable that will represent the sum of value a and value b */ System.out.println(res); // printing result to the console } }

As we can see, the variable res holds the value 27, which is the sum of 10 and 17.

Let's consider a few more examples:

java

Main

copy
12345678910111213141516171819
package com.example; public class Main { public static void main(String[] args) { int plus = 16 + 4; // creating an int variable with value of sum of 16 and 4 int minus = plus - 10; /* creating an int variable with value of subtraction of plus variable and 10 */ int multiplying = minus * 4; /* variable that holds a value of multiplying of minus variable and 4 */ int complexDivision = (multiplying - 4) / 9; /* using a subtraction and division operations on our "multiplying" variable */ System.out.println(complexDivision); //printing result to the console } }

We can use both numbers and variables in our operations. However, it's worth remembering that creating multiple variables adds to the stack memory. Therefore, it's recommended to use regular numbers when possible. Additionally, we can observe that operator precedence is followed. Operations inside parentheses are performed first, followed by multiplication or division, and then addition or subtraction.

In this way, we can perform simple operations with different numeric data.

Note

It's also worth mentioning integer division with remainder. By default, Java will round the result to the nearest integer value. We can address this issue by changing the variable to a floating-point data type.

Order of Operations

Java follows the basic principles of mathematics, and operations also have an order of execution. Let's take a look at an example:

java

main

copy
12345678
package com.example; public class Main { public static void main(String[] args) { int result = (10 + 5) * 2 - 8 / 4 + 1; System.out.println(result); } }

Here we arrived at the result by performing operations sequentially. Let's take a look at the order:

So, you can prioritize the execution of operations using regular parentheses, just like in arithmetic.

Now, let's move on to a simple exercise to reinforce the material.

Task
test

Swipe to show code editor

Try writing code that calculates the sum of the first and second numbers and then divides the result by the third number:

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
toggle bottom row

book
Mathematical Operations in Java

Operators

In the previous chapter, we observed that we used basic mathematical operators such as +, -, /, and *. These operators are familiar to us from calculators or mathematics; you can also use them in a code editor.

Let's go through the basics:

  • + – addition;
  • - – subtraction;
  • / – division;
  • * – multiplication.

You can use these four fundamental operators with numerical data types (byte, short, long, float, double).

Note

If you have gone through the previous chapter, you understand you can use these operators with char, but we won't focus on that now.

Let's explore the usage of these operators with an example code:

java

Main

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { int a = 10; // creating an int variable with value 10 int b = 17; // creating an int variable with value 17 int res = a + b; /* creating an int variable that will represent the sum of value a and value b */ System.out.println(res); // printing result to the console } }

As we can see, the variable res holds the value 27, which is the sum of 10 and 17.

Let's consider a few more examples:

java

Main

copy
12345678910111213141516171819
package com.example; public class Main { public static void main(String[] args) { int plus = 16 + 4; // creating an int variable with value of sum of 16 and 4 int minus = plus - 10; /* creating an int variable with value of subtraction of plus variable and 10 */ int multiplying = minus * 4; /* variable that holds a value of multiplying of minus variable and 4 */ int complexDivision = (multiplying - 4) / 9; /* using a subtraction and division operations on our "multiplying" variable */ System.out.println(complexDivision); //printing result to the console } }

We can use both numbers and variables in our operations. However, it's worth remembering that creating multiple variables adds to the stack memory. Therefore, it's recommended to use regular numbers when possible. Additionally, we can observe that operator precedence is followed. Operations inside parentheses are performed first, followed by multiplication or division, and then addition or subtraction.

In this way, we can perform simple operations with different numeric data.

Note

It's also worth mentioning integer division with remainder. By default, Java will round the result to the nearest integer value. We can address this issue by changing the variable to a floating-point data type.

Order of Operations

Java follows the basic principles of mathematics, and operations also have an order of execution. Let's take a look at an example:

java

main

copy
12345678
package com.example; public class Main { public static void main(String[] args) { int result = (10 + 5) * 2 - 8 / 4 + 1; System.out.println(result); } }

Here we arrived at the result by performing operations sequentially. Let's take a look at the order:

So, you can prioritize the execution of operations using regular parentheses, just like in arithmetic.

Now, let's move on to a simple exercise to reinforce the material.

Task
test

Swipe to show code editor

Try writing code that calculates the sum of the first and second numbers and then divides the result by the third number:

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt