Day9 JAVA复习

it2022-05-05  213

个人复习Day9

1.4常用工具类1.4.1 基本数据类型对应的封装类(包裹类)1.4.2 自动拆箱,自动封箱1.4.5 Math类1.4.6 Date类 1.5异常1.5.1 理解异常1.5.2 异常的关系1.5.3 处理异常1.5.4 自定义异常

1.4常用工具类

1.4.1 基本数据类型对应的封装类(包裹类)

byte short long float double boolean 首字母大写 int(Integer) char(Character)

1.4.2 自动拆箱,自动封箱

//自动封箱:把基本数据类型变成引用类型 intefer i1=1; //Integer.valueOf(1);//封箱底层的实现原理

public static void test1(){ int i1=1; i1=new Integer("2");//自动封箱:把基本数据类型变成引用类型 //Integer.valueOf(1);//封箱底层的实现原理 System.out.println(i1); Integer i2=3; //自动拆箱:把引用类型变成基本数据类型 //i2.intValue(); System.out.println(i1+i2); } public static void test2(){ String strAge="18"; int age=Integer.parseInt(strAge); System.out.println(age+1); } //数据的缓存-128~127 public static void test3(){ Integer i1=new Integer("10"); Integer i2=new Integer("10"); System.out.println(i1.equals(i2));//比较值 System.out.println(i1==i2);//比较地址 i1=10; i1=Integer.valueOf(10);//调用Integer.valueof(i),返回i1i2的值,i1i2成为int i2=10; i2=Integer.valueOf(10); System.out.println(i1.equals(i2));//比较值 System.out.println(i1==i2);//这里i1i2是int,==只比较值 //Integer.valueof(i)范围是-128~127 i1=200; i2=200;//超出范围,返回new,只要new地址肯定不一样 System.out.println(i1.equals(i2));//比较值 System.out.println(i1==i2);//比较地址 } //基本数据类型只比较值 public static void test4(){ int a=10; double b=10.0; float c=10.0f; System.out.println(a==b); System.out.println(b==c); }

1.4.5 Math类

向上取整、向下取整、四舍五入、最大值、随机数

//大于等于 最小 整数 向上取整 public static void test1(){ double d=Math.ceil(4.89); System.out.println(d); //小于等于最大整数 向下取整 double d2=Math.floor(4.89); System.out.println(d2); //四舍五入 System.out.println(Math.round(3.7)); //最大值 System.out.println(Math.max(Math.max(3, 6),4)); //随机数 Math.random();//[0,1] Random r=new Random(); r.nextInt(10);//随机数[0,10)不包括

1.4.6 Date类

显示时间 new Date(); Thu Jul 18 15:24:52 CST 2019 在此请注意几个字母大小写的差异:

大写的H为24小时制表示一天中的小时数(0-23)小写的h为12小时制表示一天中的小时数(1-12)大写的M表示年中的月份小写的m表示小时中的分钟数大写的S表示毫秒数小写的s表示秒数所以最常用的24小时制的具体日期的pattern为:yyyy-MM-dd HH:mm:ss //大于等于 最小 整数 向上取整 public static void test1(){ double d=Math.ceil(4.89); System.out.println(d); //小于等于最大整数 向下取整 double d2=Math.floor(4.89); System.out.println(d2); //四舍五入 System.out.println(Math.round(3.7)); //最大值 System.out.println(Math.max(Math.max(3, 6),4)); //随机数 Math.random();//[0,1] } public static void test2(){ Random r=new Random(); r.nextInt(10);//随机数[0,10)不包括 } public static void test3() throws ParseException{ System.out.println(new Date()); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //字符串到日期类型 字符串满足日期格式"yyyy-MM-dd HH:mm:ss String strDate="2019-07-18 15:31:14"; //sdf.parse(strDate); System.out.println(sdf.parse(strDate)); //日期到字符串类型 //sdf.format(new Date()) System.out.println(sdf.format(new Date())); } public static void test4(){ Calendar c=Calendar.getInstance(); System.out.println(c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH)+1)+"-"+c.get(Calendar.DAY_OF_MONTH)); //设置生日 c.set(Calendar.YEAR, 2019); c.set(Calendar.MONTH,06); c.set(Calendar.DAY_OF_MONTH,16); c.set(1997, 06, 16); System.out.println(c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH))+"-"+c.get(Calendar.DAY_OF_MONTH)); } //JDK1.8的两个和日期时间相关的类 public static void test5(){ LocalDate date1=LocalDate.now(); System.out.println(date1); LocalDateTime LDT=LocalDateTime.now(); System.out.println(LDT); }

1.5异常

1.5.1 理解异常

异常可以通过系统提供的类进行处理

1.5.2 异常的关系

public static void test1(){ /* 继承关系: * Throwable Error Exception 编译时异常:必须处理 运行时异常:编译时不要求必须处理,代码编写不严谨造成的 */ } //编译时异常 public static void test3(){ //IOException 文件未找到异常 FileReader fr=new FileReade("a.txt"); Thread.sleep(1000); } //运行时异常 public static void test2(int x,int y){ System.out.println(x/y);//除数为0 String[] a=null; a[0]="hello";//空指针异常 int[] b={3};//数组越界 System.out.println(b[2]); }

1.5.3 处理异常

try{}catch{} throws //正确,但要保证两个catch块子类先在前面 try{ }catch(){ }catch(){ } //错误 try{ }catch(Exception e){ }catch(IOException ex){ } //错误 try{ }try{ }catch(){ }

public class Demo8 { public static void test1(){ /* 继承关系: * Throwable Error Exception 编译时异常:必须处理 运行时异常:编译时不要求必须处理,代码编写不严谨造成的 */ } public static int test5(){ //顺序执行,finally必定执行,所以try的return先挂着 int i=1; try{ System.out.println("try"); return i++; }catch(ArithmeticException e){ return 2; }finally{ i++; System.out.println("finally"); //return i; //如果在这里返回就不走try的return } } //编译时异常 public static void test3(){ //IOException 文件未找到异常 try { //可能会出现异常的代码,一旦出现异常,后边的代码不能执行 //跳到catch块 //try块和catch块都可以包含其他的trycatch块 FileReader fr=new FileReader("a.txt"); System.out.println("读文件的数据"); } catch (FileNotFoundException e) { // 堆栈信息 e.printStackTrace(); }finally{ //不管有没有异常都必定执行 } // Thread.sleep(1000); } //编译时异常 public static void test4() throws FileNotFoundException{ //IOException 文件未找到异常 //可能会出现异常的代码,一旦出现异常,后边的代码不能执行 FileReader fr=new FileReader("a.txt"); System.out.println("读文件的数据"); } //运行时异常 public static void test2(int x,int y){ System.out.println(x/y);//除数为0 String[] a=null; a[0]="hello";//空指针异常 int[] b={3};//数组越界 System.out.println(b[2]); } public static void main(String[] args) throws FileNotFoundException { //throws异常给main异常,main方法把异常抛给JVM System.out.println(test5()); } }

1.5.4 自定义异常

步骤: 1.定义接口,存放一个错误信息 2.定义异常类,用构造方法调用信息 3.定义类,在set方法设置异常条件 4.定义测试类,输出try catch

/* * 自定义异常类 * 1.保证当前类是异常类的子类 * 2.设置异常信息 */ public class AgeIllException extends Exception{ public AgeIllException(){ super(MsgInter.ageMsg); } public AgeIllException(String msg){ super(msg); } } //定义接口存放错误信息 public interface MsgInter { String ageMsg="年龄赋值错误"; }public class Student { private int age; public int getAge() { return age; } //自定义异常和错误信息的绑定 public void setAge(int age) throws AgeIllException { if(age>=0&&age<=150) this.age = age; //抛出异常(产生异常) throw new AgeIllException(); } } public class TestException{ public static void main(String[] args) { Student stu=new Student(); try{ stu.setAge(52); System.out.println(stu.getAge()); }catch(AgeIllException e){ e.printStackTrace(); System.out.println(e.getMessage()); } } }

最新回复(0)