Java Number class


Generally, when we need to use numbers, we usually use built-in data types, such as: byte, int, long, double, etc.

Example

int a = 5000;
float b = 13.65;
byte c = 0x4a;

However, in the actual development process, we often encounter situations where we need to use objects instead of built-in data types. In order to solve this problem, the Java language provides corresponding wrapper classes for each built-in data type.

All packaging classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

number1.png

This kind of packaging specially supported by the compiler is called boxing, so when the built-in data type is used as an object, the compiler will box the built-in type For packaging type. Similarly, the compiler can unbox an object into a built-in type. Number class belongs to java.lang package.

The following is an example of using an Integer object:

public class Test{

   public static void main(String args[]){
      Integer x = 5;
      x =  x + 10;
      System.out.println(x); 
   }
}

The compilation and running results of the above example are as follows:

15

When x is assigned an integer value, since x is an object, so the compiler has to box x. Then, in order for x to be added, x is unboxed.


Number method

The following table lists the methods implemented by Number subclasses:

              Serial number             Methods and description
            1xxxValue()
Convert the number object to a value of xxx data type and return it.
              2compareTo()
Compares the number object with the parameter.
              3equals()
Determine whether the number object is equal to the parameter.
              4valueOf()
Returns a Number object of the specified built-in data type
              5toString()
Returns the value as a string.
              6parseInt()
Parse the string into type int.
              7abs()
Returns the absolute value of the argument.
              8ceil()
Round the integer variable to the left, and the return type is double.
              9floor()
Rounds integer variables to the right. The return type is double type.
              10rint()
Returns the nearest integer to the argument. The return type is double.
              11round()
Returns the nearest int or long value.
              12min()
Returns the minimum value of the two parameters.
              13max()
Returns the maximum value of the two parameters.
              14exp()
Returns the natural number base e raised to the parameter power.
              15log()
Returns the logarithm of the natural base of the argument.
              16pow()
Returns the first parameter raised to the power of the second parameter.
              17sqrt()
Find the arithmetic square root of the parameter.
              18sin()
Find the sine value of the specified double type parameter.
              19cos()
Find the cosine value of the specified double type parameter.
              20tan()
Find the tangent value of the specified double type parameter.
              21asin()
Find the arcsine value of the specified double type parameter.
              22acos()
Find the arc cosine value of the specified double type parameter.
              23atan()
Find the arctangent value of the specified double type parameter.
              24atan2()
Converts Cartesian coordinates to polar coordinates and returns the angle value of the polar coordinates.
              25toDegrees()
Convert parameters to angles.
              26toRadians()
Convert angles to radians.
              27random()
Returns a random number.