Operators in Java are special symbols that perform operations on variables and values. They are the building blocks that let you write logic, perform calculations, and control program flow. Without operators, Java would just be about storing data—you couldn’t actually do anything with it. So by learning how to use operators, you are taking a step further in mastering Java.
Arithmetic Operators
Now at this point you are going to be guessing the usual suspects when it comes to arithmetic and you are definitely right. Java arithmetic operators, just like most other languages, are +
, -
, *
and /
and this give us the ability to manipulate, reassign and work with data that may be stored in variables or even constants( no reassignments for constants).
You already know what they do, and I'm not about to spend your time telling you that the +
sign in Java is for addition and the -
sign is for subtraction. But I will do this, I will take you through some must knows about working with these operators.
Division does not always give you what you expect:
Hate to tell you, but division in programming always has something fishy to it and this is probably because of the possibility of dividing a number by 0 and other things that are a bit impossible for maths as we know it now.
First of all the difference comes in the data type of the output. Dividing an integer by an integer will give an interger as an output.However, dividing a float by an integer will give you a float😅, please bare with me don't freak out just yet.
Let's establish the base rules here:
- When you have an integer dividing another integer you are going to get an integer as an output. For example if you divide 15 by 2 you'd expect to get 7.5 however because they are both integers the fractional part will be trancated leaving you with 7 Remember that it does not round off the number.
To be able to access the true value of the division, you'd have to make either of them either a float or a double. So in the first case, you'd either make 15 or 7 a decimalnumber by adding .0
There are something things you cannot call on object in classes:
Yes you heard me right, you can't call everything on an object in programming. For any beginner who is following my series you might not yet know what an object is unless you've had a bit of experience with OOP in any other language. Don't worry this would be covered later in the series.
You see, in OOP to use a method of a class you'd have to create an instance of the class however, for the Math module in Java this is not required. This is because most of its methods are static (if not all) hence they are independent of the state of the object and can be called without them.
Safer Remainders with floorMod
The % operator is the remainder operator (sometimes called modulus).
It divides one number by another, then gives you what’s left over after the division.
For Example;
System.out.println(10 % 3); // 1
Here’s something that can trip you up: the % operator in Java doesn’t always behave the way you might expect.
If the number (the dividend) is negative, % can return a negative result. For example:
System.out.println(-5 % 3); // -2 😬
That’s not always what we want. Imagine you’re trying to calculate the position of a clock hand. You’d probably expect your values to stay between 0 and 11. But with %, you might suddenly get a -2 and your clock hand is pointing into another universe.
Enter Math.floorMod().
This handy method makes sure the remainder is always non-negative (as long as your divisor is positive).
System.out.println(Math.floorMod(-5, 3)); // 1
Java Can Be Very sneaky at times
In Java, your math might look right but secretly betray you. For example:
int result = 1_000_000_000 * 3;
System.out.println(result); // output is -1294967296
Wait, what? Negative?? 😳
Here’s the catch: Java’s int can only hold numbers up to about 2.1 billion. Anything bigger doesn’t throw a warning , it just wraps around and gives you nonsense. That’s called integer overflow.
Now, Java’s Math class comes to the rescue with methods like:
Math.multiplyExact()
Math.addExact()
Math.subtractExact()
and even Math.incrementExact()
Instead of lying to you with a bogus number, these methods throw an exception if the result can’t fit inside an int or long.
So the trade-off is simple:
Use normal operators (*, +, -) if you don’t care about overflow.
Use the Exact methods if you want Java to scream at you before you accidentally ship a bug.
Who wins when different data types collide??
In Java, operators don’t just blindly crunch numbers. When you combine values of different data types with a binary operator (like +, -, *, /), Java first makes sure they’re speaking the same “language.”
But here’s the kicker: not all data types are equal. Some have more weight than others, and Java promotes the “lighter” one to match the “heavier” one. Think of it like picking the bigger box when you want to pack two things together — both items must fit.
Here’s the hierarchy Java follows:
If either value is a double → the other becomes a double.
int n = 5;
double f = 2.5;
System.out.println(n + f); // 7.5 (int promoted to double)
Else, if either value is a float → the other becomes a float.
long big = 100L;
float num = 3.2f;
System.out.println(big + num); // 103.2 (long promoted to float)
Else, if either value is a long → the other becomes a long.
int small = 10;
long bigger = 20L;
System.out.println(small + bigger); // 30 (int promoted to long)
Else, both are converted to int.
byte a = 4;
short b = 6;
System.out.println(a + b); // 10 (both promoted to int)
If either value is a double
→ the other becomes a double
.
Extras
Floating-Point Portability in Java
One of Java’s big promises is portability, your code should run the same everywhere. But floating-point math made this tricky. Some processors use extra precision (80-bit) in calculations, while Java’s double is strictly 64-bit.
Originally: Java forced all math to 64 bits → consistent but slower.
Later: The strictfp
keyword was added. By default, JVMs could use faster extended precision, but strictfp
forced reproducible 64-bit results when needed.
Now (Java 17+): Modern CPUs handle 64-bit efficiently, so the JVM requires strict 64-bit by default again.
👉 Takeaway: Thanks to strictfp
and modern JVM rules, Java floating-point arithmetic is both portable and reliable today.
Top comments (0)