Java math FAQ: How do I square a number in Java?
You can square a number in Java in at least two different ways:
- Multiply the number by itself.
- Call the Math.pow function.
Square a number by multiplying it by itself
Here’s how to square a number by multiplying it by itself:
i = 2 int square = i * i
In that case, if you print the value of
square, it will be 4.