Java学习笔记11-异常与常用类

1.异常

        异常是指程序无法正常执行完毕。

(1)异常的体系     

   Throwable
   /       \
Error      Exception

不同类型的异常分别用不同的Java类表示,所有异常的根类为java.lang.Throwable,Throwable下面又派生了两个子类:Error和Exception

Error : 错误,一般由虚拟机生成并脱出,无需要程序猿管理

Exception : 异常

         CheckedException 编译时异常|检查时异常: 发生在程序编译期间 程序如果遇到编译时异常没有处理,程序无法正常运行

         RuntimeException 运行时异常 : 发生程序运行期间 一般可以通过增强程序健壮性的代码处理 if

:如果程序遇到异常没有处理,无法继续执行

常见的一些运行时异常:

1.空指针异常 NullPointerException

2.数组越界异常 ArrayIndexOutOfBoundsException

3.数组长度负数异常 NegativeArraySizeException

4.类型转换异常 ClassCastException

5.数学异常 ArithmeticException

6.数字格式异常 NumberFormatException

(2)异常的处理

异常抛出 : throws 把异常抛出到上一层,谁调用谁解决

throws:通常被用在声明方法时,用来指定方法可能抛出的异常,多个异常可使用逗号分隔。throws关键字将异常抛给上一级,如果不想处理该异常,可以继续向上抛出,但最终要有能够处理该异常的代码

throw:通常用在方法体中或者用来抛出用户自定义异常,并且抛出一个异常对象。程序在执行到throw语句时立即停止,如果要捕捉throw抛出的异常,则必须使用try-catch语句块或者try-catch-finally语句

异常捕获 : try..catch

try{
    有可能出现异常的代码;
}catch(FileNotFoundException e){
    处理异常代码;
}catch(NullPointerException e){
    处理异常代码;
}catch(Exception e){
    所有异常都能接收;
}finally{
    无论try中是否会出现异常,都会执行finally中的代码
    //资源的关闭等代码
}
  • 一个try的后面可以接 1~n个catch
  • try中如果一旦出现异常,try下面的代码不会执行,直接进入catch的判断
  • catch从上到下一次判断,满足哪一个catch捕获的类型,就执行对应的语句体
  • 异常一旦捕获,程序执行过程中出现的异常按照指定方案解决,不会影响后续代码的执行
//实例代码-异常的处理
public class Class002_Exception {
    public static void main(String[] args) {
        System.out.println("主方法开始了");
        //异常捕获
        try{
            System.out.println("try开始了");
            //System.out.println(5/0);
            test();
            System.out.println("try结束了");
        }catch (FileNotFoundException e){
            System.out.println("这是文件未找到异常");
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("最终的代码无论是否出现异常都会执行");
        }
        System.out.println("主方法结束了");
    }

    public static void test() throws FileNotFoundException {
        InputStream is = new FileInputStream("D:\\AAA\\DDD\\haha.txt");
    }
}

2.常用类

(1-1)String类(字符串相关类)

String-不可变长字符序列,以通过不同的形式构建String的对象,最常见的是字符串常量的形式,直接使用一对 "" 即可,也 可以通过构造器的方式,还可以通过字符数组和字节数组的形式来构建。       

//实例代码-String对象的创建
public class Class001_String {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //String() 初始化新创建的 String对象,使其表示空字符序列。
        String str1 = new String();
        String str2 = "";
        System.out.println(str1==str2);  //false

        //String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
        String str3 = new String("abc");
        System.out.println(str3);

        //String(char[] value) 分配新的 String ,使其表示当前包含在字符数组参数中的字符序列。
        //字符数组转为字符串
        char[] arr = {'y','j','x','x','t'};
        String str4 = new String(arr);
        System.out.println(str4);

        //String(char[] value, int offset, int count) 分配一个新的 String ,其中包含字符数组参数的子数组中的字符。
        //字符数组中某一部分字符转为字符串
        String str5 = new String(arr,1,3);
        System.out.println(str5);

        //String (byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组构造新的 String 。
        //不同的字符编码格式(转换方式参照不同的字符集) 1个汉字->gbk->2个字节  1个汉字->utf-8->3个字节
        //字符串转为字节数组
        byte[] arr2 = "你好".getBytes(); //默认字符编码格式utf-8
        byte[] arr3 = "你好".getBytes("gbk"); //字符编码格式gbk
        System.out.println(arr2.length);
        System.out.println(arr3.length);
        //要保证编码解码字符编码一致才不会出现乱码

        //字节数组转为字符串-->注意编码格式问题
        String str6 = new String(arr2);
        System.out.println(str6);

        //String(byte[] bytes, String charsetName) 构造一个新的String由指定用指定的字节的数组解码charset 。
        String str7 = new String(arr3,"gbk");
        System.out.println(str7);

        //字节数组中某一部分字节数据转为字符串
        //String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
        //String(byte[] bytes, int offset, int length, String charsetName) 通过使用指定的字符集解码指定的字节子 String构造新的 String 。
        String str8 = new String(arr2,3,3);
        System.out.println(str8);
    }
}

常用操作:

String类的下述方法能创建并返回一个新的String对象: concat, replace, substring, toLowerCase, toUpperCase, trim

提供查找功能的有关方法: endsWith, startsWith, indexOf,,lastIndexOf

提供比较功能的方法: equals, equalsIgnoreCase, compareTo

public static String valueOf(…) 可以将基本类型数据转换为字符串

(1-2)StringBuffer和StringBuilder(字符串相关类)

StringBuffer: 线程安全的,相对效率较低 多线程下大量操作字符串建议使用StringBuffer

StringBuilder: 线程不安全,但不保证同步,相对效率较高 适合使用在单线程下大量操作字符串,效率高

效率: StringBuilder > StringBuffer > String

一般使用StringBuilder

//实例代码-StringBuilder创建对象和常用方法
public class Class003_StringBuilferStringBuffer {
    public static void main(String[] args) {
        //StringBuilder() 构造一个字符串构建器,其中不包含任何字符,初始容量为16个字符。
        StringBuilder sb = new StringBuilder();
        System.out.println(sb);
        System.out.println(sb.length());  //字符个数
        System.out.println(sb.capacity()); //内部存储数据的字节数组的长度

        //StringBuilder(int capacity) 构造一个字符串构建器,其中没有字符,并且具有 capacity参数指定的初始容量。
        StringBuilder sb1  = new StringBuilder(10);
        System.out.println(sb1);
        System.out.println(sb1.length());
        System.out.println(sb1.capacity());

        //StringBuilder(String str) 构造一个初始化为指定字符串内容的字符串构建器。
        StringBuilder sb2  = new StringBuilder("abc");
        System.out.println(sb2);
        System.out.println(sb2.length());
        System.out.println(sb2.capacity());

        //StringBuilder append(boolean b)  追加
        StringBuilder stringBuilder = sb.append(false);
        System.out.println(sb);
        System.out.println(stringBuilder==sb);

        //StringBuilder delete(int start, int end) 删除此序列的子字符串中的字符。
        System.out.println(sb.delete(1,4));
        System.out.println(sb);
        System.out.println(sb.length());
        System.out.println(sb.capacity());

        //测试扩容问题
        sb.append("1234567890");
        System.out.println(sb.length());
        System.out.println(sb.capacity());

        sb.append("1234");
        System.out.println(sb.length());
        System.out.println(sb.capacity());

        sb.append("1");
        System.out.println(sb.length());
        System.out.println(sb.capacity());

        //StringBuilder insert(int offset, String str) 将字符串插入此字符序列。
        System.out.println(sb.insert(3,"haha"));

        //StringBuilder reverse() 导致此字符序列被序列的反向替换。
        System.out.println(sb.reverse());
        System.out.println(sb);

        //String 与 StringBuffer|StringBuilder转换问题:
            //1.new StringBuilder|StringBuffer(String)
            //2.toString() | new String(StringBuilder|StringBuffer)
        System.out.println(sb.toString());
    }
}

(2)包装类

 包装类的优点:

        1.类可以提供很多成员,功能...

        2.集合中之能存储引用数据类型,想要存储基本数据类型数据的时候,可以先转为对应的包装类型,再存储

        3.基本数据类型与对应包装类型数据的默认值不同,当在具体业务下,比如区分账户余额的0与null两种状态,可以使用包装类型表示账户余额

基本数据类型的优点:  有利于节约内存

自动装箱与拆箱:就是将基本类型和包装类进行自动的互相转换。

自动装箱的过程:每当需要一种类型的对象时,这种基本类型就自动地封装到与它相同类型的包装中。

自动拆箱的过程:每当需要一个值时,被装箱对象中的值就被自动地提取出来

自动拆装箱:
    1.自动装箱:  基本-->包装
    2.自动拆箱:  包装-->基本
//实例代码-自动拆箱与装箱
public class Class001_Data {
    public static void main(String[] args) {
        int i = 1;
        //自动装箱  Integer.valueOf(int)
        Integer in = 1;
        //自动拆箱 in.intValue()
        int i2 = in;
        test(1.1,2.2); //自动装箱
    }

    static void test(Double d1,Double d2){
        System.out.println(d1+d2); //自动拆箱
    }
}

(3)Math类

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

//实例代码-Math类的常用方法
public class Class001_Math {
    public static void main(String[] args) {
        System.out.println(Math.abs(-10)); //10 获取-10的绝对值
        System.out.println(Math.pow(3, 2)); //9.0 3的平方
        System.out.println(Math.max(21, 12));//21 大数
        System.out.println(Math.sqrt(9)); //3.0 开方
        System.out.println(Math.ceil(15.2)); //16.0 向上取整
        System.out.println(Math.floor(9.9)); //9.0 向下取整
        System.out.println(Math.ceil(-15.2)); //-15.0
        System.out.println(Math.floor(-9.9)); //-10.0
        System.out.println(Math.round(1.3)); // 1 四舍五入

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值