Java学习笔记——Java语言基础(十二)(包装类(Integer类)、正则表达式)

一、包装类

1.为了对基本数据类型进行更多的操作,Java针对于每一种基本数据类型提供了相对应的类
基本类型和包装类对应

 基本类型				包装类
	byte 			Byte
	short			Short
	int				Integer
	long			Long
	float			Float
	double		    Double
	char			Character
	boolean		    Boolean
1.1 Integer类

1.Integer类在对象中包装了一个基本类型int的值
2.类中存在多个方法,可以实现int与String类型之间互相转换
3.构造方法

	public Integer(int value)
	public Integer(String s)
public class Test01 {
    public static void main(String[] args) {
        // 构造方法 public Integer(int value)
        Integer integer = new Integer(100);
        //将100转换为2进制
        String s = integer.toBinaryString(100);
        System.out.println(s);//1100100
        //将100转换为8进制
        String s2 = integer.toOctalString(100);
        System.out.println(s2);//144
        //将100转换为16进制
        String s1 = integer.toHexString(100);
        System.out.println(s1);//64
        //判断一个数是否在int范围内
        if (200000>=Integer.MIN_VALUE&&200000<=Integer.MAX_VALUE){
            System.out.println("在范围内");
        }
        //构造方法 public Integer(String s) String参数所指示的int值
        Integer integer1 = new Integer("100");
        System.out.println(integer1);
    }
}

4.成员方法

public int intValue() 拆箱,将Integer转化为整型
public static int parseInt(String s) 将字符串转换为int类型
public static String toString(int i) 将整型转换为字符串
public static Integer valueOf(int i) 将整型装换为Integer类型
public static Integer valueOf(String s) 将字符串转换为Integer类型
public class Test02 {
    public static void main(String[] args) {
        //int --String
        int num=10;
        String s = num + "";
        String s1 = String.valueOf(num);
        Integer integer = new Integer(num);
        //将整形转换为字符串
        String s2 = integer.toString();
        //String-- int
        String strnum="100";
        Integer integer1 = new Integer(strnum);
        //将Integer类型转换为整形
        int i = integer1.intValue();
        //字符串转换为int类型
        int i1 =Integer.parseInt(strnum);

        byte b = integer.byteValue();
        System.out.println(b);//10
        short i2 = integer.shortValue();
        System.out.println(i2);//10
        float i3=integer.floatValue();
        System.out.println(i3);//10.0
    }
}

5.自动装箱和拆箱
自动装箱:把基本类型转换为包装类类型
自动拆箱:把包装类类型转换为基本类型

public class Test03 {
    public static void main(String[] args) {
        int num=100;
        //自动装箱 int--->Integer
        Integer num2=num;
        Integer integer = new Integer(30);
        //自动拆箱
        int sum=num2+integer;
        System.out.println(sum);//130

        Integer a=10;//自动装箱
        a+=20;//自动拆箱 自动装箱
    }
}
public class Test04 {
    public static void main(String[] args) {
        Integer num1=60;// Integer num1=Integer.valueOf(60);
        int num2=60;
        Integer num3 = Integer.valueOf(60);
        Integer num4 = new Integer(60);//new 的地址值
        //num1.intValue()
        System.out.println("num1==num2"+(num1==num2));//num1==num2true
        System.out.println("num1==num3"+(num1==num3));//num1==num3true
        System.out.println("num1==num4"+(num1==num4));//num1==num4false
        System.out.println("num3==num4"+(num3==num4));//num3==num4false
        //num4.intValue()
        System.out.println("num2==num4"+(num2==num4));//num2==num4true

        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);//false
        System.out.println(i1.equals(i2));//true

        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4);//false
        System.out.println(i3.equals(i4));//true
        //自动装箱
        Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6);//false
        System.out.println(i5.equals(i6));//true
	    //自动装箱
        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8);//true
        System.out.println(i7.equals(i8));//true
    }
}

补充:

1. int 和Integer在进行比较的时候,Integer会进行拆箱,转为int值与int进行比较。
2. Integer与Integer比较的时候,由于直接赋值的时候会进行自动的装箱,那么这里就需要注意两个问题,一个是-128<= x<=127的整数,将会直接缓存在IntegerCache中,那么当赋值在这个区间的时候,不会创建新的Integer对象,而是从缓存中获取已经创建好的Integer对象。二:当大于这个范围的时候,直接new Integer来创建Integer对象。
3. new Integer(1) 和Integer a = 1不同,前者会创建对象,存储在堆中,而后者因为在-128到127的范围内,不会创建新的对象,而是从IntegerCache中获取的。那么Integer a = 128, 大于该范围的话才会直接通过new Integer(128)创建对象,进行装箱。

Integer i1 = new Integer(128);
Integer i2 = 128;
i是创建的一个Integer的对象,取值为128
i2是一个自动装箱,由于取值超过范围,所以创建一个新的Integer的对象。

两个都是Integer的对象,存放在堆中,分配的地址值不同

Integer i3 = new Integer(127);
Integer i4 = 127;
i3是创建的一个Integer的对象,取值127
i4是自动装箱后的一个Integer的对象,在取值范围内,i4对象直接取缓存IntegerCache中的对应的对象,当然了也是对象。

i3和i4也都是对象,一个在缓存中,一个new在堆中,比较地址值不同

Integer i5 = 128;
Integer i6 = 128;
i5和i6都是自动装箱产生的对象,并且两个都超过了范围,都需要直接创建对象,new Integer(128)

两个都是new出来的对象,在堆中的地址值是不同的。

Integer i7 = 127;
Integer i8 = 127;
i7、i8是自动装箱产生的对象,其值都是127,并且取值都在范围内,那么会在缓存中查找,自然对象为同一个,地址值相同
1.2 其他基本类型的包装类

1.Character类(char类型对于的包装类)
构造方法:public Character(char value)
成员方法
public static boolean isUpperCase(char ch) 判断字符是否是大写
public static boolean isLowerCase(char ch) 判断字符是否是小写
public static boolean isDigit(char ch) 判断字符是否是数字
public static char toUpperCase(char ch) 将字符转大写
public static char toLowerCase(char ch) 将字符转小写

public class Test01 {
    public static void main(String[] args) {
        //构造方法
        Character c1 = new Character('c');
        Character c2='a';
        //字节数
        System.out.println(Character.BYTES);//2
        //最大值
        System.out.println((int) Character.MAX_VALUE);//65535
        //最小值
        System.out.println((int) Character.MIN_VALUE);//0
        //二进制位数
        System.out.println(Character.SIZE);//16
        //判断字符是否是大写
        System.out.println(Character.isUpperCase('A'));//true
        //判断字符是否是小写
        System.out.println(Character.isLowerCase('a'));//true
        //判断字符是否是数字
        System.out.println(Character.isDigit('8'));//true
        //将字符转大写
        System.out.println(Character.toUpperCase('a'));//A
        //将字符转小写
        System.out.println(Character.toLowerCase('A'));//a
    }
}

2.基本类型的包装类的简单方法

public class Test01 {
    public static void main(String[] args) {
        //Long类的构造方法
        Long aLong1 = new Long(2000L);
        Long aLong2 = new Long("2000");
        long a=20000;
        //long---->Long
        Long aLong3 = Long.valueOf(a);
        String s="200000";
        //String--->long
        long l = Long.parseLong(s);

        //Byte
        byte b=20;
        Byte aByte = new Byte(b);
        System.out.println(aByte);
        //byte---->Byte
        Byte aByte1 = Byte.valueOf((byte) 50);
        //String---->byte
        byte b1 = Byte.parseByte("5");

        //Boolean
        boolean flag=false;
        Boolean aBoolean1 = Boolean.valueOf(flag);
        String s1="true";
        boolean b2 = Boolean.parseBoolean(s1);
    }
}

二、正则表达式

1.正则表达式:一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串
2.校验QQ号:

import java.util.Scanner;
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入正确的QQ号");
        String s = sc.nextLine();
        boolean b = cheakQQnumber(s);
        System.out.println(b);
    }
    private static boolean cheakQQnumber(String string){
        /* 需求:校验qq号码.
        1:要求必须是5 - 15 位数字
        2:0 不能开头*/
        boolean flag=false;
        if (string.length()>=5&&string.length()<=15&&!string.startsWith("0")){
            for (int i = 0; i < string.length(); i++) {
                char c = string.charAt(i);
                if (!Character.isDigit(c)){
                    flag=false;
                    break;
                }else {
                    flag=true;
                }
            }
        }else {
            flag=false;
        }
        return flag;
    }
}

使用正则表达式的语句:

import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入正确的QQ号");
        String s = sc.nextLine();
        boolean b = cheakQQnumber(s);
        System.out.println(b);
    }
    private static boolean cheakQQnumber(String string) {
        String regx="[1-9][0-9]{4,14}";
        boolean matches = string.matches(regx);
        return matches;
    }
}

3.校验手机号:

import java.util.Scanner;
public class Test03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入正确的手机号");
        String s = sc.nextLine();
        boolean b = cheakPhonenumber(s);
        if (b){
            System.out.println("手机号规则正确");
        }else {
            System.out.println("手机号规则不正确");
        }
    }
    private static boolean cheakPhonenumber(String phoneNumber) {
        boolean flag=false;
        /*
        //手机号码的规则;位数 11位  全部都是数字
        //以 1开头  第二位 3 5 6 7 8 9  其余9位随意数字  138
         */
        if(phoneNumber.length()==11&&phoneNumber.startsWith("13")||phoneNumber.startsWith("15")|| phoneNumber.startsWith("16")||phoneNumber.startsWith("17")|| phoneNumber.startsWith("18")||phoneNumber.startsWith("19")){
            for (int i = 2; i < phoneNumber.length(); i++) {
                char c = phoneNumber.charAt(i);
                if (!Character.isDigit(c)){
                    flag=false;
                    break;
                }else {
                    flag=true;
                }
            }
        }else {
            flag=false;
        }
        return flag;
    }
}

使用正则表达式

import java.util.Scanner;

public class Test04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入正确的手机号");
        String s = sc.nextLine();
        boolean b = cheakPhonenumber(s);
        if (b){
            System.out.println("手机号规则正确");
        }else {
            System.out.println("手机号规则不正确");
        }
    }

    private static boolean cheakPhonenumber(String phoneNumber) {
        String regx="[1][3,5,6,7,8,9,][0-9]{9}";
        boolean matches = phoneNumber.matches(regx);
        return matches;
    }
}
2.1 正则表达式的组成规则
String regx="a";  就表示字符'a'
"[a,b,c,d,e]"; //列表中的任意一个即可
"[a-z]"; //列举了26个小写字母
"[1,2,3]";//列表中的任意一个
"[0-9]";//数字0-9
"[A-Z]";//26个大写字母
"[a-zA-Z0-9]";//小写字母 数字 大写字母
"[^A-Z]"; //取反 出来大写字母
 "."; //. 匹配单个任意字符
"\\."; //  \\ 转义符  表示“.”
"|"; //   | 或者
"\\|";表示“|”
"&"; //且
 ".."; //
"\\d";  // 这个等同于 [0-9]
"\\w"; //等同于 [a-zA-Z_0-9]
"\\s"; //匹配空格字符
量词
"a?";//  ? 一次或一次也没有  空串就是没有出现
"[a-z]*";//*,零次或多次  一次也算多次
"[0-9]+";//+ 一次或多次
"[0-9]{n}";//恰好n次
"[a-z]{n,}";//至少n次
 "[a-z]{n,m}";//至少n次,至多m次

2.String类中正则表达式的方法

public class Test05 {
    public static void main(String[] args) {
        String number="13=14=15=16";
        //根据正则表达式截取字符串,返回一个字符串数组
        String regx="=";
        String[] strings = number.split(regx);
        for (int i = 0; i < strings.length; i++) {
            System.out.print(strings[i]+" ");//13 14 15 16
        }
		//分割功能
        String username="张三=assdfadwefsdf232323李四=assfasdfas4444asdfasdfasdd4f王五";
        String regx2="[=0-9a-z]+";
        String[] strings2 = username.split(regx2);
        for (int i = 0; i < strings2.length; i++) {
            System.out.println(strings2[i]);//张三 李四 王五
        }
    }
}

练习

import java.util.Arrays;

public class Test06 {
    public static void main(String[] args) {
        //我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
        String s1 = "91 27 46 38 50";
        String[] strings = s1.split("\\s+");
        System.out.println(Arrays.toString(strings));//[91, 27, 46, 38, 50]
        //转换为int类型的数组
        int[] ints = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            ints[i]=Integer.parseInt(strings[i]);
        }
        Arrays.sort(ints);
        //创建一个StringBuffer对象
        StringBuffer stringBuffer = new StringBuffer();
        //遍历int数组
        for (int i = 0; i < ints.length; i++) {
          stringBuffer = stringBuffer.append(ints[i]).append(" ");
        }
        String s = stringBuffer.toString();
        System.out.println(s);
    }
}
2.2 正则表达式的替换功能

String类的功能:public String replaceAll(String regex,String replacement)

public class Test07 {
    public static void main(String[] args) {
        //String类的替换
        String str="aa-bb-c--dd";
        String replace = str.replace("-", "");
        System.out.println(replace);//aabbcdd
        //正则表达式
        String str2="aa-2412bb4124-4cc3--2dd3";
        String s = str.replaceAll("[-0-9]+", "&");
        System.out.println(s);//aa&bb&cc&dd
    }
}
2.3 Pattern和Matcher类

1.模式器 Pattern 用来封装一个正则表达式
2.匹配器 Matcher 获取匹配器之后,传入一个待匹配的数据

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test08 {
    public static void main(String[] args) {
        //封装一个正则表达式
        Pattern compile = Pattern.compile("[0-9]+");
        //通过模式器,获取匹配器,传入一个待匹配的数据
        Matcher matcher = compile.matcher("15484122");
        boolean matches = matcher.matches();
        System.out.println(matches);

        String str="iu uuu whhh wggsaoj su huh hhuw hui kos";
        Pattern compile1 = Pattern.compile("\\b[a-z]{3}\\b");
        Matcher matcher1 = compile1.matcher(str);
        //尝试查找与该模式匹配的输入序列的下一个子序列
        while (matcher1.find()){
       	 //返回由以前匹配操作所匹配的输入子序列。
            String group = matcher1.group();
            System.out.println(group);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值