Java Number & Math ç±»

一般地,当需�使用数字的时候,我们通常使用内置数�类型,如:byte�int�long�double 等。

实例

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

然而,在实际开�过程中,我们�常会�到需�使用对象,而�是内置数�类型的情形。为了解决这个问题,Java 语言为�一个内置数�类型�供了对应的包装类。

所有的包装类(Integer�Long�Byte�Double�Float�Short)都是抽象类 Number 的�类。

包装类 基本数�类型
Boolean boolean
Byte byte
Short short
Integer int
Long long
Character char
Float float
Double double

Java Numberç±»

这�由编译器特别支�的包装称为装箱,所以当内置数�类型被当作对象使用的时候,编译器会把内置类型装箱为包装类。相似的,编译器也�以把一个对象拆箱为内置类型。Number 类属于 java.lang 包。

下�是一个使用 Integer 对象的实例:

Test.java 文件代�:

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

以上实例编译�行结果如下:

15

当 x 被赋为整型值时,由于x是一个对象,所以编译器�对x进行装箱。然�,为了使x能进行加�算,所以�对x进行拆箱。


Java Math ç±»

Java 的 Math 包�了用于执行基本数学�算的属性和方法,如�等指数�对数�平方根和三角函数。

Math 的方法都被定义为 static 形�,通过 Math 类�以在主函数中直接调用。

Test.java 文件代�:

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 类方法

下�的表中列出的是 Number & Math 类常用的一些方法:

�� 方法与�述
1 xxxValue()
将 Number 对象转�为xxx数�类型的值并返回。
2 compareTo()
将number对象与�数比较。
3 equals()
判断number对象是�与�数相等。
4 valueOf()
返回一个 Number 对象指定的内置数�类型
5 toString()
以字符串形�返回值。
6 parseInt()
将字符串解�为int类型。
7 abs()
返回�数的�对值。
8 ceil()
返回大于等于( >= )给定�数的的最�整数,类型为�精度浮点型。
9 floor()
返回�于等于(<=)给定�数的最大整数 。
10 rint()
返回与�数最接近的整数。返回类型为double。
11 round()
它表示四�五入,算法为 Math.floor(x+0.5),�将原�的数字加上 0.5 ���下�整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。
12 min()
返回两个�数中的最�值。
13 max()
返回两个�数中的最大值。
14 exp()
返回自然数底数e的�数次方。
15 log()
返回�数的自然数底数的对数值。
16 pow()
返回第一个�数的第二个�数次方。
17 sqrt()
求�数的算术平方根。
18 sin()
求指定double类型�数的正弦值。
19 cos()
求指定double类型�数的余弦值。
20 tan()
求指定double类型�数的正切值。
21 asin()
求指定double类型�数的�正弦值。
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