美文网首页
7月17号笔记

7月17号笔记

作者: 须臾之北 | 来源:发表于2018-07-18 22:37 被阅读12次

7月17日笔记

今天的主要内容:

  • 数据类型的包装类
  • 异常处理机制

1. 数据类型包装类

A. 基本类型
  • byte Byte
  • short Short
  • long Long
  • char Character
  • int Integer
  • double Double
  • boolean Boolean
  • void Void
B. 相关操作(Integer为例)
  • 装箱

    • Integer numObj = new Integer(500); //装箱
    • Integer numObj2 = 5; //自动装箱
  • 拆箱

    • int num = numObj; //拆箱
    • int num = numObj.intValue();
  • 测试toString()和parseInt()

    • Integer类中重写了toString()方法

         String numStr = numObj.toString();
         System.out.println(numStr);
         //输出结果为500
      
    • Integer类中的parsrInt()方法
      System.out.println(Integer.parseInt(numStr));
      //输出结果为:500

  • 测试Integer.valueOf()

    • System.out.ptintln(Integer.valueOf("123456"));
      • 输出结果为123456
    • 此时涉及到自动装箱知识的理解
  • 自动装箱的理解

      Integer n1 = 5;
      Integer n2 = 5;
      
      System.out.println(n1==n2);   //true;
      
      Integer n4 = 500;
      Integer n3 = 500;
      
      System.out.println(n3==n4);   //false       
    
      Integer n5 = 50;
      Integer n6 = new Integer(50);
      
      System.out.println(n5==n6);   //false;  
    
      /*
          自动装箱的理解:
              Integer n1 = 5;
              Integer n2 = 5;
                  ·此时实际原理为:Integer n1 = Integer.valueOf(5);
                  ·而valueOf方法对于-128 到 127之间的数值进行装箱后返回一个Integer对象。
                  ·因此同一数字5在valueOf方法作用下被返回同一Integer对象,这就是为什么n1==n2正确  
                  
          而对于
              Integer n3 = 500;
              Integer n4 = 500;       
                  ·此时不在-128 到 127之间,所以Integer n3 = new Integer(500);
                  ·因此n3==n4不正确,n3和n4不是同一对象
          同理我们可以理解
              Integer n5 = 50;
              Integer n6 = new Integer(50);
                  ·此时,n5通过Integer.valueOf(50)返回一个存在缓存中的Integer对象
                  ·而n6的Integer是new出来的,因此n5和n6并不是同一对象的引用           
      */          
    

2. 异常(Exception)

A. 异常分类

  • 异常对象都派生于Throwable类的一个实例,多有一场都继承于Throwable

  • Throwable又分为:Error和Exception

    • Error和Exception的区别:
      • Error类描述了Java运行时系统内部错误和资源耗尽错误,JVM引起的。
      • Exception分为:RuntimeException和其他异常。
  • RuntimeException

    • RuntimeException异常为非受查的异常,可不处理的异常,一般人为的异常
    • 常见的RuntimeException异常有:
      • ArrayOutOfBoundsException异常
      • NullPointerException异常
      • ArithmeticException异常
  • 除RuntimeException以外的异常

    • 必须要进行异常处理

B. 异常处理机制try-catch-finally

  1. try-catch执行顺序问题:

    • 在try语句块中的任何代码抛出了一个在catch子句中声明的异常类,即

      ①程序将跳过try语句块的其余代码

      ②程序将执行catch子句中的处理器代码

      ③最后执行finally子句中的代码

    • 在try中没有异常,则执行完try后,再执行finally中的代码

  2. finally中return语句对try中return的影响

     public class TestFinally{
         public static void main(String[] args){
             int n = 500;
             
             System.out.println("初值为:" + n);
             
             n = test();
             System.out.println("调用test()方法后:n = " + n);
         
             System.out.println("此输出证明:若finally子句中也有一个return语句,则此时返回值会覆盖原始的try中的返回值");
         
             System.out.println("===============异常信息的获取===============");
             try{
                 throw new Exception("抛出异常");
             }
             catch(Exception e){
                 System.out.println("e.getClass().getName() : " + e.getClass().getName());
                 System.out.println("e.getMessage() : " + e.getMessage());
             }
         }
    
         public static int test(){
             
             System.out.println("=======test()方法执行中=======");
             try{    
                 System.out.println("我是try中第一条语句,我没有异常");        
                 return 0;
             }   
             finally{
                 System.out.println("此输出证明try语句中的return语句执行前会首先执行finally,然后再return");    
                         
                 return 3;
             }
         }
     }
     /*
         初值为:500
    
         =======test()方法执行中=======
         我是try中第一条语句,我没有异常
         此输出证明try语句中的return语句执行前会首先执行finally,然后再return
    
         调用test()方法后:n = 3
         此输出证明:若finally子句中也有一个return语句,则此时返回值会覆盖原始的try中的返回值
    
         ===============异常信息的获取===============
         e.getClass().getName() : java.lang.Exception
         e.getMessage() : 抛出异常
     */
    
  3. throw和throws区别

    • throw手动抛出一个异常
    • throws用在方法声明处,声明在方法上抛出一个异常,本方法的调用方法负责处理异常
  1. 异常信息获取的常见函数

    • e.printStackTrace();
    • e.getMessage(); ---错误信息
    • e.getClass().getName(); ---异常名称

相关文章

网友评论

      本文标题:7月17号笔记

      本文链接:https://www.haomeiwen.com/subject/qraapftx.html