Java Number & Math 类

[复制链接]

Java Number & Math 类

发表于 2021-12-28 13:37:13 只看大图 阅读模式 倒序浏览
542 0 查看全部
  一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte、int、long、double 等。
实例
  1. int a = 5000;
  2. float b = 13.65f;
  3. 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
OOP_WrapperClass.png
  这种由编译器特别支持的包装称为装箱,所以当内置数据类型被当作对象使用的时候,编译器会把内置类型装箱为包装类。相似的,编译器也可以把一个对象拆箱为内置类型。Number 类属于 java.lang 包。
  下面是一个使用 Integer 对象的实例:
Test.java 文件代码:
  1. public class Test{

  2.    public static void main(String[] args){
  3.       Integer x = 5;
  4.       x =  x + 10;
  5.       System.out.println(x);
  6.    }
  7. }
复制代码
  以上实例编译运行结果如下:

  1. 15
复制代码
  当 x 被赋为整型值时,由于x是一个对象,所以编译器要对x进行装箱。然后,为了使x能进行加运算,所以要对x进行拆箱。
Java Math 类  Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。
  Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用。
Test.java 文件代码:
  1. public class Test {  
  2.     public static void main (String []args)  
  3.     {  
  4.         System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));  
  5.         System.out.println("0度的余弦值:" + Math.cos(0));  
  6.         System.out.println("60度的正切值:" + Math.tan(Math.PI/3));  
  7.         System.out.println("1的反正切值: " + Math.atan(1));  
  8.         System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));  
  9.         System.out.println(Math.PI);  
  10.     }  
  11. }
复制代码
  以上实例编译运行结果如下:

  1. 90 度的正弦值:1.0
  2. 0度的余弦值:1.0
  3. 60度的正切值:1.7320508075688767
  4. 1的反正切值: 0.7853981633974483
  5. π/2的角度值:90.0
  6. 3.141592653589793
复制代码
Number & Math 类方法  下面的表中列出的是 Number & Math 类常用的一些方法:
序号方法与描述
1xxxValue()
将 Number 对象转换为xxx数据类型的值并返回。
2compareTo()
将number对象与参数比较。
3equals()
判断number对象是否与参数相等。
4valueOf()
返回一个 Number 对象指定的内置数据类型
5toString()
以字符串形式返回值。
6parseInt()
将字符串解析为int类型。
7abs()
返回参数的绝对值。
8ceil()
返回大于等于( >= )给定参数的的最小整数,类型为双精度浮点型。
9floor()
返回小于等于(‹=)给定参数的最大整数 。
10rint()
返回与参数最接近的整数。返回类型为double。
11round()
它表示四舍五入,算法为 Math.floor(x+0.5),即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。
12min()
返回两个参数中的最小值。
13max()
返回两个参数中的最大值。
14exp()
返回自然数底数e的参数次方。
15log()
返回参数的自然数底数的对数值。
16pow()
返回第一个参数的第二个参数次方。
17sqrt()
求参数的算术平方根。
18sin()
求指定double类型参数的正弦值。
19cos()
求指定double类型参数的余弦值。
20tan()
求指定double类型参数的正切值。
21asin()
求指定double类型参数的反正弦值。
22acos()
求指定double类型参数的反余弦值。
23atan()
求指定double类型参数的反正切值。
24atan2()
将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。
25toDegrees()
将参数转化为角度。
26toRadians()
将角度转换为弧度。
27random()
返回一个随机数。
Math 的 floor,round 和 ceil 方法实例比较
参数Math.floorMath.roundMath.ceil
1.4112
1.5122
1.6122
-1.4-2-1-1
-1.5-2-1-1
-1.6-2-2-1
floor,round 和 ceil 实例:
  1. public class Main {   
  2.   public static void main(String[] args) {   
  3.     double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };   
  4.     for (double num : nums) {   
  5.       test(num);   
  6.     }   
  7.   }   
  8.   
  9.   private static void test(double num) {   
  10.     System.out.println("Math.floor(" + num + ")=" + Math.floor(num));   
  11.     System.out.println("Math.round(" + num + ")=" + Math.round(num));   
  12.     System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));   
  13.   }   
  14. }
复制代码
  以上实例执行输出结果为:

  1. Math.floor(1.4)=1.0
  2. Math.round(1.4)=1
  3. Math.ceil(1.4)=2.0
  4. Math.floor(1.5)=1.0
  5. Math.round(1.5)=2
  6. Math.ceil(1.5)=2.0
  7. Math.floor(1.6)=1.0
  8. Math.round(1.6)=2
  9. Math.ceil(1.6)=2.0
  10. Math.floor(-1.4)=-2.0
  11. Math.round(-1.4)=-1
  12. Math.ceil(-1.4)=-1.0
  13. Math.floor(-1.5)=-2.0
  14. Math.round(-1.5)=-1
  15. Math.ceil(-1.5)=-1.0
  16. Math.floor(-1.6)=-2.0
  17. Math.round(-1.6)=-2
  18. Math.ceil(-1.6)=-1.0
复制代码

来源:菜鸟教程
回复

使用道具 举报

游客~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|极客同行 ( 蜀ICP备17009389号-1 )

© 2013-2016 Comsenz Inc. Powered by Discuz! X3.4