学习java的第十三天 , 学习Math方法及异常如何出现 ,异常的处理方式, 还要继续练习各种方法的调用(这么多方法真的能全记住么);
一. Math类
java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单.
常用方法:
public static int abs(int a) 返回参数的绝对值
public static double ceil(double a) 返回大于或等于参数的最小double值,等于一个整数
public static double floor(double a) 返回小于或等于参数的最大double值,等于一个整数
public static int round(float a) 按照四舍五入返回最接近参数的int
例子:
//静态常量
System.out.println(Math.PI); //3.141592653589793
//绝对值
System.out.println(Math.abs(-100)); //100
//四舍五入
System.out.println(Math.round(3.1)); //3
System.out.println(Math.round(3.8)); //4
//向上取整
System.out.println(Math.ceil(10.00001)); //11.0
System.out.println(Math.ceil(11.999999999999999999999999)); //12.0
//向下取整
System.out.println(Math.floor(10.0000001)); //10.0
System.out.println(Math.floor(11.9999999999999)); //11.0
//两个数最大值
System.out.println(Math.max(10,78)); //max为78
//最小值
System.out.println(Math.min(10,78)); //min为10
//a的b次方
System.out.println(Math.pow(2,4)); //2的4次方 为16
//随机小数
System.out.println(Math.random()); //生成的是[0 1)之间的随机小数
//1-100的随机整数
System.out.println(Math.floor(Math.random()*100)+1);
二. BigInteger 类
2.1概述
java.math.BigInteger类,不可变的任意精度的整数。
如果运算中,数据的范围超过了long类型后,可以使用BigInteger类实现,该类的计算整数是不限制长度的
2.2构造方法
构造方法
public BigInteger(String s);
2.3常用方法
add(BigInteger value) 返回其值为(this + val)的BigInteger,超大整数加法运算
subtract(BigInteger value) 返回其值为(this - val)的 BigInteger,超大整数减法运算
multiply(BigInteger value) 返回其值为(this * val)的 BigInteger,超大整数乘法运算
divide(BigInteger value) 返回其值为(this / val)的 BigInteger,超大整数除法运算,除不尽取整数部分
例子:
BigInteger big1 = new BigInteger("987654321123456789000");
BigInteger big2 = new BigInteger("123456789987654321");
//加法运算
BigInteger add = big1.add(big2);
System.out.println("求和:"+add);
//减法运算
BigInteger sub = big1.subtract(big2);
System.out.println("求差:"+sub);
//乘法运算
BigInteger mul = big1.multiply(big2);
System.out.println("乘积:"+mul);
//除法运算 整数整除 取商部分
BigInteger div = big1.divide(big2);
System.out.println("除法:"+div);
三. BigDecimal 类
3.1概述
java.math.BigDecimal类,不可变的、任意精度的有符号十进制数。
该类可以实现超大浮点数据的精确运算。
3.2 构造方法
public BigDecimal(String value);
3.3常用方法
BigDecimal类的常用方法与 (2.3)BigInteger类的常用方法相同
四. 基本类型包装类
4.1基本类型所相对应的包装类
| 基本类型 | 对应的包装类(位于java.lang包中) |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
4.2字符串与基本类型转换
String转换成基本类型
除了Character类之外,其他所有包装类都具有parseXxx静态方法可以将字符串参数转换为对应的基本类型:
public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。
public static short parseShort(String s):将字符串参数转换为对应的short基本类型。
public static int parseInt(String s):将字符串参数转换为对应的int基本类型。
public static long parseLong(String s):将字符串参数转换为对应的long基本类型。
public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。
public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。
public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean基本类型。
基本类型 ---> String
(1) String s = 基本类型数值+ "";
(2) String 方法
static String valueOf(int num)
4.3 例子
//String ---> int
int i = Integer.parseInt("230");
System.out.println(i); //字符串230转换为int类型的整数230
//String ---> double
double b = Double.parseDouble("10.2");
System.out.println(b); //字符串"10.2"转换为double类型的小数10.2
//String ---> boolean
boolean aTrue = Boolean.parseBoolean("true");
System.out.println(aTrue); //字符串"true"转换为boolean类型的true
System.out.println("----------------------");
//int ----> String
String s = 10 + "";
System.out.println(s); //基本类型的整数10转换为字符串"10"
String s1 = String.valueOf(10);
System.out.println(s1); //基本类型的整数10转换为字符串"10"
五. 异常
5.1概述
异常:指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止
Java处理异常的方式是中断处理
5.2异常(Exception)的分类
(1)编译时期异常: checked异常 (必须处理)
在编译时期,就会检查,如果没有处理异常,则编译失败。(如日期格式化异常)
(2)运行时期异常: runtime异常
在运行时期,检查异常.在编译时期,运行异常不会编译器检测(不报错)。(如数学异常)
5.3异常的产生过程
5.4异常处理
Java异常处理的五个关键字:try、catch、finally、throw、throws
5.4.1 捕获异常 try ....catch (在方法中使用try-catch的语句块来处理异常)
异常的第一种处理方式:
try{
可能出现异常的代码
}catch(异常的类名 变量名){
打印异常的信息
}catch(异常的类名 变量名){
打印异常的信息
}....
多 catch 处理异常 有没有顺序?
如果多个异常之间没有继承关系 没有顺序
如果有继承关系 有顺序 小的在前 大的在后
演示如下:
public static void main(String[] args) {
try {// 当产生异常时,必须有处理方式。要么捕获,要么声明。
read("b.txt");
} catch (FileNotFoundException e) {// 括号中需要定义什么呢?
//try中抛出的是什么异常,在括号中就定义什么异常类型
System.out.println(e);
}
System.out.println("over");
}
//我们 当前的这个方法中 有异常 有编译期异常
public static void read(String path) throws FileNotFoundException {
if (!path.equals("a.txt")) {//如果不是 a.txt这个文件
// 我假设 如果不是 a.txt 认为 该文件不存在 是一个错误 也就是异常 throw
throw new FileNotFoundException("文件不存在");
}
}
5.4.2 finally 代码块
finally :有一些特定的代码无论异常是否发生,都需要执行
无论是否有异常,无论是否捕获了异常在finally代码块中存放的代码都是一定会被执行的。
finally的语法:
try...catch....finally:自身需要处理异常,最终还得关闭资源
5.4.3 声明异常(异常处理的第二种方式)
throw 异常的对象 抛出异常 在方法内使用
throw new 异常类名(参数);
throws 异常的类名 声明异常 在方法的声明上使用
修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2…{ }
代码演示:
public static void main(String[] args) throws IOException {
read("a.txt");
}
public static void read(String path)throws FileNotFoundException, IOException {
if (!path.equals("a.txt")) {//如果不是 a.txt这个文件
//假设 如果不是a.txt认为 该文件不存在 是一个错误 也就是异常 throw
throw new FileNotFoundException("文件不存在");
}
if (!path.equals("b.txt")) {
throw new IOException();
}
}
六. 自定义异常
6.1异常类如何定义:
1. 自定义一个编译期异常: 自定义类 并继承于`java.lang.Exception`。
2. 自定义一个运行时期的异常类:自定义类 并继承于`java.lang.RuntimeException`。
6.2自定义异常练习
模拟注册操作,如果用户名已存在,则抛出异常并提示:亲,该用户名已经被注册。
首先定义一个注册异常类RegisterException
public class RegisterException extends Exception{
public RegisterException(){}
public RegisterException(String message){
super(message);
}
}
模拟登陆操作,使用数组模拟数据库中存储的数据,并提供当前注册账号是否存在方法用于判断
public class Test {
private static String[] names = {"I","Love","Java"};
public static void main(String[] args) {
//调用方法
try {
//可能出现异常的代码
checkUsername("love");
System.out.println("注册成功");//如果没有异常就是注册成功
} catch ( RegisterException e) {
//处理异常
e.printStackTrace();
}
}
//判断当前注册账号是否存在
//因为是编译期异常 又想调用者去处理 所以声明该异常
public static boolean checkUsername(String uname) throws RegisterException {
for (int i = 0; i < names.length; i++) {
String name = names[i];
if (name.equals(uname)){ //如果名字在这里面 就抛出登陆异常
throw new RegisterException("亲"+name+"已经被注册");
}
}
return true;
}
}