StackTips

Perform Math Operations in Java

stacktips avtar

Written by

Editorial,  10 min read,  - views, updated on Dec. 21, 2023

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.

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

// adding numbers
int cakes = 5;
int cookies = 7;

int total = cakes + cookies;
System.out.println("Total: " + total);
Total: 12

Subtraction

// subtracting numbers
int cakes = 14;
int eaten = 7;

int total = cakes - eaten;
System.out.println("Total: " + total);
Total: 7

Multiplication

// multiplying numbers
int cakes = 3;
int people = 7;

int total = cakes * people;
System.out.println("Total: " + total);
Total: 21

Division

// dividing numbers
int cakes = 100;
int people = 5;

int total = cakes / people;
System.out.println("Total: " + total);
Total: 20

Modulus

// modulus operator
int cakes = 100;
int people = 7;

int remainder = cakes / people;
System.out.println("Remainder: " + remainder);
Total: 14

Order of Operations

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

// order of operations
int result = (5 * 6) + 40 / 2;

System.out.println("Result: " + result);
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

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

Floor

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

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

Ceiling

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

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

Natural Logarithm

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

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

Base-10 Logarithm

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

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

Maximum

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

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

Minimum

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

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

Power

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

int base = 5;
int exp = 4;

System.out.println("Number: " + Math.pow(base, exp));
Number: 625.0

Random

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

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

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

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

Round

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

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

Square Root

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

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

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE

Continue reading..