Java makes working with math straightforward and simple. It offers the usual arithmetic operators you expect, in addition to mathematical functions and properties so that we don't need to implement them ourselves. This lets us focus on the core logic of our application.

Let's dive into some math!

Arithmetic Operators

Arithmetic operators are what you use to manipulate the value of numerical variables and numbers. Here are the ones you can use:

Addition

JAVA
// adding numbers int cakes = 5; int cookies = 7; int total = cakes + cookies; System.out.println("Total: " + total);
BASH
Total: 12

Subtraction

JAVA
// subtracting numbers int cakes = 14; int eaten = 7; int total = cakes - eaten; System.out.println("Total: " + total);
BASH
Total: 7

Multiplication

JAVA
// multiplying numbers int cakes = 3; int people = 7; int total = cakes * people; System.out.println("Total: " + total);
BASH
Total: 21

Division

JAVA
// dividing numbers int cakes = 100; int people = 5; int total = cakes / people; System.out.println("Total: " + total);
BASH
Total: 20

Modulus

JAVA
// modulus operator int cakes = 100; int people = 7; int remainder = cakes / people; System.out.println("Remainder: " + remainder);
BASH
Total: 14

Order of Operations

Order of Operations, also known as PEMDAS, works just as you would expect, including parentheses:

JAVA
// order of operations int result = (5 * 6) + 40 / 2; System.out.println("Result: " + result);
BASH
Result: 50

Math Methods

Java offers many useful math methods for numbers that are commonly used in calculations.

Absolute Value

Get the absolute number of a number using the

JAVA
int number = -34; System.out.println("Number: " + Math.abs(number));
BASH
Number: 34

Floor

Use the floor method to round any number down to the nearest whole number:

JAVA
double number = 856.234; System.out.println("Number: " + Math.floor(number));
BASH
Number: 856.0

Ceiling

Use the ceil method to round any number up to the nearest whole number:

JAVA
double number = 856.234; System.out.println("Number: " + Math.ceil(number));
BASH
Number: 857.0

Natural Logarithm

You can get the natural logarithm of a number using the log() function:

JAVA
int number = 43; System.out.println("Number: " + Math.log(number));
BASH
Number: 3.7612001156935624

Base-10 Logarithm

You can get the base-10 logarithm of a number using the log10() function:

JAVA
int number = 43; System.out.println("Number: " + Math.log10(number));
BASH
Number: 1.6334684555795864

Maximum

Get the highest value of a set of numbers using the max() function:

JAVA
int number1 = 53; int number2 = 64; System.out.println("Number: " + Math.max(number1, number2));
BASH
Number: 64

Minimum

Get the lowest value of a set of numbers using the min() function:

JAVA
int number1 = 53; int number2 = 64; System.out.println("Number: " + Math.min(number1, number2));
BASH
Number: 53

Power

You can calculate the value of one number risen to the power of another number using the pow method:

JAVA
int base = 5; int exp = 4; System.out.println("Number: " + Math.pow(base, exp));
BASH
Number: 625.0

Random

You can get a random number between 0 and 1 using the random() function:

JAVA
double random = Math.random(); System.out.println("Random: " + random);
BASH
Random: 0.5497127671041443

You can then manipulate this value to do other things like getting a random number between two other numbers, like so:

JAVA
int min = 4; int max = 29; double result = (Math.random() * ((max - min) + 1)) + min; System.out.println("Result: " + result);
BASH
Result: 9.63955715156336

Round

You can round a number to the nearest integer with round():

JAVA
double value = 9.63955715156336; long result = Math.round(value); System.out.println("Result: " + result);
BASH
Result: 10

Square Root

You can take the square root of a number using the sqrt() function:

JAVA
int value = 245; double result = Math.sqrt(value); System.out.println("Result: " + result);
BASH
Result: 15.652475842498529

That's all we got for math! 🤓🤓🤓

Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.