当前位置: 代码迷 >> 综合 >> Java学习 DAY14 包装类、教学类、时间、异常
  详细解决方案

Java学习 DAY14 包装类、教学类、时间、异常

热度:41   发布时间:2024-02-12 14:29:36.0

包装类

6.包装类对象的哈希码值是固定的
7.数值类型的包装类的父类是Number

package cn.tedu.baozhuang;public class BZDemo1 {public static void main(String[] args) {//包装类的对象的哈希码值是固定值System.out.println(new Integer(1).hashCode());System.out.println(new Byte((byte) 1).hashCode());System.out.println(new Short((short) 1).hashCode());System.out.println(new Long(1).hashCode());System.out.println(new Character('a').hashCode());System.out.println(new Float(1.1f).hashCode());System.out.println(new Double(1.1).hashCode());System.out.println(new Boolean(true).hashCode());//Integer in=1;m(1);}public static void m(Object obj){}
}

数学类

Math数学类,提供简单的数学运算(提供的属性和方法都是静态的)

package cn.tedu.math;public class MathDemo {public static void main(String[] args) {//绝对值//System.out.println(Math.abs(-2.134));//向上取整/*System.out.println(Math.ceil(1.0000001));//向下取整System.out.println(Math.floor(1.999999));//四舍五入(结果是整数)System.out.println(Math.round(3.45));*///第一个参数是底数,第二个参数是次幂//System.out.println(Math.pow(2.0,3.0));//随机数(从0.0到1.0之间(不包含1.0))//底层根据随机算法//System.out.println(Math.random());//获取从20到40(包含)之间的随机数//System.out.println(20+(int)(Math.random()*21));//简易验证码//验证码可以出现的内容char[] cs={'4','A','8','G','9','B','2','撒'};//字符串来存储验证码内容String str="";//循环实现随机选取验证码内容for (int i = 0; i < 4; i++) {//验证码只有四位str+=cs[(int)(Math.random()*9)];//下标范围(0~7)}//System.out.println(str);}
}

BigDecimal类提供小数精确运算(保证参数是字符串形式)

package cn.tedu.math;
import java.math.BigDecimal;
public class BigDecemalDemo {//在运算过程中可以提高到80位来运算,但最终还是以64位来存储public strictfp static void main(String[] args) {//绝大部分小数的二进制补码形式是无限位数不能精确计算/*double d=2.1-1.9;System.out.println(d);*///只有参数是字符串形式才能进行精确运算BigDecimal bd1=new BigDecimal("2.1");BigDecimal bd2=new BigDecimal("1.9");//调用方法操作对象的值System.out.println(bd1.subtract(bd2));}
}

BigInteger类提供超大数之间的运算

package cn.tedu.math;import java.math.BigInteger;public class BigIntegerDemo {public static void main(String[] args) {//超大数之间的运算//BigInteger bi1=new BigInteger("5343212313245342135421315646877983");BigInteger bi2=new BigInteger("5134532312135431231546431321231585");//通过调用调用操作对象值System.out.println(bi1.multiply(bi2));}
}

时间

Date类代表时间和日期
SimpleDateFormat类提供日期和字符串的相互转换
parse():把字符串转成日期
foamat():把日期转成字符串

package cn.tedu.time;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class DateDemo {public static void main(String[] args) throws ParseException {//创建代表时间和日期的类//Date date=new Date();//CST(中国标准时区)---当前时间和日期//有参构造---在指定的基础上加上1900年1月//黑线---代表过时,代表在某个新版本中就会被淘汰/*Date date=new Date(2008-1900,8-1,8);System.out.println(date);*///字符串与日期的相互转换//字符串转成日期String str="2012-12-12 12:12:12";//创建对象//ParseException---解析异常//指定解析格式SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//调用方法把字符串转成日期Date date=sdf1.parse(str);//日期转成字符串//创建对象SimpleDateFormat sdf2=newSimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");//调用方法把日期转成字符串String s=sdf2.format(date);System.out.println(s);}
}

Calendar类代表日历类

package cn.tedu.time;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo {public static void main(String[] args) {//获取Calendar类的子类对象Calendar c=Calendar.getInstance();//设置时间c.setTime(new Date(2012-1900,12-1,12));//获取日历信息System.out.println(c.get(Calendar.DAY_OF_WEEK));System.out.println(c.get(Calendar.DAY_OF_MONTH));}
}

异常

用于发现问题、反馈问题以及解决问题的一套机制
Throwable类—异常的顶级父类
子类--------
Error:是一个合理的应用程序,不该试图抓住的严重问题(改变外部需求、环境、资源等等)
Exception:是一个合理的应用程序可以吹了也可以不处理
处理方式(1.抛出、2.捕获)

package cn.tedu.exception;import java.text.SimpleDateFormat;public class ExceptionDemo1 {public static void main(String[] args) {//算术异常----编译没错,运行有错/*try {System.out.println(1/0);}catch (Exception e){}*///System.out.println(1/0);//数组下标越界---编译没错,运行有错/*int[] arr={1};arr[1]=1;*///克隆不支持异常-----编译有错/*try {new ExceptionDemo1().clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}*///解析异常---编译有错//new SimpleDateFormat().parse();}
}

分类
编译时异常—在编译时期出错 一定要处理
除了RuntimeException类以及子类以外其他的异常类
CloneNotSupportedException—克隆不支持异常
ParseException—解析异常
运行时异常—在运行时期出错
可以处理也可以不处理 RuntimeException类以及子类
ArithmeticException—算术异常
NullPointerException—空指针异常
ArrayIndexOutofBoundsException—数组下标越界异常
ClassCastException—类型转换异常
NumberFomatException—数字格式异常

package cn.tedu.exception;public class ExceptionDemo2 {public static void main(String[] args) {//调用方法读取文件//方法上抛出几个编译时异常就捕获几个编译时异常try {//try块里存放的是可能会出问题的代码String s=readFiles(null);//"W:\\a.txt"} catch (FileNotExitException e) {//catch块会一只检测try块里是否有异常//如果出现异常,根据对应的异常类的类型来捕获对应抛出的异常//e.printStackTrace();System.out.println("问题已经处理...");}catch (FilesNotFoundException e){//=new FilesNotFoundException("亲,你的文件类型不对!!!");//获取描述信息//调用父类的方法来间接的获取父类的私有化属性值System.out.println(e.getMessage());}catch (NullPointerException e){//打印栈轨迹e.printStackTrace();}//当异常被处理之后后续代码正常执行System.out.println("读取完成");}//定义读取文件的方法//方法上可以抛出多个异常(中间用逗号间隔)//如果方法上必须要抛出的异常就是编译时异常public static String readFiles(String path) throws FileNotExitException, FilesNotFoundException,NullPointerException {//判断路径是否是nullif (path == null) {//进了判断说明path为null//发现问题,反馈问题throw new NullPointerException();}//判断文件类型是否是txt文件if (!path.endsWith("txt")) {//进了判断说明不是txt文件//发现问题//反馈问题(描述信息),往上传递异常类对象throw new FilesNotFoundException("亲,你的文件类型不对!!!");}//判断路径是否是W盘符if (path.startsWith("W")) {//进了判断说明以W可判断来开头//说明出现问题//反馈问题---往上传递异常类对象//反馈问题后面代码都不执行throw new FileNotExitException();}//读取文件内容return "文件内容";}
}//自定义异常类
//自定义异常类继承除了RunTimeException类以及子类以外其他的异常类
//自定义异常默认就是编译时异常
class FileNotExitException extends Exception{}
class FilesNotFoundException extends Exception{/* //私有化属性private String message;//提供获取私有化属性的get方法@Overridepublic String getMessage() {return message;}//有参构造给私有化属性进行赋值public FilesNotFoundException(String message){this.message=message;}*///有参构造给父类的私有化属性进行赋值public FilesNotFoundException(String message){super(message);}}
  相关解决方案