Java Number and Math Class

However, in the actual development process, we often encounter situations require the use of objects instead of the built-in data types. To solve this problem, Java language provides a corresponding wrapper class for each built-in data types.

All wrapper classes (Integer, Long, Byte, Double , Float, Short) is a subclass of the abstract class Number.

This is called by the compiler packing packaging special support, so when the built-in data types are used as an object, the compiler will built-in types of packaging containers. Similarly, the compiler is able to unpack an object is built-in type. Number category belongs java.lang package.

Below is an example using the object Integer:

public class Test{
 
   public static void main(String args[]){
      Integer x = 5;
      x =  x + 10;
      System.out.println(x); 
   }
}复制代码

The above examples compiled results are as follows:

15复制代码

When x is assigned an integer value, since x is an object, the compiler of x to be boxed. Then, in order to make x-add operation can be performed, so the x-be unpacking.

Java Math class

Java's Math contains properties and methods for performing basic mathematical operations, such as ever exponential, logarithmic, square root, and trigonometric functions.

Math methods are defined as static form, the main function can be called directly by Math class.

Test.java file code:

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);  
    }  
}以上实例编译运行结果如下:复制代码
90 度的正弦值:1.0
0度的余弦值:1.0
60度的正切值:1.7320508075688767
1的反正切值: 0.7853981633974483
π/2的角度值:90.0
3.141592653589793复制代码

Number & Math class methods

Number & Math class is commonly used methods are listed in the following table:

No. Method and Description
1 xxxValue ()
will be converted to a value xxx Number object data type and returns.
2 the compareTo ()
comparing the number with the object parameters.
3 equals ()
determining whether the object is equal to the number of parameters.
4 valueOf ()
Returns a specified Number object built-in data types
5 toString ()
Returns a string value.
6 the parseInt ()
parse a string int.
7 ABS ()
returns the absolute value of the parameters.
8 ceil ()
Returns greater than or equal to (> =) to a given parameter is the smallest integer, double precision floating point type.
9 floor ()
returns is less than or equal to (<=) to a maximum integer given parameter.
10 RINT ()
returns the nearest integer parameters. Return type is double.
11 round ()
which indicates rounding , algorithm Math.floor (x + 0.5), is about 0.5 and then adding the original digital rounding down, therefore, results Math.round (11.5) is 12, Math.round (- 11.5) results -11.
12 min ()
returns the minimum of the two parameters.
13 max ()
returns the maximum of the two parameters.
14 exp ()
Returns the natural log base e of power parameters.
15 log ()
to return the value of the parameters of the natural logarithm base.
16 POW ()
returns the first argument of the second argument power.
17 sqrt ()
request parameter square root.
18 sin ()
required to specify the value of the sine of double type.
19 cos ()
request type parameter specifies double cosine.
20 Tan ()
request specifies double tangent type parameter.
21 asin ()
request to specify the parameters of the type double arc sine value.
22 acos()
求指定double类型参数的反余弦值。
23 atan()
求指定double类型参数的反正切值。
24 atan2()
将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。
25 toDegrees()
将参数转化为角度。
26 toRadians()
将角度转换为弧度。
27 random()
返回一个随机数。

Math 的 floor,round 和 ceil 方法实例比较

参数 Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

floor,round 和 ceil 实例:

public class Main { public static void main(String[] args) { double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println("Math.floor(" + num + ")=" + Math.floor(num)); System.out.println("Math.round(" + num + ")=" + Math.round(num)); System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num)); } }

以上实例执行输出结果为:

Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0复制代码


转载于:https://juejin.im/post/5d02f2a0f265da1b7c611474

Guess you like

Origin blog.csdn.net/weixin_34235135/article/details/93162958