Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java Number class


May 10, 2021 Java


Table of contents


Java Number class

In general, we use the basic data types of the data: byte, int, short, long, double, float, boolean, char;

There are also eight types of packaging: Byte, Integer, Short, Long, Double, Float, Character, Boolean;

The wrapper types are declared in final and can not be inherited and override; in practice, the compiler automatically boxes the base data type into an object type, or unboxs the object type into a base data type;

public static void main(String[] args) {
	int num1 = 1;
	//将基本数据类型装箱成对象包装类型
	Integer num2 = num1;
	Integer num3 = 3;
	//将对象数据类拆箱
	int num4 = num3;
}

The Number class is an abstract class under the java.lang package that provides a way to unbox the wrapper type into a basic type, and all the wrapper types of the base type (data type) inherit the abstract class, and the final declaration is not inheritable;

package java.lang;

public abstract class Number implements java.io.Serializable {

    public abstract int intValue();

    public abstract long longValue();

    public abstract float floatValue();

    public abstract double doubleValue();

    public byte byteValue() {
        return (byte)intValue();
    }

    public short shortValue() {
        return (short)intValue();
    }

    private static final long serialVersionUID = -8742448824652078965L;
}
The packaging class The basic data type
Boolean boolean
Byte byte
Short short
Integer Int
Long long
Character char
Float float
Double double

Java Number class

This wrapper, which is especially supported by the compiler, is called boxing, so when the built-in data type is used as an object, the compiler boxes the built-in type as a wrapper class. S imilarly, the compiler can unbox an object as a built-in type. The Number class belongs to the java.lang package.

Here 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 above examples compile and run as follows:

15

When x is assigned an integer value, the compiler boxes x because x is an object. Then, in order for x to add, unbox x.


Java Math class

The Math of Java contains properties and methods for performing basic mathematical operations, such as primary exponents, logs, square roots, and triangular functions.

Math's methods are defined as static, and the Math class can be called directly in the main function.

Instance

public class Test {  
    public static void main (String []args)  
    {  
        System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));  
        System.out.println("0度的余弦值:" + Math.cos(0));  
        System.out.println("60度的正切值:" + Math.tan(Math.PI/3));  
        System.out.println("1的反正切值: " + Math.atan(1));  
        System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));  
        System.out.println(Math.PI);  
    }  
}

The above examples compile and run as follows:

90 度的正弦值:1.0
0度的余弦值:1.0
60度的正切值:1.7320508075688767
1的反正切值: 0.7853981633974483
π/2的角度值:90.0
3.141592653589793


The Number and Math class method

The following table lists common methods for Number and Math classes:

Serial number Method and description
1 xxxValue()
Convert the number object to a value of the xxx data type and return it.
2 compareTo()
Compare the number object with the argument.
3 equals()
Determines whether the number object is equal to the parameters.
4 valueOf()
Returns the built-in data type specified by an Integer object
5 toString()
Returns a value as a string.
6 parseInt()
Resolve the string to the int type.
7 abs()
Returns the absolute value of the argument.
8 ceil()
Returns the smallest integer, of double-precision floating-point type, that is greater than or equal to the given parameter.
9 floor()
Returns the maximum integer that is less than or equal to a given parameter.
10 rint()
Returns the integer closest to the argument. The return type is double.
11 round()
Returns the closest int, long value.
12 min()
Returns the minimum of the two parameters.
13 max()
Returns the maximum of the two parameters.
14 exp()
Returns the parameter side of the natural number bottom e.
15 log()
Returns a pair of values for the natural base of the argument.
16 pow()
Returns the second argument side of the first argument.
17 sqrt()
Find the arithmetic square root of the argument.
18 sin()
Find the sine value that specifies the double type parameter.
19 cos()
Find the cosine value that specifies the double type parameter.
20 tan()
Find the positive cut value of the double type parameter.
21 asin()
Find the antisine value of the double type parameter.
22 acos()
Find the anti-cosine value of the double type parameter.
23 atan()
Find the reverse cut value of the double type parameter.
24 atan2()
Converts Descartes coordinates to polar coordinates and returns the angle values of polar coordinates.
25 toDegrees()
Turn parameters into angles.
26 toRadians()
Converts the angle to radian.
27 random()
Returns a random number.