Home  >  Article  >  Java  >  [java tutorial] Java Number class

[java tutorial] Java Number class

黄舟
黄舟Original
2016-12-26 13:04:491258browse

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 i = 5000;
float gpa = 13.65;
byte mask = 0xaf;

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 wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

This kind of packaging specially supported by the compiler is called boxing, so when a built-in data type is used as an object, the compiler will box the built-in type into a packaging class . 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 boxing and unboxing:

public class Test{

   public static void main(String args[]){
      Integer x = 5; // boxes int to an Integer object
      x =  x + 10;   // unboxes the Integer to a int
      System.out.println(x); 
   }
}

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

15

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

Member methods of Number class

The following table lists the methods of Number class:

Serial number

Methods and descriptions

1 xxxValue()
Convert the number object to a value of xxx data type and return it.

2 compareTo()
Compare the number object with the parameter.

3 equals()
Determine whether the number object is equal to the parameter.

4 valueOf()
returns the built-in data type specified by an Integer object

5 toString()
returns the value in string form.

6 parseInt()
Parse the string into int type.

7 abs()
returns the absolute value of the parameter.

8 ceil()
rounds the integer variable to the left, and the return type is double.

9 floor()
rounds the integer variable to the right. The return type is double.

10 rint()
Returns the integer closest to the parameter. The return type is double.

11 round()
returns the nearest int or long value.

12 min()
returns the minimum value of the two parameters.

13 max()
returns the maximum value of the two parameters.

14 exp()
returns the parameter power of the natural number base e.

15 log()
returns the logarithm of the natural number base of the parameter.

16 pow()
returns the first parameter raised to the power of the second parameter.

17 sqrt()
Find the arithmetic square root of the parameter.

18 sin()
Find the sine value of the specified double type parameter.

19 cos()
Find the cosine value of the specified double type parameter.

20 tan()
Finds the tangent value of the specified double type parameter.

21 asin()
finds the arcsine value of the specified double type parameter.

22 acos()
Finds the inverse cosine value of the specified double type parameter.

23 atan()
finds the arctangent value of the specified double type parameter.

24 atan2()
Convert Cartesian coordinates to polar coordinates and return the angle value of the polar coordinates.

25 toDegrees()
Convert parameters into angles.

26 toRadians()
Convert angles to radians.

27 random()
returns a random number.

The above is the content of [java tutorial] Java Number class. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn