Day18 常用Api、异常机制

Random(随机数)

从0开始

 

 number

DecimalFormat

 BigInteger&BigDecimal

java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。

abs   绝对值   acos,asin,atan,cos,sin,tan 三角函数  sqrt 平方根

pow(double a,doble b)    a的b次幂  log   自然对数

exp  e为底指数

max(double a,double b)

min(double a,double b)

random() 返回0.0到1.0的随机数

long round(double a)      double型数据a转换为long型(四舍五入)

toDegrees(double angrad) 弧度—>角度

toRadians(double angdeg) 角度—>弧度

BigInteger和BigDecimal类

BigInteger

1、Integer类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的, 最大为263-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。

2、java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供

所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。

3、构造器

BigInteger(String val):根据字符串构建BigInteger对象

4、常用方法

public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger。

BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger

BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger

BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger

BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数 相除只保留整数部分。

BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。

BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。

BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

BigDecimal

一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中, 要求数字精度比较高,故用到java.math.BigDecimal类。

BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

构造器

public BigDecimal(double val)

public BigDecimal(String val)

常用方法

public BigDecimal add(BigDecimal augend)

public BigDecimal subtract(BigDecimal subtrahend)

public BigDecimal multiply(BigDecimal multiplicand)

public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)

//创建对象的时候,需要传递字符串的数字
        BigInteger b1 = new BigInteger("222");
        BigDecimal b2 = new BigDecimal(200);
        BigDecimal b3 = new BigDecimal(200);
        // +
        BigDecimal b4 = b2.add(b3);
        System.out.println(b4);//400
        // -
        b4= b2.subtract(b3);
        System.out.println(b4);//0
        // *
        b4 = b2.multiply(b3);
        System.out.println(b4);//40000
        // /
        b4 = b2.divide(b3);
        System.out.println(b4);//1
        // %
        b4 = b2.remainder(b3);
        System.out.println(b4);//0
阶乘
        import java.math.BigDecimal;
        public class Test{
            public static void main(String[] args){
                System.out.println(m1(5));  //120
            }
            public static BigDecimal m1(int n){
                if(n < 1){
                    return new BigDecimal(1);
                }
                return new BigDecimal(n).multiply(m1(n-1));
            }
        }
Math
     概述
        Math 提供了科学计算和基本的数字操作方法。
        abs    绝对值 acos,asin,atan,cos,sin,tan    三角函数  sqrt        平方根
        pow(double a,doble b)    a的b次幂  log    自然对数
        exp    e为底指数
        max(double a,double b)
        min(double a,double b)
        random()    返回0.0到1.0的随机数
        long round(double a)    double型数据a转换为long型(四舍五入)
        toDegrees(double angrad) 弧度—>角度
        toRadians(double angdeg) 角度—>弧度
    常用方法
        // abs 绝对值
            System.out.println(Math.abs(-56));    // 56
        // ceil 向上取整数,小数大于0 就 + 进位
            System.out.println(Math.ceil(1.003));    // 2.0
        // floor 向下取整数,舍弃小数
            System.out.println(Math.floor(1.235));    // 1.0
        // sqrt 平方根
            System.out.println(Math.sqrt(9));    // 3.0
        // cbrt 立方根
            System.out.println(Math.cbrt(8));    // 2.0
        // random 随机数 生成大于等于0.0且小于1.0
            System.out.println(Math.random());    
            int i  = (int) (Math.random()*100);
            System.out.println(i);
        // rint 四舍五入  银行算法:四舍六入无取双 取偶数
            System.out.println(Math.rint(1.5));    // 2
            System.out.println(Math.rint(2.5));    // 2

1 异常机制
    1.1 之前见过的异常机制
        下标越界
        空指针异常
        栈内存溢出
        类型转换异常
        数字格式化异常
    1.2概述
        异常:
            是java提供的识别错误的相应机制,可以使程序更加健壮,易于调试。
            异常是错误的另外一种说法。
            java中,有个专门模拟所有异常的类,Throwable是异常类的祖类,所以的异常类,都是它的子类。
            如果程序,出现了异常,会终止程序生命周期执行,所以需要进行处理。
    1.3 异常处理形式
        try catch :    解决异常,一般用于客户端。
        throws : 抛异常,一般用在类库端
        throw : 异常源,制作异常,创建异常对象。
    1.4 Error
        1.4.1 概念
            系统内部错误,这类错误由系统进行处理,程序本身无需捕获处理。
        1.4.2 示例
            堆溢出错误
    1.5 Exception
        1.5.1概述
            Exception 是所有异常类的父类, 分为非 RuntimeException 和 RuntimeException
        非 RuntimeException
            指程序编译时需要捕获或处理的异常,如 IOException 、 自定义异常等。属于 checked 异常。
        RuntimeException
            指程序编译时不需要捕获或处理的异常。如:NullPointException等。属于unchecked 异常,一般是由程序员粗心导致的,如空指针异常、数组越界、类型转换异常等。
        1.5.2 常用方法
            public String gerMessage()  返回关于发生的异常的详细信息,这个消息在Throwable类的构造函数中初始化了。
            public void printStackTrace() 打印toString()结果和栈层次到 System.err ,即错误输出流。
        1.5.3 try catch
            语法:
                try
                {
                    高风险代码;
                }
                catch (异常类型:变量)
                {
                    出错后的操作;
                }
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    public class Test{
        public static void main(String[] args){
            FileInputStream ss = null;
            try {
                ss = new FileInputStream("F:/test.txt");
            } catch (FileNotFoundException e) {
                String ssg = e.getMessage();
                System.out.println(ssg);
                e.printStackTrace();
            }    
        }
    }

        1.5.4 第二种 try catch
                try
                {
                    高风险代码;
                }
                catch (异常类型:变量)
                {
                    出错后的操作;
                }
                catch(异常类型:变量)
                {
                    出错后的操作;
                }
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        public class Test{
            public static void main(String[] args){
                try {
                    new FileInputStream("F:/test.txt");
                    String ss = null;
                    ss.equals("");
                } catch (FileNotFoundException e) {
                    System.out.println("您所要的东西老娘没找到");
                }catch(NullPointerException e){
                    System.out.println("拿个空的糊弄我么?");
                }    
            }
        }
        1.5.5 第三种 try catch
                    自动关闭资源
            try(开启资源;)
            {
                高风险代码;
            }
            catch (异常类型:变量)
            {
                出错后的操作;
            }
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        public class Test{
            public static void main(String[] args){
                try(FileInputStream fis = new FileInputStream("F:");){/*操作自动关闭,不用finally*/}
                catch(Exception e){
                }
            }
        }
    


1.6 throws
        throws 把异常抛出,是一种提醒,跟调用处说明,这里可能有异常,可以抛出多个异常,多个之间,逗号隔开
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    public class Test{
        public static void main(String[] args){
            try{
                m1();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public static void m1()throws FileNotFoundException{
            m2();
        }
        public static void m2()throws FileNotFoundException{
            new FileInputStream("dnf");
        }
    }
1.7 finally
    finally: 是必须执行的语句,只有一种情况不执行,System.exix(0);
    1. finally 不能单独使用
        2. 必须和 try catch 一起使用
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    public class Test{
        public static void main(String[] args) throws IOException{
            FileInputStream fis = null;
            try{
                fis = new FileInputStream("");
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(fis!=null){
                    fis.close();//关闭资源
                }
            }

        }
    }
1.8 不能有更宽泛的异常
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    public class Test{
        public static void main(String[] args) throws IOException{
            
        }
    }
    class A{
        public void m1() throws FileNotFoundException{
            
        }
    }
    class B extends  A{
        //子类抛出的异常只能和父类或者父类的子类异常一样,或者没有异常
        public void m1()throws FileNotFoundException{
            
        }
    }
1.9 自定义异常
    1.9.1 语法:
        自定义异常类:
            1. 继承一个已有的异常类
                    继承 Exception 即可
            2. 公共的无参构造
            3.有参构造,传入 String 是错误信息
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        public class Test extends Exception{
            public Test(){}
            public Test(String str){
                super(str);
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值