Java

Square Root in Java

Consider the following math equation:

5 x 5 = 25

Since the two numbers on the left of the equal sign are the same, we say 25 is the square of 5, and 5 is the square root of 25, written respectively as,

25 = 52

and

5 =

Consider another math equation, thus:

4 x 4 = 16

Since the two numbers on the left of the equal sign are the same, we say 16 is the square of 4, and 4 is the square root of 16, written respectively as,

16 = 42

and

4 =

The same thing can be said for 3 and 9, and 6 and 36, and other pairs of numbers. In many cases, both numbers are of the double type. This article focuses on a square root. This article explains how to use Java to find the square root of a number, similar to how the calculator would produce the square root of a number.

Syntax

Java has a method to return the square root of a number: it is sqrt(). It is a member of the Math Class. The full syntax is:

public static double sqrt(double a)

It is public. This means that it can be called from outside the class implementation. It is static. This means that the Math class does not need to be instantiated for the method to be used. To use the method, begin with the name of the math class, Math, followed by a dot, and then the method. Do not forget to put in the argument, which is the number whose square root is required. The argument is the double type, and the return value is the double type. This means that the return type has to be declared as double. The Math class does not have to be imported by the programmer before being used.

Why double Argument Type and double return Type

The argument is the double type, and the return value is the double type. Java has the following numeric types: byte, int, long, float, and double. There are other sub-int types, which this article will not address. The difference between the long type and the int type is that the long type variable can hold numbers larger than the int type limit and hold numbers within the range of the int type. Both the float and the double type numbers are not exact. The difference between the float and the double is that the double number is closer to the number it represents than the float number.

Square Root of int Number

The following program, outputs the square root of the integer, 10:

    public class TheClass {
        public static void main(String[] args) {
            double ret = Math.sqrt(10);
            System.out.println(ret);
        }
    }

Note that the return type is a coded as double. The output is:

3.1622776601683795

The decimal places are too long. When not told, round your decimal places to two decimal digits. The following program, illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            double ret1 = Math.sqrt(10);
            double ret2 = Math.round(ret1*100) / 100.0;
            System.out.println(ret2);
        }
    }

The second statement in main() does the rounding. The output is 3.16.

The output must not always be rounded by code. If the integer whose square root was required was 25, the return value of the sqrt() method would have been 5.0. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            double ret = Math.sqrt(25);
            System.out.println(ret);
        }
    }

The rounding statement has been omitted. The output is 5.0. The “.0” is there because the return value is a double.

Square Root of float Number

In Java, a float number should end with ‘f’.

The following program, outputs the square root of the float, 11.22:

    public class TheClass {
        public static void main(String[] args) {
            double ret = Math.sqrt(11.22f);
            System.out.println(ret);
        }
    }

Note that the return type is coded as a double. The output is:

3.349626884748331

The decimal places are too long. When not told, round your decimal places to two decimal digits. The following program, illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            double ret1 = Math.sqrt(11.22f);
            double ret2 = Math.round(ret1*100) / 100.0;
            System.out.println(ret2);
        }
    }

The second statement in main() does the rounding. The output is 3.35.

The output must not always be rounded by code. If the integer whose square root was required was 16.0, the return value of the sqrt() method would have been 4.0. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            double ret = Math.sqrt(16.0f);
            System.out.println(ret);
        }
    }

The rounding statement has been omitted. The output is 4.0. The “.0” is there because the return value is a double.

Square Root of double Number

The following program, outputs the square root of the double, 11.22:

    public class TheClass {
        public static void main(String[] args) {
            double ret = Math.sqrt(11.22);
            System.out.println(ret);
        }
    }

Note that the return type is coded as a double. The output is:

3.3496268448888453


The decimal places are too long. When not told, round your decimal places to two decimal digits. The following program, illustrates this:

[cc lang="java"  width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
    public class TheClass {
        public static void main(String[] args) {
            double ret1 = Math.sqrt(11.22);
            double ret2 = Math.round(ret1*100) / 100.0;
            System.out.println(ret2);
        }
    }

The second statement in main() does the rounding. The output is 3.35.

The output must not always be rounded by code. If the integer whose square root was required was 16.0, the return value of the sqrt() method would have been 4.0. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            double ret = Math.sqrt(16.0);
            System.out.println(ret);
        }
    }

The rounding statement has been omitted. The output is 4.0. The “.0” is there because the return value is a double.

Conclusion

Java has the method, sqrt() for square root. It takes a number as argument and returns the square root as a double type. If the number of decimal places is high, then the number has to be rounded. How to round a number to two decimal places has been illustrated above.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.