java常用类和异常机制

自动拆装箱

//测试自动拆装箱
public class AutoboxAndUnbox {
    public static void main(String[] args) {
        //jdk5.0后,自动装箱,编译帮我们改进代码:Integer a = new Integer("100");
        Integer a = 100;
        Integer b = 200;

        //int c = new Integer("1000");
        int c = new Integer(1000);  //自动拆箱,编译器改进:new Integer(1000).intValue();

        Integer d = null;
        int e = d; //d.intValue(); 会抛出空指针异常(NullPointerException)

        Integer f = 1234;
        Integer f1 = 1234;
        System.out.println(f == f1);  //false
        System.out.println(f.equals(f1));  //true

        Integer g = 123;//[-128, 127]之间的数,仍然当做基本数据类型来处理
        Integer g1 = 123;
        System.out.println(g == g1);  //true
        System.out.println(g.equals(g1));  //true
    }
}

Date类

//测试Date类
public class DateTest {
    public static void main(String[] args) {
        Date d = new Date();
        System.out.println(d);  //打印当前时间

        //获取从时间戳到此时此刻所经历的毫秒数
        long t = System.currentTimeMillis();
        System.out.println(t);

        Date d2 = new Date(1000);
        System.out.println(d2.toGMTString()); //1 Jan 1970 00:00:01 GMT

        d2.setTime(24324324);

        System.out.println(d2.getTime());  //24324324

        System.out.println(d.getTime() < d2.getTime()); //false
    }
}

DateFormat类和SimpleDateFormat类

//测试DateFormat类和SimpleDateFormat类
public class DateFormatTest {
    public static void main(String[] args) {
        //DateFormat类是抽象类
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");//MM必须大写

        Date d = new Date();
        String str = df.format(d); //将时间对象按照格式化字符串转化为字符串
        System.out.println(str);  //2016年11月30日 10:38:26

        String str2 = "1997-7-7";
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d2 = df2.parse(str2);//将字符串按照格式化字符串转化为时间对象
            System.out.println(d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Calendar类和GregorianCalendar类

注意:
- 月份:1月用0表示,12月用11表示
- 星期:周日是1,周六是7

//测试Calendar类
public class CalendarTest {
    public static void main(String[] args) {
        //Calendar是抽象类
        Calendar c = new GregorianCalendar();
        c.set(2001, 1, 10, 12, 23, 34);
        Date d = c.getTime();
        System.out.println(d); //Sat Feb 10 12:23:34 CST 2001

        //测试日期计算
        c.add(Calendar.YEAR, 30); //加30年
        System.out.println(c);
    }
}

可视化日历小程序

public class VisualCarlendar {
    public static void main(String[] args) throws Exception {
        System.out.println("请输入日期(格式1970-01-01):");
        Scanner scanner = new Scanner(System.in);
        String inputStr = scanner.nextLine();       

        //将输入的日期字符串转化成为日期
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date inputDate = df.parse(inputStr);

        //将日期放进Calendar类进行处理
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(inputDate);

        //记录输入的日期
        int date = calendar.get(Calendar.DATE);

        //确定当月的1号是星期几
        calendar.set(Calendar.DATE, 1);
        int startDay = calendar.get(Calendar.DAY_OF_WEEK);

        //确定当月最大的天数是多少
        int endDay = calendar.getActualMaximum(Calendar.DATE);

        System.out.println("一\t二\t三\t四\t五\t六\t日");
        //打印空格
        for(int i = 0; i < startDay - 2; i++) {
            System.out.print("\t");
        }

        for(int i = 1; i <= endDay; i++) {
            //标记输入的日期
            if(date == i) {
                System.out.print(i + "*\t");
            }else{
                System.out.print(i + "\t");
            }
            int temp = calendar.get(Calendar.DAY_OF_WEEK);
            //每到星期日换行
            if( temp == Calendar.SUNDAY) {
                System.out.println();
            }
            //每打印一次日期加一
            calendar.add(Calendar.DATE, 1);
        }
    }
}

异常处理类结构

需要程序员处理的异常是已检异常
异常

带资源的try语句

try(Scanner in = new Scanner(new FileInputStream("F:/DB/abc.txt")),PrintWriter out = new PrintWriter("out.txt")){
  while(in.hasNext()) {
    out.println(in.next().toUpperCase());
  }
}

1)、不论这个块如何退出,in和out都会关闭
2)、带资源的try语句自身也可以带catch和finally字句,这些字句会在资源关闭后执行

分析堆栈跟踪元素

public class StackTraceTest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter n: ");
        int n = in.nextInt();
        factorial(n);
    }

    //求阶乘
    public static int factorial(int n) {
        System.out.println("factorial(" + n + ");");
        Throwable t = new Throwable();
        StackTraceElement[] frames = t.getStackTrace();  //获取所有堆栈信息,组成一个数组
        for(StackTraceElement f : frames) {
            System.out.println(f);
        }
        int r;
        if(n <= 1) {
            r = 1;
        }else{
            r = n * factorial(n - 1);
            System.out.println("return " + r);
        }
        return r;
    }
}

使用断言

assert关键字有两种用法
1)、assert条件
2)、assert条件:表达式
这两种方式都会对条件进行检测,如果结果为false,就抛出一个AssertionError异常

debug常用快捷键

  • F5 单步跳入,遇到函数进入函数内部
  • F6 单步跳过,遇到函数不进入函数内部
  • F7 单步返回,由函数内部返回到调用处
  • F8 一直执行到下一个断点

异常

异常处理的三种方式:
- try…catch…finally语句
- throws关键字(声明异常:将异常交给调用者处理)
- throw关键字手动抛出异常
方法重写中声明异常的原则:
- 父类没有声明异常,那么子类也不能声明异常
- 子类不可抛出原有方法抛出异常类的父类或上层类
- 子类抛出的异常类型的数目不可以比原有方法抛出的还多
自定义异常:
从Exception类或它的子类派生一个子类即可

//自定义异常
public class MyException extends Exception{
    //自定义异常一般要两个构造器:
    //一个空参构造器,一个是带有详细信息的构造器
    public MyException() {

    }

    public MyException(String message) {
        super(message);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值