How to square a number in Java?

To square a number in Java do this: Math.pow(number, 2).

So, if your number is 5, then you can square it like so:

int myNumber = 5;
double square = Math.pow(myNumber, 2); // 25.0

Another way

Another way is to multiply the number by itself using the * operator:

int myNumber = 5;
double square = myNumber * myNumber; // 25.0