day05 API异常

BigDecimal类小数

import java.math.BigDecimal;

//BigDecimal类的加减乘与BigInteger类相同,除法不同
public class BigDecimalTest {
    public static void main(String[] args) {
    method01();
    }

    /* 除法
    *  BigDecimal 进行除法运算
    *  方法名字 divide
    *  如果计算的结果,是无限小数,出现异常
    *
    *  divide方法参数 divide(BigDecimal b,int scale,int round)
    *   b : 除数
     *  scale : 保留的位数
     *  round : 舍入模式:
     *    BigDecimal定义了静态成员变量 (舍入模式)
     *    ROUND_UP  向上加1
     *    ROUND_DOWN 直接舍去
     *    ROUND_HALF_UP 四舍五入
    *
    * */
    public static void method03(){
        BigDecimal b1 = new BigDecimal("5.56");
        BigDecimal b2 = new BigDecimal("1.55");

        //b1/b2
        //保留指定的位数,进行取舍
        //BigDecimal divide = b1.divide(b2, 2, BigDecimal.ROUND_UP); 向上加1
        // BigDecimal divide = b1.divide(b2,2,BigDecimal.ROUND_DOWN); 直接舍去
        BigDecimal divide= b1.divide(b2,12,BigDecimal.ROUND_HALF_UP); //四舍五入
        System.out.println(divide);
    }




    /*加减乘
     *  BigDecimal 高精度的浮点运算
     *  四则运算,使用方式和BigInteger几乎一样
     *  +  -   *
     */
    public static void method02(){
        BigDecimal b1 = new BigDecimal("5.56");
        BigDecimal b2 = new BigDecimal("1.55");

        //b1+b2
        BigDecimal add = b1.add(b2);
        System.out.println(add);

        //b1-b2
        BigDecimal subtract = b1.subtract(b2);
        System.out.println(subtract);

        //b1*b2
        BigDecimal multiply = b1.multiply(b2);
        System.out.println(multiply);
    }





    /*
    * BigDecimal构造方法
    * 运算精确性和超级大数,选择使用String参数的构造
    *  BigDecimal(String value)
    * */
    public static void method01(){
        BigDecimal bigDecimal = new BigDecimal("132132132.156313");
        System.out.println(bigDecimal);
    }
}

--------------------------------------------------------

BigInteger类整数

import java.math.BigInteger;

/*
 *  大数据运算类
 *  基本类型 int long 取值范围
 *  数据可能远超过long的范围
 *  Java: 超过long范围的整数,
 *    java.math.BigInteger 封装了超级大整数
 *    称为BigInteger对象,超级大数计算的方式和结果,称为对象
 */
public class BigIntegerTest {
    public static void main(String[] args) {
    method02();
    }

    /*
    * BigInteger 四则运算 + - * /
    * 而是BigInteger对象的运算,计算结果不是数,是BigInteger对象
    * add(BigInteger)+运算
    * subtract(BigInteger) -运算
    * multiply(BigInteger) *运算
    * divide(BigInteger) /运算
    * */

    public static void method02(){
        BigInteger b1 = new BigInteger("1564165341653454165321");
        BigInteger b2 = new BigInteger("1231345646");

        //b1对象+b2对象
        BigInteger add = b1.add(b2);
        System.out.println(add);

        //b1对象-b2对象
        BigInteger subtract = b1.subtract(b2);
        System.out.println(subtract);

        //b1对象*b2对象
        BigInteger multiply = b1.multiply(b2);
        System.out.println(multiply);

        //b1对象/b2对象
        BigInteger divide = b1.divide(b2);
        System.out.println(divide);
    }




    /*
    * BigInteger构造方法
    * BigInteger(String value)可以传递任意长度的整数
    * */

    public static void method01(){
        BigInteger bigInteger = new BigInteger("45643135416532413524165324156532416");
        System.out.println(bigInteger);
    }


}

--------------------------------------------------------

Math类

/*
 *  java.lang.Math类,数学计算类
 *   最终类,不能继承
 *   Math类的方法全部静态修饰,类名调用即可
 *   私有构造方法
 */
public class MathTest {
    public static void main(String[] args) {
        method02();
    }


    /*
    * static double sqrt(double d)
    * 计算参数的平方根
    * */
    public static void method07(){
        double sqrt = Math.sqrt(4);
        System.out.println(sqrt);
    }




    /*
    * static double random()
    * 产生随机数:是浮点型,范围0.0-1.0之间
    * 范围:可能是0.0,不可能到1.0
    * */
    public static void method06(){
        for (int i = 0; i < 10; i++) {
            double random = Math.random();
            System.out.println(random);
        }
    }


    /*
    * static double pow(double a,double b)
    * 幂运算:a的b次幂
    * poe(2,3)   2的3次方
    * */
    public static void method05(){
        double pow = Math.pow(2, 3);
        System.out.println(pow);
    }



    /*
    * static long round (double d)
    * 对参数进行四舍五入,取整数部分
    * 算法:参数d+0.5
    * */
    public static void method04(){
        long round = Math.round(5.4999);
        System.out.println(round);
    }



    /*
    * static double floor(double d)
    * 向下取整
    * */
    public static void method03(){
        double floor = Math.floor(11.2);
        System.out.println(floor);
    }



    /*
    * static double ceil(double d)
    * 传递参数返回结果,结果大于或者等于参数
    * 向上取整数
    * */
    public static void method02(){
        double ceil = Math.ceil(8.1);
        double ceil02 = Math.ceil(-18.1);
        System.out.println(ceil);
        System.out.println(ceil02);
    }



    /*
    * static int abs(int a)
    * 绝对值:传递参数,返回他的绝对值
    * */
    public static void method01(){
        int abs = Math.abs(-5);
        System.out.println(abs);
    }
}

--------------------------------------------------------

基本类型包装类

/*
 *  基本数据类型,对象包装类
 *  提供8个基本类型,除去布尔类型以外,其他类型进行基本数学计算
 *  JDK提供每个基本类型的包装类, 一切多是对象,基本数据类型也是对象
 *  基本类型对应的包装类(位于java.lang包中)
 *  byte    Byte
 *  short   Short
 *  int     Integer
 *  long    Long
 *  float   Float
 *  double  Double
 *  char    Character
 *  boolean Boolean
 */
public class BaseTest {
    public static void main(String[] args) {
        //范围
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);

        System.out.println(Long.MAX_VALUE);
        System.out.println(Long.MIN_VALUE);

        System.out.println(Double.MAX_VALUE);
        System.out.println(Double.MIN_VALUE);
    }
}

01 Integer类
字符串转成Integer对象

/*
 *  int类型的对象包装类 java.lang.Integer
 *  包装int类型 ,基本类型提供了更多的功能
 */
public class IntegerTest01字符串转成Integer对象 {
    public static void main(String[] args) {
        method02();
    }

    /*第二种
     * static Integer valueOf(String s)传递字符串,字符串转成Integer对象
     */
    public static void method02(){
        Integer integer = Integer.valueOf("100");
        System.out.println(integer);
    }




    /*第一种
     * Integer 类的构造方法
     * Integer(String s) 传递字符串,字符串转成Integer对象
     * 注意: 字符串必须是数字格式
     */
    public static void method01(){
        Integer integer = new Integer("100");
        System.out.println(integer);
    }
}

字符串转成int

/*
 * 基本数据类型包装类:
 *   实现了基本数据类型和字符串的互转
 *   "100"数据类型是String  "100"+1 1001
 */
public class IntegerTest02字符串转成int {
    public static void main(String[] args) {
        method02();
    }
    /*第二种
     * 使用Integer类的非静态方法,需要new对象
     * intValue() 返回值int类型
     * 将Integer构造方法中的字符串转成int
     */
    public static void method02(){
        Integer integer = new Integer("100");
        int i = integer.intValue();//构造方法中的字符串转成int
        System.out.println(i+1);
    }




    /*第一种
     *  Integer类静态方法,直接.调用
     *  static int parseInt(String str)方法参数字符串,转成int基本类型
     *  必须数字格式字符串
     */
    public static void method01(){
        int i = Integer.parseInt("100");
        System.out.println(i+1);
    }

}

02 自动装箱和拆箱

import java.util.ArrayList;

/*
 * JDK5版本,出现新特性: 自动装箱和自动拆箱
 * 自动装箱 : 基本数据类型自动包装成对象
 * 自动拆箱 : 对象在自动转成基本数据类型
 */
public class AutoBoxTest101 {
    public static void main(String[] args) {
    method03();
    }



    /*
     * 自动装箱和自动拆箱例子
     */
public static void method03(){
    //创建集合,ArrayList 存储整数
    ArrayList<Integer> list = new ArrayList<>();
    //存储的时候,自动装箱, 自动提升为Integer对象
    list.add(100);
    list.add(200);
    list.add(300);
    for (int i = 0; i < list.size(); i++) {
        //合理,自动拆箱
        int j=list.get(i);
        System.out.println(j);
    }
}



    /*
     *  代码上出现问题:
     *    任何引用类型,都可以赋值为null
     */
    public static void method02(){
        Integer i=null;
        if(i!=null){
            i=i+1; //拆箱, i.intValue()
            System.out.println(i);
        }
    }



    /*
     * 自动装箱和拆箱
     */
public static void method01(){
    //基本类型的数据,赋值到引用类型,自动装箱过程
    //自动将基本类型1,封装成Integer对象
    Integer i=1;//等同于 i = new Integer(1)

    //先将引用类型i,转成基本类型(自动拆箱)
    //i+1的结果,还是基本类型, 赋值的时候,进行了装箱操作
    i=i+1;//等同于  i.intValue()+1 -->拆箱  结果等到2  装箱

    System.out.println(i);
}

}

public class AutoBoxTest102细节问题 {
    public static void main(String[] args) {
        method();
    }
    /*
     * 自动装箱和拆箱细节问题
     */
    public static void method(){
        Integer ii = new Integer(100);
        Integer jj = new Integer(100);
        System.out.println(ii==jj);//引用类型比较地址  F
        System.out.println(ii.equals(jj));//继承Object,重写方法equals,比较的具体数据 T
        System.out.println("----------------------------");

        Integer a=200;//new Integer(200)
        Integer b=200;
        System.out.println(a==b);//引用类型比较地址 F
        System.out.println(a.equals(b));//继承Object,重写方法equals,比较的具体数据 T
        System.out.println("----------------------------");

        /*
         * 数据在byte范围内,从缓存中取出对象使用,不会new
         * -128到127的范围,为了节约内存空间
         * 200大于,所以new了对象,比较地址值
         * 而100在范围内,直接用,比较数值大小
         */
        Integer aa=100;// static Integer Integer.valueOf(100)
        Integer bb=100;
        System.out.println(aa==bb);//引用类型比较地址  True
        System.out.println(aa.equals(bb));//继承Object,重写方法equals,比较的具体数据 T
    }
}

--------------------------------------------------------

异常
01

/*
* 异常:
*    定义就是指程序在运行期间,出现的不正常现象
*    面向对象语言,什么都是对象,异常也是对象
*    对象是由类产生的,new对象()
*
*    java.lang.Throwable类,是java语言中所有错误和异常的父类
*    具有2个子类:Error,Exception
*
*    Error:表示程序中的所有错误
*          程序中出现了严重问题,不能修改源代码,不能执行
*          比如:非典,艾滋病,癌症
*
*    Exception:表示程序中的所有异常
*           程序中出现的小问题,处理完,程序继续执行
*           比如:扎刺,阑尾炎
*
*     异常的继承体系:
*           Throwable类中的方法,所有的子类具备
*           String toString():返回字符串,表示异常信息的简短描述
*           String getMessage():返回字符串,表示异常信息的详细描述
*           void  printStackTrace(): 将异常信息,追踪到标准错误流
* */
public class ExceptionTest01 {
    public static void main(String[] args) {
        int[]arr=new int[99999999];
    }
}

02

/*
* 异常:
*    异常产生的过程和JVM的默认处理方式
*
*    以前的程序,出现异常的几率很大
* */
public class ExceptionTest02 {
    public static void main(String[] args) {
        int[]arr={1,2,3,4,5};
        int element = getElement(arr, 5);
        System.out.println(element);
        System.out.println("程序结束");

    }
    /*
     * 创建方法: 传递数组和索引
     * 返回该索引上的元素
     */
    public static int getElement(int[]arr,int index){
        return arr[index];
    }
}
//ArrayIndexOutOfBoundsException用非法索引访问数组时抛出的异常。如果索引为负或大于等于数组大小,则该索引为非法索引。

03

/*
 *  异常处理,自己处理,不让JVM处理
 *  异常处理的方式: try(尝试) catch(捕获)方式
 *  try{
 *      要被检测的代码
 *      可能出现异常的代码
 *  }catch(异常类的类名  变量名){
 *      异常的处理方式代码
 *      写输出语句,循环,判断,调用方法...
 *  }finally{
 *      一定要执行的代码
 *  }
 */
public class ExceptionTest03 {
    public static void main(String[] args) {
        int[]arr={1,2,3,4,5};
        try {
            int element = getElement(arr, 5);
            System.out.println(element);
        }catch (Exception ex){
            System.out.println("异常被处理了");
        }
        System.out.println("程序结束");
    }
    /*
     * 创建方法: 传递数组和索引
     * 返回该索引上的元素
     */
    public static int getElement(int[]arr,int index){
        return arr[index];
    }
}

04

/*
 *  多catch并行处理
 *  try{
 *
 *  }catch(异常类名  变量){
 *
 *  }catch(异常类名  变量){
 *
 *  }
 *
 *  ArrayIndexOutOfBoundsException和NullPointerException
 *  当多个catch中,捕获的异常类,没有父子关系的时候,先写谁都可以
 *
 *  ArrayIndexOutOfBoundsException继承IndexOutOfBoundsException
 *  IndexOutOfBoundsException继承RuntimeException
 *
 *  NullPointerException继承RuntimeException
 *
 *  当多个catch中,捕获的异常类,异常类之间存在继承关系
 *  越是父类,越往后写
 */
public class ExceptionTest04 {
    public static void main(String[] args) {
        //进行异常处理了
        try {
            show(10);
        }catch (ArrayIndexOutOfBoundsException ex){
            System.out.println("处理了数组越界异常");
        }catch (NullPointerException ex){
            System.out.println("处理了空指针异常");
        }
    }





    /*
     * 创建方法,设置一个参数int类型
     * 要求:
     *    参数传递的是0,方法抛出数组的越界异常
     *    参数传递的是非0,方法抛出空指针异常
     */
    public static void show(int i){
        if(i==0){
            //方法抛出数组的越界异常
            int[]arr=new  int[1];
            arr[1]=1;
        }else {
            int[]arr=null;
            arr[i]=1;
        }
    }
}

05

/*
 * 多个catch合并为一个catch
 *   catch中捕获的异常类,没有继承关系
 *   不推荐使用: 多个合并为一个之后,处理方式只有一个
 */
public class ExceptionTest05{
    public static void main(String[] args) {
        //进行异常处理了
        try {
            show(10);
        }catch (ArrayIndexOutOfBoundsException | NullPointerException ex){
            System.out.println("异常被处理了");
        }
    }
    /*
     * 创建方法,设置一个参数int类型
     * 要求:
     *    参数传递的是0,方法抛出数组的越界异常
     *    参数传递的是非0,方法抛出空指针异常
     */
    public static void show(int i){
        if(i==0){
            //方法抛出数组的越界异常
            int[]arr=new  int[1];
            arr[1]=1;
        }else {
            int[]arr=null;
            arr[1]=1;
        }
    }
}

06

/*
 * finally代码块
 *   try{
 *
 *   }catch(异常类 变量名){
 *
 *   }finally{
 *
 *   }
 *
 *   作用: 无论程序是否出现异常,finally代码块中的程序必须执行
 *   释放资源
 *
 */
public class ExceptionTest06 {
    public static void main(String[] args) {
        int[]arr={1,2,3,4,5};
        try {
            int element = getElement(arr, 5);
            System.out.println(element);
        }catch (Exception ex){
            System.out.println("异常被处理了");
        }finally {
            System.out.println("这里必须执行");
        }
    }
    /*
     * 创建方法: 传递数组和索引
     * 返回该索引上的元素
     */
    public static int getElement(int[]arr,int index){
        return arr[index];
    }
}

07

/*
 *   Throwable类定义了三个方法
 *       String toString(): 返回字符串,表示异常信息的简短描述
 *       String getMessage(): 返回字符串,表示异常信息的详细描述
 *       void printStackTrace(): 将异常信息,追踪到标准错误流
 */
public class ExceptionTest07 {
    public static void main(String[] args) {
        int []arr={1,2,3};
        try {
            int element = getElement(arr, 10);
            System.out.println(element);
        }
        //捕获到异常   Exception ex =  new ArrayIndexOutOfBoundsException();
        //多态性: 变量ex调用方法
        catch (Exception ex){
            System.out.println(ex);// 返回字符串,表示异常信息的简短描述  java.lang.ArrayIndexOutOfBoundsException: 10
            String message = ex.getMessage();
            System.out.println(message);// 返回字符串,表示异常信息的详细描述  10

            ex.printStackTrace();
        }

    }
    public static int getElement(int[]arr ,int index){
        /*
         * 数组的索引不存在
         * 产生异常的对象 new ArrayIndexOutOfBoundsException()
         * 对象会被抛出,方法的调用者
         */
        return arr[index];
    }
}

08

/*
 *  异常处理:
 *   关键字 throw, throws
 *   throw: 关键字: 只能写在方法内部, 后面必须是new出来的异常对象
 *   throws: 关键字: 只能写在方法的声明上,后面必须是异常类的类名
 */
public class ExceptionTest08 {
    public static void main(String[] args) {
        int[]arr={1,2,3};
        //调用的方法,抛出异常,调用者,处理异常 try catch
        try {
            getElement(arr,10);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    /*
     *  判断索引的合法性
     *  变量index,必须是合法的索引,否则不能返回
     *
     *  你是计算个税计算器  -10
     *
     *  方法的定义上,表现出来方法中有异常
     *  关键字 throws
     */
    public static int getElement(int[]arr,int index) throws Exception {
        if(index<0||index>arr.length-1){
            throw new Exception();
        }
        return arr[index];
    }
}

09


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 *  异常分类:
 *    Exception 所有异常的父类 (继承Throwable)
 *     |-- RuntimeException 运行异常|-- 所有子类
 *
 *     |-- 非RuntimeException 编译异常
 *
 *   方法中抛出的异常是运行异常: 不能抓,修改源码
 *   方法中抛出的异常是编译异常: try catch 或者throws
 */
public class ExceptionTest09 {
    public static void main(String[] args) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str="2019-11-20";
        /*
         *  调用了抛出异常的方法
         *  抛出的异常是编译异常
         *  调用者必须处理,否则编译失败
         *  try catch  throws
         */
        Date date = simpleDateFormat.parse(str);
        System.out.println(date);

        /*
         * 方法抛出异常是运行异常
         * 方法中抛出的异常是运行异常,方法的声明上不需要写throws
         * 定义方法的这个人,不打算让调用者去抓这个异常
         * 一旦程序真的抛出运行异常 (初衷: 不要去抓,修改源码)
         */
        int i = Integer.parseInt("10a0");
        System.out.println(i+1);
    }
}

10

public class FuShuExcepetion extends Exception {
    /*
     * Exception类,带有String参数的构造方法
     * 赋值异常的消息,调用父类的构造方法,传递异常信息
     */
    public FuShuExcepetion(String message){
        super(message);
    }
}

/*
 * 自定义异常
 *  定义类继承Exception
 */
public class ExceptionTest10 {
    public static void main(String[] args) {

        try {
            int avg = getAvg(90, 90, -90);
            System.out.println(avg);
        }catch (FuShuExcepetion ex){
            ex.printStackTrace();
        }
    }

    /*
     *  创建方法,计算学生考试的平均分
     *  三个成绩,数学,语文,英语
     *  成绩不能是负数,如果是负数,不能计算
     *  告诉方法调用者成绩是负数,利用异常
     */

    public static int getAvg(int math,int chinese,int english)throws FuShuExcepetion{
        if(math<0){
            //手动抛出负数成绩异常
            throw new FuShuExcepetion("数学成绩是负数");
        }
        if(chinese<0){
            //手动抛出负数成绩异常
            throw new FuShuExcepetion("语文成绩是负数");
        }
        if(english<0){
            //手动抛出负数成绩异常
            throw new FuShuExcepetion("英语成绩是负数");
        }
        return (math+chinese+english)/3;
    }
}

11

class Fu{
    public void show(){

    }
}
class Zi extends Fu{
    public void show(){

    }
}
/*
 *  子类父类异常处理
 *  1: 如果父类的方法抛出了异常
 *    子类重写: 可以不抛出异常,也可以抛出异常
 *    但是: 子类抛出的异常,不能大于父类抛出的异常
 *    重写父类的方法,保证方法的声明和父类一致
 *
 *  2: 如果父类的方法没有异常抛出
 *   子类就不能抛出,子类方法中, 调用了抛出异常的方法,只能try catch
 *
 *  线程: 重写接口中的方法run()
 */
public class ExceptionTest11 {
    public static void main(String[] args) {
        Fu fu=new Zi();
        fu.show();
    }
}

--------------------------------------------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值