常用其他API
一、Math类
java.lang包下——不需要导入
public static void main(String[] args) {
//1.random//随机产生0-1之间的小树,包含0不包含1 同Random类下的random()
System.out.print(Math.random());
//2.abs绝对值
//System.out.prntln(Math.abs(-100));
System.out.println(Math.abs(Byte.MIN_VALUE));//-128可以变成128
System.out.println(Math.abs(Integer.MIN_VALUE));
//Math.abs()不一定能够获取正数Integer
//3.ceil向上取整 floor向下取整,返回的是整数,类型是double型
System.out.println(Math.ceil(3.2));
System.out.println(Math.floor(3.2));
//4.max min返回两个参数中比较大的一个
System.out.println(Math.min(1.5,2.1));
//5.sqrt开平方
System.out.println(Math.sqrt(-1));//不是一个数字
}
二、Date类型
下面的很多方法都已经不被使用(计算时间差的时候使用的)
public static void main(String[] args) {
Date date=new Date();
System.out.println(date);
//加入毫秒整数:计算冲1970年1月1日走过对应的毫秒数后的时间
//返回值是按照本地时间计算
//System.out.println(new Date(毫秒数));
System.out.println(new Date(1000));
//getTime返回从1970年1月1日0点到该date对象所走过的毫秒数
System.out.println(date.getTime());
//日期类型的格式化
//需要通过SimpleDateFormat格式化日期,将时间date类型转换成指定的字符串类型
//格式模板(固定):年yyyy 月MM 日dd 时(24)HH 分钟mm 秒ss
//例如:yyyy-MM-dd HH:mm:ss
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//(1)format 将日期类型对象转换成指定格式的字符串
String strdate=sdf.format(date);
System.out.println(strdate);
//2019-02-05 字符串转换成date
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
//(2)parse方法是将字符串类型转换成Date类型,按照sdf的格式进行转换的,也就是字符串必须符sdf中的二个是
//否则无法解析,无法将字符串类型转换成Date
//sdf1.parse("2019-02-05");
try {
Date d=sdf1.parse("2019-02-05");
System.out.println(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
三、Calender日历类
//java.util下的类
//抽象类,不能够直接创建对象,提供了getInstance方法,来创建对象
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.DATE));
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.HOUR));
System.out.println(c.get(Calendar.MINUTE));
System.out.println(c.get(Calendar.SECOND));
System.out.println(c.get(Calendar.MILLISECOND));
//可以通过set方法设置年月日时分秒
c.set(2000, 2, 3, 12, 23, 24);
System.out.print(c.get(Calendar.MONTH));
//可以通过add方法跟其他日期进行加减
//第一个参数指定年、月、日。。(单位)
//第二个参数指定要经历的时间(时间单位按照第一个参数)正数,往后,负数,往前算
c.add(Calendar.YEAR, 10);//10年之后
System.out.println(c.get(Calendar.YEAR));
}
四、Random
随机类,提供很多随机的方法
public static void main(String[] args) {
Random r=new Random();
//(1)返回随机数
System.out.println(r.nextInt());//返回int整数类型
//(2)返回某个区间的随机数
System.out.println(r.nextInt(100));//返回从0到指定参数-1之间随机数
//几乎所有的数据类型都支持nextX随机
System.out.println(r.nextFloat());//返回0到1.0之间的浮点数float
System.out.println(r.nextDouble());//返回0到1.0之间的浮点数double
}
五、Scanner
扫描键盘输入
hasnext() 判断是否还有下个元素,如果没有,则会阻塞
next()取出输入的下一个元素,就可以赋予给String类型应用
next()的使用,如果输入的元素之间有空格,会按照“两个”元素,如果有多个元素拆开输入
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
String name=sc.next();
System.out.println(name);
if(name.equals("exit")){
break;
System.exit(0);//退出虚拟机 }
sc.close();
}
六、大整数类
8中基本类型long
java中提供了java.math.BigInteger 可以对任意大小的整数进行操作
如果是通过方法,不是通过运算符
public static void main(String[] args) {
BigInteger bi1=new BigInteger("55555555555555555555555555555555555555555555");
BigInteger bi2=new BigInteger("55555555555555555555555555555555555555555555");
System.out.println(bi1.add(bi2));
//add 相加
//substract 减
//multiply乘
//divide除
//mod取余数
//返回值都还是BigInteger
}
七、国际化
背景 当程序中需要使用不同的语言来存储“汉子、英语”信息,可以通过国际化进行编码的统一,防止乱码
需要使用到的类:
(1)java.util.Locale:封装了特定国家的语言环境 CN—zh US—en 国际-语言
(2)java.util.ResourceBundle:用来加载一个国家语言的资源
【资源】properties文件存储,需要显示的“英文、中文”
具体实现
(1)产生一个资源文件:
两类:默认的资源文件 文件名.properties 当没有指定locale就会去加载这个文件
指定语言的资源文件 文件名_zh_CN.properties
文件名_en_US.properties
//name=“姓名”;(可以使用unicode编码)
//age=“年龄”;
//place=“地点”;
(2)通过上面java.util.Locale来确定运行环境
构造器new Locale(语言,国际)
new Locale(“zh”,“CN”)
(3)通过ResourceBundle调取资源文件之间的内容,内容等号左侧的内容
程序就显示=右侧的信息
public class Day11_1_API {
public static void main(String[] args) {
// 如果调用的时候,没有指定 Locale,则会使用默认的properties
ResourceBundle res=ResourceBundle.getBundle("day11/info");
System.out.println(res.getString("name")+res.getString("gender"));
// 加载指定语言环境文件
Locale local=new Locale("en","US");
ResourceBundle res1=ResourceBundle.getBundle("day11/info",local);
System.out.println(res1.getString("name")+res1.getString("gender"));
}
}
异常
一、背景
一个系统处理异常的能力代表该系统的健壮
在编译的过程中,可能遇见的“问题”可能包含三类:
(1)受检异常:在编译期间就可以预见的异常,这种异常必须进行异常的捕获
(2)运行期异常:可以称为是bug,可以处理,也不可以,也可以是异常的捕获
(3)错误:出现在编译期,不是因为没有处理异常,是因为本身的语法或者是规则使用有问题
【问题】如果遇见首检异常,需要进行处理,或者是遇见了运行期异常,又不希望程序执行中断,只能使用异常捕获机制
二、常见的异常类型
1.异常类型层次结构
所有的异常类型最根基的类也是Object
所有的“问题”都集成了Throwable类
具有两个子类,一个是Error(错误),一个是Exception(包含了受检异常和运行期异常)
Exception下分为很多的受检异常子类
熟悉了多个异常类型,才能知道如何处理异常
2.常见的异常
(1)ArichmeticException 数学运算异常 运行期异常
(2)IllegalArgumentException 方法接收参数的时 非法参数异常 运行期异常
(3)ArrayIndexOutOfBoundsException 数组越界异常
(4)NullPointerException 空指针异常,运行期异常 当调用空引用下的属性、方法,出现异常
(5)ClassNotFoundExcept 类没有找到,不能够加载所需要的类
(6)NumberFoundException 格式化异常,使用parse的时候,无法将参数转换成(格式化成)指定的对象
(7)FileNotFoundException :文件不存在异常
class Phone{
public void test(){
}
}
class Person{
private Phone phone;
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
public void buyPhone(Phone p){
this.phone=p;
}
public Person() {
}
}
public class Day11_2_Exception {
public static void main(String[] args) {
// Phone ph=new Phone();
// Person p=new Person();
// p.buyPhone(ph);
// p.getPhone().test();
// int i=1/0;
// int []ii =new int[2];
// System.out.println(ii[2]);
// String s=null;
// System.out.println(s.charAt(1));
String [] ss=new String[2];//[null,null]
Integer.parseInt("abc");
}
}
三、异常的处理
可以有两种方式:捕获异常,抛出异常;
当异常产生的时候,程序会在异常产生的位置,创建一个关于异常类型的对象,对象中包含了异常的信息,异常的堆栈
程序会不会就直接直接终止?不会,从上下文中寻找其他程序
如果成功的捕获了异常,可以继续运行其他程序
如果没有成功捕获,异常会继续向上传播
【向上传播】程序中方法A调用了方法B,A称为是上,B称为是下,调用者是上,被调用者是下。
宗旨:让调用者处理
public class Day11_2_Exception {
public void A(){
this.B();
}
public void B(){
this.C();
}
public void C(){
this.D();
}
public void D(){
this.E();
}
public void E(){//下
int i=1/0;//出现了异常,在自己的上下文中寻找是否有异常处理的程序?
// 没有异常处理,所以继续将抛出给调用者。
}
public static void main(String[] args) {
Day11_2_Exception d= new Day11_2_Exception();
d.A(); //A出现了异常
}
}
1.捕获异常
try{
可能会产生异常的代码段
}catch(异常类型 异常对象的引用){
处理
}
当使用try catch处理了异常的时候,会遇见的三种情况
(1)try中代码正常执行,没有出现异常,不会走任何catch语句,try catch之外的代码也会正常执行
public class Day11_2_Exception {
public static void main(String[] args) {
int a=10;
int b=2;
try{
System.out.println(a/b);
System.out.println("try中异常之后的代码段");
}catch(ArithmeticException e){
System.out.println("除数为 0的异常!!!");
}
System.out.println("其他代码段");
}
}
(2)try中产生了异常,异常之后的代码不会执行,某一个catch捕获了异常,try catch之外的代码也会正常执行
catch可以有多个,会将异常信息跟catch中的异常类型进行匹配,匹配的顺序从上到下依次匹配,只要遇见一次匹配的类型,则不再继续向下匹配
合并异常:当认为多个异常类型可以做统一处理的时候,可以合并异常
如果多个异常之间有父子继承关系:子类异常在上,父类异常在下
【最后一个异常Exception】 如果try中出现了意外代码,需要必须要解决的情况,就会 被最后一个异常捕获,相当于掩盖异常
public class Day11_2_Exception {
public static void main(String[] args) {
int a=10;
int b=5;
int []z=new int[]{1,2,3};
try{
System.out.println(a/b);
System.out.println(z[3]);
System.out.println("try中异常之后的代码段");
// }catch(ArrayIndexOutOfBoundsException | ArithmeticException e){
// System.out.println("出现了错误");
// }
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界");
}catch(ArithmeticException e){
System.out.println("除数为 0的异常!!!");
}catch(Exception e){
System.out.println("Exception");
}
System.out.println("其他代码段");
}
}
(3)try中产生了异常,异常之后的代码不会执行,没catch成功捕获的异常,try catch之外的代码不会正常执行
将异常向上抛出
【思考】处理异常的程序,是调用端出来?还是方法中处理?
将异常处理放在调用端,不同的调用者处理异常的程序可能不同,如果只放在方法中,就变成了一种方式
public class Day11_2_Exception {
public static int test(int a,int b){
return a/b;
}
public static int test2(int a,int b){
int result=0;
try{
result=a/b;
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界");
}catch(ArithmeticException e){
System.out.println("除数为 0的异常!!!");
}
return result;
}
public static void first(){
int a=10;
int b=5;
try{
test(a,b);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界");
}catch(ArithmeticException e){
System.out.println("除数为 0的异常!!!");
}
System.out.println("其他代码段");
}
public static void main(String[] args) {
}
}
2.finally
【作用】任何情况下都会运行语句,不管程序中是否发生了异常?是否成功的捕获了异常?
可以运用的关键字组合:
try catch 可以
try finally 可以
try catch finally 可以
catch和finally不能脱离try使用
【不论什么情况,finally永远执行,除非退出虚拟机】
【finally适用场合】
资源被打开时候,占用内存,释放资源
public static void main(String[] args) {
while(true) {
try {
int i=1/0;
break;
//System.exit(0);
}catch(ArithmeticException e) {
System.out.println("ArithmeticException");
}finally {
System.out.println("始终会执行的代码段");
}
}
// System.out.println("其他代码段");
}
3.抛出异常
【格式】
在某个条件符合的时候,throw new 创建异常对象()——抛出异常
如果主动抛出异常实在方法中,在方法定义的尾部throws关键字 异常类型:要求调用该方法的人,必须去捕获异常(受检异常)
【使用的用法】
(1)可以使用到catch中
public static void test() {
try {
int i=1/0;
}catch(ArithmeticException e) {
System.out.println("ArithmeticException");
throw new ArithmeticException();
}
}
public static void main(String[] args) {
test();
}
(2)直接使用throw的是抛出异常:不掩盖函数定义时出现的异常
都是主动抛出受检异常,目的为了让调用者,强制调用者捕获异常。
public class Day11_2_Exception {
public void regist(int age) throws Exception{
if(age>0)
System.out.println("成功注册");
else{
System.out.println("年龄<0");
throw new Exception("出错了");
}
}
public void browse(){
System.out.println("浏览网页");
}
public static void main(String[] args) {
Day11_2_Exception d=new Day11_2_Exception();
try{
d.regist(-10);
}catch(Exception e){
System.out.println("");
}
d.browse();
}
}
4.重写
子类方法不能抛出笔父类更多的受检异常【规则】
子类是为了扩展
访问权限不能低于父类的访问权限
子类异常要少于父类异常(受检异常)
class Fruit{
public void m(){
}
}
class Apple extends Fruit{
public void m() throws Exception{
}
}
Fruit f=new Apple(); f.m()
//编译期按照父类中m进行校验,运行期的时候,按照子类中m进行运行
5.自定义异常
但是一旦业务非常复杂,自定义一些符合业务需求的异常类型
如果希望自定义运行期异常,继承RuntimeException
如果希望自定义的是受检异常,直接继承Exception
习惯上,自定义异常都以Exception结尾
【自定义异常的语法】
class 自定义的异常类 extends Exception{
习惯上要有至少两个构造器(一个有参数的,一个没参数的)
public 无参数构造器
public 有参数构造器(参数message)
}
class AgeException extends Exception{
public AgeException(){}
public AgeException(String message){
super(message);
}
}
public class Day11_2_Exception {
public void regist(int age) throws AgeException{
if(age>0)
System.out.println("成功注册");
else{
System.out.println("年龄<0");
throw new AgeException("出错了");
}
}
public void browse(){
System.out.println("浏览网页");
}
public static void main(String[] args) {
Day11_2_Exception d=new Day11_2_Exception();
// try{
// d.regist(-10);
// }catch(AgeException e){
// System.out.println("");
// }
// d.regist(-10);
}
}
【异常的优势】
(1)将可能产生错误的代码和正常的代码分离
(2)可以将异常向上传播,调用者可以根据自己的需求来处理异常
(3)异常类型明确,可以继承,异常分类处理,可以清楚的分出不同异常类型,不同异常类型就可以使用不同的方式处理。