Table of Contents
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);
BASHTotal: 12
Subtraction
JAVA// subtracting numbers
int cakes = 14;
int eaten = 7;
int total = cakes - eaten;
System.out.println("Total: " + total);
BASHTotal: 7
Multiplication
JAVA// multiplying numbers
int cakes = 3;
int people = 7;
int total = cakes * people;
System.out.println("Total: " + total);
BASHTotal: 21
Division
JAVA// dividing numbers
int cakes = 100;
int people = 5;
int total = cakes / people;
System.out.println("Total: " + total);
BASHTotal: 20
Modulus
JAVA// modulus operator
int cakes = 100;
int people = 7;
int remainder = cakes / people;
System.out.println("Remainder: " + remainder);
BASHTotal: 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);
BASHResult: 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
JAVAint number = -34;
System.out.println("Number: " + Math.abs(number));
BASHNumber: 34
Floor
Use the floor
method to round any number down to the nearest whole number:
JAVAdouble number = 856.234;
System.out.println("Number: " + Math.floor(number));
BASHNumber: 856.0
Ceiling
Use the ceil
method to round any number up to the nearest whole number:
JAVAdouble number = 856.234;
System.out.println("Number: " + Math.ceil(number));
BASHNumber: 857.0
Natural Logarithm
You can get the natural logarithm of a number using the log()
function:
JAVAint number = 43;
System.out.println("Number: " + Math.log(number));
BASHNumber: 3.7612001156935624
Base-10 Logarithm
You can get the base-10 logarithm of a number using the log10()
function:
JAVAint number = 43;
System.out.println("Number: " + Math.log10(number));
BASHNumber: 1.6334684555795864
Maximum
Get the highest value of a set of numbers using the max()
function:
JAVAint number1 = 53;
int number2 = 64;
System.out.println("Number: " + Math.max(number1, number2));
BASHNumber: 64
Minimum
Get the lowest value of a set of numbers using the min()
function:
JAVAint number1 = 53;
int number2 = 64;
System.out.println("Number: " + Math.min(number1, number2));
BASHNumber: 53
Power
You can calculate the value of one number risen to the power of another number using the pow
method:
JAVAint base = 5;
int exp = 4;
System.out.println("Number: " + Math.pow(base, exp));
BASHNumber: 625.0
Random
You can get a random number between 0
and 1
using the random()
function:
JAVAdouble random = Math.random();
System.out.println("Random: " + random);
BASHRandom: 0.5497127671041443
You can then manipulate this value to do other things like getting a random number between two other numbers, like so:
JAVAint min = 4;
int max = 29;
double result = (Math.random() * ((max - min) + 1)) + min;
System.out.println("Result: " + result);
BASHResult: 9.63955715156336
Round
You can round a number to the nearest integer with round()
:
JAVAdouble value = 9.63955715156336;
long result = Math.round(value);
System.out.println("Result: " + result);
BASHResult: 10
Square Root
You can take the square root of a number using the sqrt()
function:
JAVAint value = 245;
double result = Math.sqrt(value);
System.out.println("Result: " + result);
BASHResult: 15.652475842498529
That's all we got for math! ๐ค๐ค๐ค
- How to Install Node on Windows, macOS and Linux
- Getting Started with Solid
- How to Set Up Cron Jobs in Linux
- How to deploy a Deno app using Docker
- How to deploy a MySQL Server using Docker
- How to deploy an Express app using Docker
- Using Puppeteer and Jest for End-to-End Testing
- Getting User Location using JavaScript's Geolocation API
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up a Local Web Server using Node.js
- Using Axios to Pull Data from a REST API