The Math class

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

The Math class

Rating:

The Math class is one of the classes defined in the java.lang package. It extends the java.lang.Object class and is declared as final. It contains a set of static methods for performing common mathematical functions that include rounding numbers, finding the maximum or minimum of two numbers, finding square root of numbers, generating pseudo random numbers, performing various trigonometry operations, etc.

The Math class also defines two static fields, i.e., Math.E and Math.PI. These two fields are commonly used constants in mathematical operations. The Math.E field represents the value of e, which is the base of the natural logarithms. The Math.PI field represents the value of pi, which is the ratio of the circumference of the circle to its diameter.

As the Math class is declared as final, it cannot be subclassed. Due to a single private constructor that takes no argument, the Math class cannot be instantiated. However, it is never necessary to instantiate the Math class because all the methods and fields defined in the class are static and hence can be invoked simply by using the class name along with a dot operator.

Methods defined in the Math class

The following sections define some of the methods defined in the Math class:

  • abs() method

    The abs() method returns the absolute value of the argument. If the value supplied as an argument is negative, the negation of the value is returned. Otherwise, the same value is returned. The abs() method is overloaded with separate versions for the int, long, float, or double type argument.

    The signature of different versions of the abs() method in the Math class are given below:

    public static int abs(int num)

    This version of the method returns the absolute value of an int value.

    Special case: If the argument to the method is equal to the value of Integer.MIN_VALUE (which represents the most negative int value), the same value is returned by the method.

    public static long abs(long num)

    This version of the method returns the absolute value of a long value.

    Special case: If the argument to the method is equal to the value of Long.MIN_VALUE (which represents the most negative long value), the same value is returned by the method.

    public static float abs(float num)

    This version of the method returns the absolute value of a float value.

    public static double abs(double num)

    This version of the method returns the absolute value of a double value.


  • ceil() method

    The ceil() method returns the smallest double value that is not less than the argument and is equal to a mathematical integer. The signature of the method in the Math class is given below:

    public static double ceil(double a)

    Special cases: The value returned by the ceil() method is the same as the argument in the following situations:

    • The value supplied as the argument is already equal to an integer.

    • The value supplied as the argument is NaN.

    • The value supplied as the argument is an infinity.

    • The value supplied as the argument is a positive zero or a negative zero.

    The ceil() method returns a negative zero if the value supplied as the argument is less than zero but greater than -1.0.

    Note: The value of Math.ceil(x) is equal to the value of -Math.floor(-x).


  • floor() method

    The floor() method returns the largest double value, which is not greater than the argument and is equal to an integer.

    public static double floor(double a)

    Special cases: The value returned by the floor() method is the same as argument in the following situations:

    • If the argument value is already equal to an integer.

    • If the argument is NaN.

    • If the argument is infinity.

    • If the argument is a positive zero or negative zero.

  • max() method

    The max() methods returns the greater of the two values that are supplied as arguments to the method. The max() method is overloaded with separate versions for the int, long, float, or double type arguments.

    The signature of different versions of the max() method are given below:

    • public static int max(int a, int b)

    • public static long max(long a, long b)

    • public static float max(float a, float b)

    • public static double max(double a, double b)

  • min() method

    The min() method returns the smaller of the two values that are supplied as arguments to the method. The min() method is overloaded with separate versions for the int, long, float, or double type arguments.

    The signature of different versions of the min() method are given below:

    • public static int min(int a, int b)

    • public static long min(long a, long b)

    • public static float min(float a, float b)

    • public static double min(double a, double b)

  • random() method

    The random() method returns a randomly generated double value. The value generated by the random() method is either greater than or equal to 0.0 and less than 1.0. This can be mathematically shown as follows:

    0.0 <= Math.random() < 1.0

    The signature of the method is given below:

    public static double random()

    Note: The random method of the Math class is the only method that takes no argument.


  • round() method

    The round() method returns an integer that is closest to the floating-point value supplied as the argument. The round() method is overloaded with separate versions for the float and double arguments. The signatures of the two versions of the round() method are given below:

    • public static int round(float num)

      This version of the round() method returns the closest int value to the argument.

      The value returned by the round method for the argument x can be determined by first adding the float value 0.5f to x, then taking the floor of the result, and finally casting the result to int type. In other words, the value returned by the round() method is equal to the value of the following expression:

      (int) Math.floor(x + 0.5f)

      Special cases:

      • The method returns the value 0 if the argument is NaN.

      • The method returns the value equal to Integer.MIN_VALUE if the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE.

      • The method returns the value equal to Integer.MAX_VALUE if the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE.

    • public static long round(double num)

      This version of the round() method returns the closest long value to the argument.

      The value returned by the round method for the argument x (say) can be determined by first adding the double value 0.5d to x, then taking the floor of the result, and finally casting the result to long type. In other words, the value returned by the round() method is equal to the value of the following expression:

      (long) Math.floor(x + 0.5d)

      Special cases:

      • The method returns the value 0 if the argument is NaN.

      • The method returns the value equal to Long.MIN_VALUE if the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE.

      • The method returns the value equal to Long.MAX_VALUE if the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE.

    The value returned by the round() method is the same as the value returned by the Math.floor() method in the following situations:

    • If the argument to the method is a positive number and the fractional part of the number is less than 0.5.

    • If the argument to the method is a negative number and the fractional part of the number is greater than 0.5.

    The value returned by the round() method is the same as the value returned by the Math.ceil() method in the following situations:

    • If the argument to the method is a positive number and the fractional part of the number is greater than or equal to 0.5.

    • If the argument to the method is a negative number and the fractional part of the number is less than or equal to 0.5.

  • sin() method

    The sin() method returns a primitive type double value, which represents the trigonometric sine of the angle supplied as an argument. The argument to the sin() method is a primitive type double value, which represents the angle in radians. The signature of the sin() method is given below:

    public static double sin(double num)

    where, num represents the angle in radians.


  • cos() method

    The cos() method returns a primitive type double value, which represents the trigonometric cosine of the angle supplied as an argument. The argument to the cos() method is a primitive type double value, which represents the angle in radians. The signature of the cos() method is given below:

    public static double cos(double num)

    where, num represents the angle in radians.


  • tan() method

    The tan() method returns a primitive type double value, which represents the trigonometric tangent of the angle supplied as an argument. The argument to the tan() method is a primitive type double value, which represents the angle in radians. The signature of the tan() method is given below:

    public static double tan(double num)

    where, num represents the angle in radians.


  • sqrt() method

    The sqrt() method takes a primitive type double value as an argument and returns the positive square root of the value. The value returned by the method is a floating-point value whose data type is double. The signature of the sqrt() method is given below:

    public static double sqrt(double a)

    Special cases:

    • If the value used as an argument to call the method is NaN or a value less than zero, the method returns NaN.
    • If the value used as an argument to call the method is positive infinity, then the method returns positive infinity.


Rating:



Other articles

Click here to Article home

MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD, SCJA, SCBCD, SCMAD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.
 
HACKER SAFE certified sites prevent over 99.9% of hacker crime.