玖叶教程网

前端编程开发入门

学员笔记连载,第13天,API、异常

1.Math类

1.1概述

java.lang.math 数学工具类 和数学相关的一些方法 都是静态方法,直接类名调用

1.2方法

绝对值 public static int abs(int a)返回参数的绝对值

 System.out.println(Math.abs(100));//绝对值为100

四舍五入 public static int(long) round(int(double) a) 返回大于或者等于参数的最小double值,等于一个整数 参数为float返回值为int 参数值为double 返回值为long

System.out.println(Math.round(10.3));//结果为10
System.out.println(Math.round(10.5));//结果为11

向上取整 public static double ceil(double a)

System.out.println(Math.ceil(10.1));//结果为11.0

向下取整 public static double floor(double a)

 System.out.println(Math.floor(10.1));//结果为10.0

取最大 public static int max(int a,int b)

System.out.println(Math.max(10,2));//结果为10

取最小 public static int min(int a,int b)

System.out.println(Math.min(10,2));//结果为2

取a的b次方 public static double pow(int a,int b)

System.out.println(Math.pow(2,3));//2的三次方为8

随机小数 Math.random()随机小数[0,1)

System.out.println(Math.random());//取随机小数0-1不包含1的小数

2.大整型BigInteger

java.math.BigInteger 进行大整形的运算

2.1构造方法

public BigInteger(String args);

BigInteger bigInteger1=new BigInteger("1234567891011121314151617181920");
 BigInteger bigInteger2=new BigInteger("1234567891011121314151617181920");

2.2方法

add(BigInteger b)加法

BigInteger add = bigInteger1.add(bigInteger2);

subtract(BigInteger b)减法

BigInteger subtract = bigInteger1.subtract(bigInteger2);

multiply(BigInteger b)乘法

BigInteger multiply = bigInteger1.multiply(bigInteger2);

divide(BigInteger b)除法 除不尽取整数部分 整数整除 不要小数

BigInteger divide = bigInteger1.divide(bigInteger2);

intValue,转换为int类型 注意在int的范围内

longValue,转换为long类型 注意在long的范围内

3.BigDecima

3.1构造方法

BigDecimal(String val);

3.2方法

加add( BigDecima n)

  BigDecimal add = bigDecimal1.add(bigDecimal2);

减subtract( BigDecima n)

   BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);

乘multiply( BigDecima n)

  BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);

除法divide( BigDecima n) 不能除出现无限循环的,必须保证返回的结果是精确的.//会报异常

除法divide( BigDecima n,保留的小数位数,保留方式)

 BigDecimal divide = bigDecimal1.divide(bigDecimal2, 2, BigDecimal.ROUND_HALF_DOWN);//直接舍去
 BigDecimal divide1 = bigDecimal1.divide(bigDecimal2, 2, BigDecimal.ROUND_HALF_UP);//向上+1
 bigDecimal1.divide(bigDecimal2,2,BigDecimal.ROUND_HALF_UP);//四舍五入

4.包装类

4.1八个包装类

包装类

基本类型

Integer

int

Character

char

Boolean

boolean

Long

long

Double

double

Byte

byte

Short

short

Float

float

4.2Integer

字符串转int

static int Integer.parseInt(String str);

转换为字符串

String str=String.valueOf(其他数据类型)

int转Integer 装箱

Integer i=new Integer(10);

Integer转int 拆箱

i.intValue();

jdk1.5之后可以自动装箱和拆箱

4.3compare()方法

Integer的

源码:
public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }

Double 的

    public static int compare(double d1, double d2) {
        if (d1 < d2)
            return -1;           // Neither val is NaN, thisVal is smaller
        if (d1 > d2)
            return 1;            // Neither val is NaN, thisVal is larger

        // Cannot use doubleToRawLongBits because of possibility of NaNs.
        long thisBits    = Double.doubleToLongBits(d1);
        long anotherBits = Double.doubleToLongBits(d2);

        return (thisBits == anotherBits ?  0 : // Values are equal
                (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                 1));                          // (0.0, -0.0) or (NaN, !NaN)
    }

byte常量池,只要是Integer在-128-127之内,不会创建新的对象.

5.异常

5.1异常产生过程


注意:只要异常对象被抛出,下面的代码都不执行.

5.2异常处理方式1

5.2.1 try--catch

try{
	//可能出现异常的代码
}catch(异常的类名   变量名){
    //处理异常的方式,一般打印异常的信息
}

5.2.2图解


5.2.3多catch异常捕获

try{
	//要处理的代码块
}catch(范围小的异常){
	//如何处理该异常
}catch(范围大的异常){
	//如何处理该异常
}

多catch处理异常 多个异常之间如果没有继承关系,就没有顺序.但是如果有继承关系,先catch子类,后子类,


5.2.4try{}catch(){}finally{}

无论是否有异常,都会执行.finally 无论如何都会执行,遇到return也是如此,除非System.exit(0),停止虚拟机.

 public static void main(String[] args) {

        try {
            double div = div(1, 0);
            System.out.println(div);
        }catch (ArithmeticException e){
            System.out.println("除数不能为0");

        }finally {
            System.out.println("吔屎啦梁非凡");
        }
        System.out.println("hehe ");
    }
    public static double div(double a,double b){
        return a/b;
    }

5.3异常的处理方式2 throw声明 thwors抛出

自己抛异常 throw new 异常对象();

throw在方法中使用

throws在方法的声明上使用 后面写异常的类名可以多个,多个用逗号隔开,不分顺序

在使用trycatch处理时,也要捕获多个异常,也可以捕获exception.

public class Demo2 {
    public static void main(String[] args) {

        try {
            method("刘醒");
        } catch (FileNotFoundException e) {
            System.out.println("吔屎了梁非凡");
        } catch (RuntimeException e) {
            System.out.println("刘醒你gay住");
        }
    }
    public static void method(String str) throws FileNotFoundException, RuntimeException {
        if (str.equals("梁非凡")) {
            throw new FileNotFoundException();
        }
        if (str.equals("刘醒")) {
            throw new RuntimeException();
        }
        if (str.equals("")) {
            System.out.println("无异常");
        }
    }
}

5.4编译时异常和运行时异常

编译时异常

必须处理,不处理代码编译不通过,代码无法执行.

运行时异常

编译时不会报错,运行时才报错,可以处理也可以不处理. 推荐不处理

因为一般情况下出现运行异常 都是代码写的有问题,需要改代码.

5.5编译时异常变为运行时异常并抛出


5.6 Throwable

方法 获取异常信息的三种方式

String toString()

String getMessage()

5.7有异常后方法重写注意事项

1.父类方法声明了异常,子类重写方法可以声明异常,但是 声明的异常必须小于等于父类声明的异常(编译时异常)

2.父类方法没有声明异常

子类重写方法也不能声明异常.如果子类方法中有异常,只能自行try catch进行处理.

5.6自定义异常

如果继承Exception就是编译时异常

如果继承RuntimeeException就是运行时异常


6.正则表达式

1.字符匹配

单字符匹配

正则表达

匹配

任意字符

任意字符

\

\

\n

匹配换行

\t

匹配制表符

2.字符集匹配

可以从里面任选一个字符

正则

匹配

[abc]

a,b,c任意一个字符

[^abc]

不能是abc任意一个

[a-zA-Z]

由任意一个字母组成,不区分大小写

[0-9]

由一位数字所组成

3.简化的字符集

正则

匹配

.

任意 的一个字符

\d等价于[0-9]

由一位数字所组成

\D等价于0-9

不是任意一个数字

\s

匹配空格,换行,制表符

\w等价于[a-zA-Z_0-9]

匹配 字母 数字 下划线

\W等价于a-zA-Z_0-9

匹配非 字母数字 下划线

4.边界匹配

正则

匹配

^

匹配边界开始

$

匹配边界结束

5.数量表述

正则

匹配

表达式?

0次或者1次

表达式*

0次1次或多次

表达式+

至少出现一次

表达式{n}

表达式的长度正好为n次

表达式{n,}

表达式的长度为n次以上

表达式{n,m}

长度在n-m次



6.逻辑表达式



表达式X表达式Y


表达式X|表达式Y

匹配任意一个表达式X或者Y

(X)

为表达式设置一个整体描述



发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言