java 12 包装类、正则表达式、常用类

包装类

java为我们更加方便的去操作这些基本数据类型。针对每种基本数据类型,都提供了他的一个包装类型(引用类型)

基本类型 包装类型

byte Byte

short Short

int Integer

long Long

float Float

double Double

char Character

boolean Boolean

​ Integer 类在对象中包装了一个基本类型 的值int 。Integer 类型的对象包含一个 int 类型的字段。
​ 此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
还提供了处理 int 类型时非常有用的其他一些常量和方法。

Integer( int value)

int num=100;
Integer integer = new Integer(num);

​ 构造一个新分配的 Integer 对象,它表示指定的 int 值
Integer(String s)

Integer integer2 = new Integer("200");

​ 构造一个新分配的 Integer 对象,它表示String 参数所指示的 int 值

String和int类型的相互转换

int ===> String:

int num=100;

1.String str=num+"";

2.String s=String.valueOf(num);

3.Integer integer=new Integer(num);

String s1=integer.toString();

String ===> int:

String strNum=“100”;

1.Integer integer1 = new Integer(strNum);
int i = integer1.intValue();

2.int i1 = Integer.parseInt(strNum);

  • JDK1.5之后引入的自动拆装箱的机制。

    自动装箱:将基本数据类型转换成它所对相应的包装类型。

    int num=10;
    //自动装箱
    Integer numInteger=num;
    
    //手动装箱 Integer.valueOf(num);
    Integer integer = Integer.valueOf(num);
    

    自动拆箱:将包装类型自动转换成它所对应的基本数据类型。

    Integer a = new Integer(100);
    Integer b = new Integer(100);
    //自动拆箱
    int r=a+b;
    System.out.println(r);
    
    //手动拆箱 intValue();
    
    Integer c = new Integer(100);
    Integer d = new Integer(100);
    //手动拆箱
    int i = c.intValue();
    int i1 = d.intValue();
    System.out.pr
    intln(i+i1);
    

    ​ Integer 重写了i1.equals(i2)方法,比较的是,他包装的这个值是否相同
    ​ Integer 重写了toString()方法,把他包装的这个int类型的数据,转换成字符串。

正则表达式

例:校验qq号

​ 1.需要必须是5-15位数字

​ 2.0不能开头

在这里插入图片描述

正确规则的表达式,很多语言都支持。

作用;校验数据,看符不符合

  • 定义正则表达式

    String regx = "a";
    regx = "[a,b,c]"; //是我列举的某一个
    regx = "[a-z]"; //列举26个字母
    regx = "[A,B,C,D]";
    regx = "[A-Z]";
    regx = "[a-zA-Z]";
    regx = "[0,1,2,3]";
    regx = "[0-9]";
    regx = "[a-zA-Z0-9]";
    regx = "[^0-9]"; //^不是列表中的某一个
    regx = ".";//配置单个任意字符。
    regx = "\\.";   // \ 转义字符
    regx = "|"; // | 或者
    regx = "\\|";
    regx = "\\d"; // 跟 [0-9] 这个的意思一样   \D 非数字: [^0 - 9]
    regx = "\\w"; // 跟 [a-zA-Z_0-9] 这个意思一样  \W 非单词字符:[^\w]
    regx = " ";
    regx = "\\s"; //空格  \S 非空白字符:[^\s]
    regx = "abc$"; //以abc结尾
    regx = "^abc"; // 以abc开头
    regx = "[0-9]+"; //0-9 + 可以出现一个或多个
    regx = "[a-z]*"; //0-9 * 0个或多个 1个也算多个  空串算0个
    regx = "[A-Z]?"; // ? 一个或 0个
    regx = "\\w+";
    regx = "[a-zA-Z0-9_]+";
    regx = "[a-z]{6}"; //正好几个
    regx = "[a-zA-Z]{6,}"; //至少6个
    regx = "[a-zA-Z]{6,16}"; //大于等于6 小于等于16
    regx="[\\u4e00-\\u9fa5]"//校验中文正则
    
  • 上述例题用正则表达式表示

在这里插入图片描述

  • 用正则切割字符串,返回一个字符串数组

    String names2 = "张三assdfa=12542242assadfafasdf2411李四asdfdadsf25244=asdfasdf25411王五"; //
    String[] arr = names2.split("[a-z=0-9]+");
    System.out.println(arr[0]); //张三
    System.out.println(arr[1]); //李四
    System.out.println(arr[2]); //王五
    
常用类
System.out.println(Math.PI); //π
System.out.println(Math.E);  //e
System.out.println(Math.abs(-1)); //1 绝对值
System.out.println(Math.ceil(3.1)); //4.0 向上取整
System.out.println(Math.floor(3.1));  //3.0 向下取整
System.out.println(Math.random());  //0.5214156547885063 生成随机数
System.out.println(Math.max(3.1, 36.2)); //36.2 取两个数的最大值
System.out.println(Math.max(Math.max(3.1, 36.2),36)); //36.2 取三个数的最大值
System.out.println(Math.min(3.3, 68)); //3.3 取最小值
System.out.println(Math.pow(3, 3)); //27.0 n的n次方
System.out.println(Math.sqrt(4)); //2.0 n的平方根
System.out.println(Math.round(3.5)); //4 四舍五入
System.out.println(Math.pow(8, 1/3.0)); //2.0 n的m次方根
  • Random

    Random()
    创建一个新的随机数生成器。
    Random( long seed)
    使用单个 long 种子创建一个新的随机数生成器。

  • 指定范围来生成随机数

    int num= random.nextInt(); //生成范围是int的随机数

  • Random random = new Random(10L);
    for (int i = 0; i < 10; i++) {
        int i1 = random.nextInt(10);
        System.out.println(i1);
        //给了种子,就会根据种子算出一些随机数,每次重新运行,生成的随机数就不变了。
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值