正则表达式、常用类、String面试题

正则表达式
是一个特殊的字符串,用于检测其他字符串是否合法
boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。
字符串对象调用,谁调用这个方法,谁就比配后面给定的正则表达式
看 StringMatches


String
面试题1
看StringDemo
总结:
1.使用"创建字符串直接常量",在编译时期就能确定其值,已经存储到常量池
例子:String str4 = "AB" + "CD";
2.使用new创建在堆中,引用的是堆中的地址
例子: String str2 = new String("ABCD");
3.只使用字符串量例如 "ab"+"cd",编译优化,就会认为这是一个字符串常量"abcd"
4.若使用了变量或方法来存储字符串,使用变量或方法操作当前字符串,编译时期是无法确定其值的,只有在运行是才能确定
并且底层使用的是StringBuilder来进行拼接的 --> StringBuilder是通过new关键字创建

面试题2:
在开发中会定义 static final String str = "ab"
好处是在编译阶段就会存到常量池中,fianl修饰只能赋值一次,它不会影响内存分配
ps:
static --> static修饰方法(静态方法) static 变量(静态变量) static修饰内部类(静态内部类) static代码块(静态代码)
1.优先加载到内存中,随着类的加载加载
2.static修饰变量 所有对象共享的
class A{
static String name;
int age;
}
A a1 = new A();
a1.name = "张三";
a1.age = 20;
A a2 = new A();
System.out.println(a2.name);//张三
System.out.println(a2.age);//0
看StringDemo2

常用类:
数据类:
Math:数学类
BigDecimal:小数(精确---> 电信金融)
BigInteger:大整数(7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777)
DecimalFormat:格式化数据
NumberFormat:格式化数据
看NumberClass

System类和Runtime类 --> JVM
System:封装了一些与当前os(系统)进行信息交互的工具方法
static InputStream in
“标准”输入流。
static PrintStream out
“标准”输出流。
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
static long currentTimeMillis()
返回以毫秒为单位的当前时间。
static void exit(int status)
终止当前正在运行的 Java 虚拟机。
static void gc()
运行垃圾回收器。
static void setIn(InputStream in)
重新分配“标准”输入流。
static void setOut(PrintStream out)
重新分配“标准”输出流。
每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。
应用程序不能创建自己的 Runtime 类实例。
若需要创建Runtime实例
例子: Runtime rt = Runtime.getRuntime();
看System

Random类
是一个随机数类 --> Math.random()
Random类是一个伪随机数类(只要相同的种子,就能产生相同的随机数)
无参构造 Random() --> 使用的是默认种子
有参构造 Random(long seed)了根据指定种子来计算随机数
ps:这个种子不是你随机的范围

Java7中提供一个Random子类,在多线程并发访问的前提下是线程安全
ThreadLocalRandom-->创建对象
看Ramdom

/**
 * 
 */
package com.qfedu.Day13.FacetoFace;

public class StringDemo {
         public static void main(String[] args) {
             //下面语句中创建了多少个对
             //最多1个,最少一个都不创建
             /*直接创建在池中的,所以会创建"ABCD" 一个
              * 若池有"ABCD",一个都不创建会引用之前创建好的那个字符串 
              */
             String str1 = "ABCD";
             //最少1个 最多2个
             /*
              * 若池有"ABCD",一个都不创建会引用之前创建好的那个字符串
              * 只会在堆中开辟地址,然后将"ABCD"存在堆中
              * 
              * 若池中没有就会先创建一个"ABCD",然后后创堆中
              */
             String str2 = new  String("ABCD");
             //常量字符串进行的拼接,底层会做自动优化 看做是一个整体 进行优化编译
             String str3 = "A"+"B"+"C"+"D"; // "ABCD"
             String str4 = "AB" + "CD"; //"ABCD"
             System.out.println(str1 == str2);//false
             System.out.println(str1 == str3);//true  
             System.out.println(str1 == str4);//true
             
             //String str5 = (new StringBuilder(String.valueOf(str5_1))).append(str5_2).toString();
             //变量在操作字符串拼接的时候,在编译阶段期间是无法确定其值是什么
             //只有在运行期间,变量才会得到真正的值,str5所的到的空间就会被改变为堆空间
             String str5_1 = "AB";
             String str5_2 = "CD";
             String str5 = str5_1 + str5_2;
             System.out.println(str1 == str5);//false 3
             //方法在编译阶段是无法确认,调用方法的时候,这样做就和将字符串存储到变量效果是一样
             //    String str6 = (new StringBuilder(String.valueOf(getString()))).append("CD").toString();
             String str6 = getString() + "CD";
             System.out.println(str1 == str6);// false  
             
        }
         
         public static  String getString() {
             return "AB";
         }
           
         
}


/**
 * 
 */
package com.qfedu.Day13.FacetoFace;

public class StringDemo2 {
     public static final String A = "ab";
     public static final String B = "cd";
     //可以给static final进行赋值
     public static final String C;
     public static final String D;
     static {
         C = "ab";
         D = "cd";
     }
     
     public static void main(String[] args) {
         String str = "abcd";
         String str1 = "ab";
         String str2 = "cd";
         String str3 = str1 + str2;//new Stringbuilder进行拼接 --> 堆空
         System.out.println(str == str3);//false
         String str4 = "ab"+str2;//new Stringbuilder进行拼接 --> 堆空
         System.out.println(str == str4);//false
         /*
          * A和B都是常量,常量只能赋值一次
          * A和B这两个常量在声明时期就已经能确定其值了
          *在计算 A+B == "ab" + "cd" --> 指向的位置其实就和str一致了
          */
         String str5 = A + B;//"ab"+"cd"
         System.out.println(str == str5);//true
         /*
          * C和D都是常量,没有任何问题,只不过使用静态代码块,进行初始化赋值
          * 在编译时期C和D没有能力确定值什么
          * str6 就相当于 使用new Stringuilder来创建的 也就是存储在堆中
          */
         String str6 = C + D; //"ab"+"cd"
         System.out.println(str == str6);//false
 
    }
}
/**
 * 
 */
package com.qfedu.Day13.NumberClass;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Demo {
    public static void main(String[] args) {
        //Math数学类:这里封装了一些常用的数据公式,Math是一个工具类里面的方法都是static方法
        //ps:大数据--> 数学,应用数学+计算机
        //求次方法
        double max = Math.pow(2, 31)-1;
        //求随机数
        double random = Math.random()*100;
        //比较两个数随最大或最小
        System.out.println(Math.max(1, 2));
        System.out.println(Math.min(1, 2));
        //求绝对值
        System.out.println(Math.abs(-1));
        //开平方
        System.out.println(Math.sqrt(4));
        //定义PI
        System.out.println(Math.PI);

        //BigDecimal(精确)
        //double是一个不精确存的(1/3乘以3是一个无线接近于1的值)
        System.out.println("0.09+0.01="+(0.09+0.01));
        System.out.println("1.0-0.33="+(1.0-0.33));
        System.out.println("4.015*1000="+(4.015*1000));
        System.out.println("12.3/100="+(12.3/100));
        //不能精确的计算结果
        BigDecimal num1 = new BigDecimal("0.09");
        BigDecimal num2 = new BigDecimal("0.01");
        //System.out.println(num1+num2);
        //已经将数据封装到了BigDecimal中所以我们在次操作数据的时候,就是在操作两个对象
        //所以不能直接使用运算符进行计算
        //BigDecimal中已经封装好了对应的计算方法
        System.out.println(num1.add(num2));
        
        //BigInteger
        BigInteger num3 = 
                new  BigInteger("7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
        
        BigInteger num4 = new BigInteger("7");
        //mod求余数
        if((num3.mod(num4)).toString().equals("0")) {
            System.out.println("能被整除");
        }else {
            System.out.println("不能被整除");
        }
        
        //格式化数据
        //static NumberFormat getInstance()  返回当前默认语言环境的通用数值格式。 
        NumberFormat nf = NumberFormat.getInstance();
        //只能帮助格式化16位
        System.out.println(nf.format(1234567891111111.0));
        //有一个构造方法-->可以传入一个字符串 ,自定义格式   
        DecimalFormat df = new DecimalFormat();
        System.out.println(df.format(1234567891111111.0));
        
    }

}
/**
 * 
 */
package com.qfedu.Day13.Ramdom;

import java.util.Arrays;
import java.util.Random;

public class RandomDemo {

    public static void main(String[] args) {
        // 伪随机
        Random r1 = new Random(10);
        System.out.println(r1.nextBoolean());//boolean类型
        System.out.println(r1.nextDouble());//double类型
        System.out.println(r1.nextInt());//int类型
        //确定范围的随机
        //可以传入参数,这个参数就是随机的范围[0,1)-->[0,100) 0~99
        System.out.println(r1.nextInt(100));
        
        System.out.println("----------------------------------------------");
        
        Random r2 = new Random(10);
        System.out.println(r2.nextBoolean());//boolean类型
        System.out.println(r2.nextDouble());//double类型
        System.out.println(r2.nextInt());//int类型
        //确定范围的随机
        //可以传入参数,这个参数就是随机的范围[0,1)-->[0,100) 0~99
        System.out.println(r2.nextInt(100));
        System.out.println("----------------------------------------------");
        //正常随机
        //ps:默认种子是当前时间currentTimeMillis()
        
        Random r3 = new Random();
        int[] array = new int[10];
        for(int i = 0;i<array.length;i++) {
              array[i] = r3.nextInt(100);
            
        }
        System.out.println(Arrays.toString(array));
        
        //生成34-179之间的随机数
        //nextInt(145)+34;


    }

}
/**
 * 
 */
package com.qfedu.Day13.StringMatches;

public class StringMatchesDemo {
    public static void main(String[] args) {
        String name = "我是谁";
        boolean result = name.matches("[我是谁]");
        //[我是谁] --> 我|是|谁
        System.out.println(result);
        boolean result2 = "我".matches("[我是谁]");
        System.out.println(result2);
        //判断字符串9是不是数字
        String number = "9";
        // \d 数字 \w 字母 数字 下划线 \s 回车 空格等空白符
        
         boolean result3 = number.matches("\\d");
         System.out.println(result3);
         
          //匹配邮箱: 字母数字_@字母数字.字母出现的次数 .com
          String regx = "\\w+@\\w+(\\.\\w+){1,2}";
          String email = "[email protected]";
          boolean result4 = email.matches(regx);
          System.out.println(result4);
          
          //匹配手机号  131 2 3 4 5 6 7 8 9   XXXX   XXXX
          //          14 7  
          //          16 6
          //          15 1 2 3 4 5 6 7 8 9
          //          17 1
          //          18 1 2 3 8
          //前两位是  1位是变化  9位是固定的
         String numberPhone = "1[3|4|5|6|7|8]\\d{9}";
         String str = "11111111111";
         System.out.println(str.matches(numberPhone));
                
    }

}

猜你喜欢

转载自www.cnblogs.com/lijun199309/p/9485261.html