15、正则表达式 包装类 自动装包和拆包 BigDecimal

1、正则表达式

1.1、常用正则表达式

image-20210715185606163.

1.2、案例:验证身份证

String regex="[0-9]{17}[0-9X]";

regex.matches(input);

package cn.tedu.api;
/*本类用于测试正则表达式*/

import java.util.Scanner;

public class TestRegex {
    public static void main(String[] args) {
        method();

    }

    private static void method() {
        System.out.println("请输入您的身份证号:");
        String input = new Scanner(System.in).nextLine();
//        String regex="[0-9]{17}[0-9X]";
        /*单个\在java中有特殊的意义,会认为这是一个转义字符
           \n换行,\r回车
          想表示是\就需要再加一个\
        * */
        String regex="\\d{17}[0-9X]";
        if (regex.matches(input)){
            System.out.println("恭喜您输入正确!");
        }else {
            System.out.println("输入不正确!请重新输入");
            method();
        }
    }
}

2、包装类

2.1、基本类型对应的包装类

image-20210715185921223

2.2、Integer包装类

1、两种创建方式:

Integer i=new Integer(124);

Integer i2=Integer.valueOf(124);
Integer i3=Integer.valueOf(124);
System.out.println(s2==s3);//true

Integer i4 = Integer.valueOf(300);//超过了缓存的大小
Integer i5 = Integer.valueOf(300);
System.out.println(i4==i5);//false

2、只有Integer的valueOf()才有高效的效果,其他类型没有;高效性:i3引用i2创建的数据

注意:Integer类中包含256个Integer缓存对象,数据范围是 -128~127,i4,i5超过了范围

3、Stirng类型转换为指定类型的方式

System.out.println(i1.parseInt("600")+10);//使用Integer的对象i1,调用parseInt(),将String转为int

System.out.println((d1.parseDouble("3.14") + 1));//使用Double的对象d1,调用parseDouble(),将String转为double

4、以后用Integer定义会多一点,因为Integer除了可以赋值,还可以使用Integer类里的许多方法

3、自动装箱和自动拆箱

3.1、自动装箱:

把基本类型int自动包装成对应的包装类Integer

Integer i=127;
3.2、自动拆箱:

把包装类型Integer自动拆箱成对应的基本类型int

Integer i=Integer.valueOf(124);
int a=i;

4、BigDecimal(浮点数计算的精度)

4.1、创建的方式
BigDecimal bd1 = new BigDecimal(a+"");//把double类型的a转为String

常用String类型的参数,传Double类型的参数会有精度问题

4.2、常用方法
bd1.add(bd2);//相加
bd1.subtract(bd2);//相减
bd3=bd1.multiply(bd2);//相乘
/*divide(对象,保留位数,舍入方法)*/
bd3=bd1.divide(bd2,3,BigDecimal.ROUND_HALF_UP);//相除  BigDecimal.ROUND_HALF_UP四舍五入
package cn.tedu.bigdecimal;
/*本类用作浮点数运算不精确解决方案*/

import java.math.BigDecimal;
import java.util.Random;
import java.util.Scanner;

public class TestBigDecimal {
    public static void main(String[] args) {
//        f1();
        f2();
    }

    private static void f2() {
        System.out.println("请输入您要计算的第一个小数:");
        double a=new Scanner(System.in).nextDouble();
        System.out.println("请输入您要计算的第二个小数:");
        double b=new Scanner(System.in).nextDouble();
        /*1.最好不要使用double作为构造函数的参数,不然还是会有计算不精确的现象
          2.一般使用String类型作为构造函数的参数
          3.double转String拼接个""就可以
        * */
        BigDecimal bd1 = new BigDecimal(a+"");//把double类型的a转为String
        BigDecimal bd2 = new BigDecimal(b+"");
        BigDecimal bd3;//用来保存结果
        bd3= bd1.add(bd2);
        System.out.println(bd3);
        bd3=bd1.subtract(bd2);
        System.out.println(bd3);
        bd3=bd1.multiply(bd2);
        System.out.println(bd3);

        /*divide(对象,保留位数,舍入方法)*/
        bd3=bd1.divide(bd2,3,BigDecimal.ROUND_HALF_UP);
        System.out.println(bd3);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值