zoukankan      html  css  js  c++  java
  • Java学习第十天

    1:包装类:
      byte      Byte
      short     Short
      int       nteger
      long       Long
      char       Character
      boolean    Boolean
      double      Double
      float       Float

    2.基本类型转成字符串类型
      String.valueOf()
      String ss =Integer.toString(7)
      String str=Double.toString(6.6)

    3字符串类型转成基本类型:常用
      int a = Integer.parseInt("88");
      double d=Double.parseDouble("99.99");

    4.十进制转成其它进制

      String num = Integer.toHexString(60);
      String n =Integer.toOctalString(24);
      String m = Integer.toBinaryString(6);

    5.其它进制转十进制
      int x = Integer.parseInt("3c",16); //第一个参数是被转的数,第二个参数说明第一个参数的进制
      int x = Integer.parseInt("30",8);
      int x = Integer.parseInt("110",2);


    6: 装箱和拆箱
      

     1 Integer tt=6;// new Integer(6) ---自动装箱
     2   tt=tt+2;// tt.intValue()---6 自动拆箱 6+2==8 new Integer(8) 自动装箱
     3 
     4 
     5   Integer m=127;
     6   Integer n=127;
     7 
     8   sop(m==n);//true jdb1.5开始
     9 
    10   m=128;
    11   n=128;
    12 
    13   sop(m==n);//false
    14   //如果一个数在byte的范围之内(-128---127),之前已经定义过,再定义时使用已经定义的

    7:异常:程序在运行过程中出现的特殊情况,
      Java把程序在运行时出现的不正常情况,提取属性和行为描述(异常名称,异常发生的位置,异常具体信息)
      从而出现了异常,从而实现了异常的面向对象

    8.异常体系: (继承Object类)Throwable
            (Throwable的子类)--| Error:严重的错误,没办法处理的
            (Throwable的子类)--| Exception:异常,程序运行时可处理的问题
    9.异常特点:具备可抛性

      默认的异常处理方式:系统创建异常类对象,因为main方法不能处理异常,抛给了JVM,JVM调用异常类对象的printStackTrace()方法,
      输出异常名,异常信息,异常发生的位置,然后程序中断

      try{

      }catch(Exception e){
      }

      throws声明可能会出现的异常
      必须处理:1:使用 try{}catch(){} 2:使用throws继续声明抛出

    10.多重异常:

    1 try{
    2 
    3 }catch(XXX e){
    4 
    5 }catch(YYY ee){
    6 
    7 }

      子类异常要写在父类异常的上边

    11.自定义异常:

    1 class MyException extends Exception{
    2 
    3 MyException(){}
    4 
    5 MyException(String message)
    6 {
    7 super(message);
    8 }
    9 }

      对于自定义异常,系统不会自动创建异常类对象,所以需要使用throw 手动创建异常类对象

    12.throw:用于手动抛出异常类对象
      用了throw,必须处理,处理方式有2种:
        1.使用try{}catch{}
        2:使用throws继续声明抛出

    13.throw和throws对比:
      1.throw用在方法内部,throws用在方法后边
      2.throw后边是异常类对象,throws后边是异常类

    14.java把异常分为两类:
      非运行时异常:编译时检测的异常,必须接收处理
      运行时异常:编译时不检测的异常-------RuntimeException及其子类,编译不检测,即使使用了throws或throw,不处理,编译照样通过
          原因:java认为出现运行时异常,程序就该中断,不该处理
             运行时异常都是因为数据传送出错造成的,程序就该中断,修改错误数据,然后再执行

     1 例子:
     2 class MyMath
     3 {
     4   public int div(int a,int b)
     5   {
     6     return a/b;// throw new ArithmeticException()
     7   }
     8 }
     9 class Demo5 
    10 {
    11   public static void main(String[] args) //不用try{}catch(){} 可以,只不过是程序异常时会中断
    12   {
    13     MyMath myMath=new MyMath();
    14     //检测异常
    15     try
    16     {
    17       int m = myMath.div(5,0); //new ArithmeticException()
    18       System.out.println(m); //不执行
    19 
    20     }catch(Exception e)//捕获 Exception e=new ArithmeticException() 多态
    21     {
    22       System.out.println(e.getMessage());//异常信息
    23       System.out.println(e.toString());//异常类名:异常信息
    24       e.printStackTrace(); //异常类名:异常信息 异常发生的位置
    25 
    26       System.out.println("除数为0了");
    27     }
    28     System.out.println("hehe")    
    29   }
    30 }

      注意:对于程序来说,最好把问题提前到编译时期,也就是说运行时可能发生的异常,在编译时期就能知道

    15.finally中的代码在return之前执行的

     1 System.exit()退出JVM虚拟机,finally中的代码不能执行
     2 
     3 try{}catch(){}
     4 
     5 try{}catch(){}finally{}
     6 
     7 try{}finally{必须执行的语句}
     8 
     9 特殊情况:
    10 class MyMath
    11 {
    12   public int div(int a,int b)
    13   {
    14   return a/b;
    15   }
    16 }
    17 class Demo11 
    18 {
    19   public static void main(String[] args)
    20   {
    21     MyMath myMath=new MyMath();
    22 
    23   try{
    24 
    25     int m = myMath.div(5,0); 
    26     System.out.println(m);
    27 
    28   }catch(Exception e)
    29   {
    30     System.out.println(e.getMessage());
    31     //return;
    32     System.exit(1);//退出JVM finally中的代码不能执行
    33   }finally
    34   {
    35     System.out.println("哈哈");//finally中的代码在return之前执行
    36   }
    37   }
    38 }
  • 相关阅读:
    TCP/IP研究(1)-Timer(2)
    linux学习
    TCP/IP研究(2)-TCB
    vi学习笔记
    TCP/IP研究(1)-Timer
    yxr:Makefile 简单样本
    zt:vim环境配置
    zt:文件轻松比对,伟大而自由的比较软件们
    就是这么简单!使用Rest-assured 测试Restful Web Services
    手把手教你接口自动化测试 – SoapUI & Groovy
  • 原文地址:https://www.cnblogs.com/demain/p/11305853.html
Copyright © 2011-2022 走看看