[学习笔记】Java Numbers类与Math类

来源:互联网 发布:java iterator 将int 编辑:程序博客网 时间:2024/05/18 21:47

Numbers类

  • Number类是一个抽象类,包装类 (Integer, Long, Byte, Double, Float, Short) 是Number的子类。
  • 使用包装类后,我们就可以将原始数据类型的数据转变成对象,从而可以调用对应对象的方法。

包装类的装箱和拆箱

public class Test {    public static void main(String[] args) {        Double d = 1.0; // 装箱处理        System.out.println(d.equals(1.0)); // 调用对象方法equals        d += 2.15;  #自动拆箱        System.out.println(d);      }

类方法 parseXxx

  • 用来获取某个字符串的原始数据类型

    public class Test {    public static void main(String[] args) {        int x = Integer.valueOf("256");        System.out.println(x);        double d = Double.parseDouble("256");        System.err.println(d);    }}

Math类方法:

public class MathTest{    public static void main(String[] args)     {        /*---------下面是三角运算---------*/        //将弧度转换角度        System.out.println("Math.toDegrees(1.57):" + Math.toDegrees(1.57));         //将角度转换为弧度        System.out.println("Math.toRadians(90):"             + Math.toRadians(90));        //.....        /*---------下面是取整运算---------*/        //取整,返回小于目标数的最大整数。        System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2 ));         //取整,返回大于目标数的最小整数。        System.out.println("Math.ceil(1.2):" + Math.ceil(1.2));         //四舍五入取整        System.out.println("Math.round(2.3 ):" + Math.round(2.3 ));         /*---------下面是乘方、开方、指数运算---------*/        //计算平方根。        System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3 ));         //计算立方根。         System.out.println("Math.cbrt(9):" + Math.cbrt(9));         //返回欧拉数 e 的n次幂。        System.out.println("Math.exp(2):" + Math.exp(2));         //返回 sqrt(x2 ,y2),没有中间溢出或下溢。        System.out.println("Math.hypot(4 , 4):" + Math.hypot(4 , 4));        //计算乘方        System.out.println("Math.pow(3, 2):" + Math.pow(3, 2));        //计算自然对数,底为e        System.out.println("Math.log(12):" + Math.log(12));         //计算底数为 10 的对数。        System.out.println("Math.log10(9):" + Math.log10(9));         /*---------下面是符号相关的运算---------*/        //计算绝对值。        System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));        //符号赋值,返回带有第二个浮点数符号的第一个浮点参数。        /*---------下面是大小相关的运算---------*/        //找出最大值        System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3 , 4.5));        //计算最小值         System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2 , 3.4));        //返回一个伪随机数(double型),该值大于等于 0.0 且小于 1.0。        System.out.println("Math.random():" + Math.random());    }}
0 0
原创粉丝点击