美文网首页
Java中的常用类

Java中的常用类

作者: 王一萍o叩丁狼教育 | 来源:发表于2018-09-13 17:51 被阅读0次

BigDecimal(掌握)

float和double都不能表示精确的小数,使用BigDecimal类可以解决该问题,BigDecimal用于处理金钱或任意精度要求高的数据。
BigDecimal不能直接把赋值和运算操作,只能通过构造器传递数据,而且必须使用字符串类型的构造器,操作BigDecimal主要是加减乘除四个操作。

// 使用double类型:
System.out.println(0.09 + 0.01);// ?
// 使用BigDecimal类型double类型的构造器:
BigDecimal num1 = new BigDecimal(0.09);
BigDecimal num2 = new BigDecimal(0.01);
System.out.println(num1.add(num2));// ?
// 使用BigDecimal类型String类型的构造器:
BigDecimal num3 = new BigDecimal("0.09");
BigDecimal num4 = new BigDecimal("0.01");
System.out.println(num3.add(num4));// ?

结果为:

0.09999999999999999
0.09999999999999999687749774324174723005853593349456787109375
0.10

加减乘除操作:

BigDecimal num1 = new BigDecimal("10");
BigDecimal num2 = new BigDecimal("3.1415926");
        
BigDecimal ret1 = num1.add(num2);
BigDecimal ret2 = num1.subtract(num2);
BigDecimal ret3 = num1.multiply(num2).setScale(2, RoundingMode.HALF_UP);
BigDecimal ret4 = num1.divide(num2, 2 , RoundingMode.HALF_UP);

上述蓝色区域代码分别表示乘法和除法按照四舍五入方式保留两位小数。

随机数

Math(了解)

Math 类包含用于执行数学运算的方法,如初等指数、对数、平方根和三角函数等,该类的方法都是static修饰的,在开发中其实运用并不是很多,里面有一个求随机数的方法,偶尔会用到。

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.max(99, 10));// 返回最大值
        System.out.println(Math.min(99, 10));// 返回最小值
        // 返回一个[0,1)之间的随机小数
        double num = Math.random();
        System.out.println(num);
        // 得到一个[0,100)之间的随机整数
        int intNum1 = (int) (num * 100);
        System.out.println(intNum1);
        //得到23~104之间的随机数等价于0~81之间随机数+23
        int intNum2 = (int)(Math.random() * 81 + 23);
        System.out.println(intNum2);
    }
}

Random(了解)

Random类用于生产一个伪随机数(通过相同的种子,产生的随机数是相同的),Math类的random方法底层使用的就是Random类的方式。

public class RandomDemo {
    public static void main(String[] args) {
        Random r = new Random();
        int intNum1 = r.nextInt(100);// 100以内随机数
        System.out.println(intNum1);
        // 随机获取A~Z之间的5个字母组成的字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            int intNum2 = 65 + r.nextInt(25);
            char ch = (char) intNum2;
            sb.append(ch);
        }
        System.out.println(sb);
    }
}

UUID (了解)

UUID表示通用唯一标识符 (Universally Unique Identifier) ,其算法通过电脑的网卡、当地时间、随机数等组合而成,优点是真实的唯一性,缺点是字符串太长了。

public class UUIDDemo {
    public static void main(String[] args) {
        //UUID随机字符串
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        //获取UUID前5个字母作为验证码
        String code = uuid.substring(0, 5);
        System.out.println(code);
        System.out.println(code.toUpperCase());//把验证码转为大写字母
    }
}

1. 字符串

字符串(字符序列),表示把多个字符按照一定得顺序排列起来。

字符串的分类(根据同一个对象,内容能不能改变而区分):

  • 不可变的字符串——String:当String对象创建完毕之后,该对象的内容是不能改变的,一旦内容改变就变成了一个新的对象。

  • 可变的字符串——StringBuilder/StringBuffer:当StringBuilder对象创建完毕之后,对象的内容可以发生改变,当内容发生改变的时候,对象保持不变。

字符串的本质是char[],char表示一个字符,char[]表示同一种类型的多个字符。

String  str = "ABCD";  等价于  char[] cs = new char[]{'A','B','C','D'};

String(掌握)

String概述

String类,表示不可变的字符串,当String对象创建完毕之后,该对象的内容是不能改变的,一旦内容改变就变成了一个新的对象,看下面代码。

String str = "龙哥";

str = "龙哥17";
image.png

String对象的创建的两种方式:

1、直接赋一个字面量:       String   str1  =  "ABCD";

2、通过构造器创建:         String   str2  =  new String("ABCD");

两种方式有什么区别,分别在内存中如何分布?

image.png

String对象的空值:

  • 表示引用为空(null) : String str 1 = null ; 没有初始化,没有分配内存空间.

  • 内容为空字符串 : String str2 = “”; 已经初始化,分配内存空间,不过没有内容

判断字符串非空:字符串不为null并且字符内容不能为空字符串(“”)

判断一个字符串非空:

public static boolean hasLength(String str) {

     return str != null && !"".equals(str.trim());

}

字符串的比较操作:

  • 使用”==”号:比较两个字符串引用的内存地址是否相同

  • 使用equals方法:比较两个字符串的内容是否相同

System.out.println("ABCD" == "ABCD");  //true

System.out.println("ABCD" == new String("ABCD"));  //false

System.out.println("ABCD".equals("ABCD"));  //true

System.out.println("ABCD".equals(new String("ABCD")));  //true

常用方法(要记住)

  • int length() 返回此字符串的字符个数

  • char charAt(int index) 返回指定索引位置的字符

  • int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引位置

  • boolean equals(Object anObject) 比较内容是否相同

  • boolean equalsIgnoreCase(String anotherString) 忽略虑大小写,比较内容是否相同

  • String toUpperCase() 把当前字符串转换为大写

  • String toLowerCase() 把当前字符串转换为小写

  • String substring(int beginIndex):从指定位置开始截取字符串

  • String substring(int beginIndex, int endIndex):截取指定区域的字符串

public class StringDemo{

    public static void main(String[] args) {

        String str = "HelloWorld";

        System.out.println(str.length());// 10

        char ch = str.charAt(3);

        System.out.println(ch);// l

        int index = str.indexOf("lo");

        System.out.println(index);// 3

        System.out.println("helloworld".equals(str));// false

        System.out.println("helloworld".equalsIgnoreCase(str));// true

        System.out.println(str.toLowerCase());// helloworld

        System.out.println(str.toUpperCase());// HELLOWORLD

        String str1 = str.substring(3);

        System.out.println(str1);// loWorld

        String str2 = str.substring(3, 6);

        System.out.println(str2);// loW

    }

}

1.2. StringBuilder(掌握)

Sting是不可变的,每次内容改变都会在内存中创建新的内存区域,如果现在要把N个字符串连接起来,此时需要在内存中创建N块内存空间,性能很低。此时要解决多个字符串做拼接性能低的问题,我们可以使用StringBuilder来搞定。

StringBuffer和StringBuilder都表示可变的字符串,功能方法相同的,区别是:

  • StringBuffer:StringBuffer中的方法都使用了synchronized修饰,保证线程安全但性能较低

  • StringBuilder:StringBuilder中的方法没有使用synchronized修饰,线程不安全但但是性能较高

开发中建议使用StringBuilder,具体用法如下:

  • 如果事先知道需要拼接多少个字符,可以在创建StringBuilder对象时指定字符数组容量,缺省为16
StringBuilder sb = new StringBuilder(40);

使用append方法在原字符串后面继续拼接字符串

sb.append("ABC").append(123).append("will");

代码:

public class StringBuilderDemo {

    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer(40);

        sb.append("abc").append("will").append(17);

        String str = sb.toString();

        System.out.println(str);  //abcwill17

    }

}

2. 日期

2.1. Date(掌握)

Date类,时期时间类,表示特定的瞬间,可以解释为年、月、日、小时、分钟和秒值。

注意:我们使用的是java.util.Date类,而不是java.sql.Date。

Date类中的大量方法都标记为已经时的,即官方不建议使用。在开发中,我们要表示日期(年月日)或时间(时分秒)类型都使用Date类来表示。

public class DateDemo {

    public static void main(String[] args) {

        java.util.Date d = new java.util.Date();

        System.out.println(d);// 欧美人的时间风格

        System.out.println(d.toLocaleString());// 本地区域时间风格

        long time = d.getTime();// 获取当前系统时间距离1970 年 1 月 1 日 00:00:00 以来的毫秒数

        System.out.println(time);

    }

}

输出结果:

Thu May 17 14:25:12 CST 2018

2018-5-17 14:25:12

1526538312866

## 2.2. **SimpleDateFormat(掌握)**

打印Date对象时,默认打印的是欧美人的日期时间风格,如果需要输出自定义的时间格式,比如2020年12月12日 12:12:12格式或者2020-12-12 12:12:12,此时可以使用SimpleDateFormat类。

SimpleDateFormat类,顾名思义是日期的格式化类,主要包括两个功能的方法:

+ 格式化(format):Date类型转换为String类型:String format(Date date)

+ 解析(parse):String类型转换为Date类型:Date parse(String source)  

无论是格式化还是解析都需要设置日期时间的模式,所谓模式就是一种格式。

![image.png](https://img.haomeiwen.com/i10465542/e8810fb6382a4ef0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


日期模式举例:

yyyy-MM-dd 如2020-12-12

HH:mm:ss 如20:12:12

yyyy-MM-dd HH:mm:ss 如2020-12-12 20:12:12

yyyy/MM/dd HH:mm:ss 如2020/12/12 20:12:12

yyyy年MM月dd日 HH时mm分ss秒 如2020年12月12日 20时12分12秒

格式化和解析代码如下:

public class SimpleDateFormatDemo {

public static void main(String[] args) throws Exception {

    java.util.Date d = new java.util.Date();

    // 创建SimpleDateFormat对象,设置日期时间转换模式

    SimpleDateFormat sdf = new SimpleDateFormat();

    String pattern = "yyyy-MM-dd HH:mm:ss";

    sdf.applyPattern(pattern);

    // 格式化(format):Date类型转换为String类型:String format(Date date)

    String str = sdf.format(d);

    System.out.println(str);//2018-05-17 14:48:38

    // 解析(parse):String类型转换为Date类型:Date parse(String source)

    java.util.Date dd = sdf.parse(str);

    System.out.println(dd);//Thu May 17 14:48:38 CST 2018

}

}

代码中public static void main(String[] args) throws Exception表示抛出异常,在main方法中不作任何处理,在异常章节再细讲。

## 2.3. **Calendar(了解)**

Calendar是日历类,主要用来对日期做相加减,重新设置日期时间功能,Calendar本身是一个抽象类,通过getInstance方法获取对象,其底层创建的是Calendar的子类对象。

public class CalendarDemo1 {

public static void main(String[] args) throws Exception {

    Calendar c = Calendar.getInstance();

    int year = c.get(Calendar.YEAR);

    int month = c.get(Calendar.MONTH) + 1;

    int date = c.get(Calendar.DAY_OF_MONTH);

    int hour = c.get(Calendar.HOUR_OF_DAY);

    int minute = c.get(Calendar.MINUTE);

    int second = c.get(Calendar.SECOND);

    System.out.println(year);//2018

    System.out.println(month);//5

    System.out.println(date);//17

    System.out.println(hour);//15

    System.out.println(minute);//1

    System.out.println(second);//38

    c.add(Calendar.YEAR, 100);//在当前年份上增加100

    System.out.println(c.get(Calendar.YEAR));//2118

}

}

需求:查询某个时间最近一周的信息,如何表示最近这一周的开始时间和结束时间

假如给出时间为:2018-05-18 15:05:30,那么最近一周的开始和结束时间分别为:

开始时间:2018-05-12 00:00:00

结束时间:2018-05-18 23:59:59

public class CalendarDemo2 {

public static void main(String[] args) throws Exception {

    String input = "2018-05-18 15:05:30";// 输入时间

    String pattern = "yyyy-MM-dd HH:mm:ss";

    SimpleDateFormat sdf = new SimpleDateFormat();

    sdf.applyPattern(pattern);

    Date d = sdf.parse(input);

    // -------------------------------------------

    Calendar c = Calendar.getInstance();

    c.setTime(d);// 把当前输入时间转换为Calendar对象

    c.set(Calendar.HOUR_OF_DAY, 23);

    c.set(Calendar.MINUTE, 59);

    c.set(Calendar.SECOND, 59);

    Date endDate = c.getTime();

    System.out.println(endDate.toLocaleString());

    c.add(Calendar.SECOND, 1);// 秒数增加1

    c.add(Calendar.DAY_OF_MONTH, -7);// 天数减去7

    Date beginDate = c.getTime();

    System.out.println(beginDate.toLocaleString());

}

}

# 3. **正则表达式**

正则表达式,简写为regex和RE。

正则表达式用来判断某一个字符串是不是符合某一种正确的规则,在开发中通常用于判断操作、替换操作、分割操作等。

![image.png](https://img.haomeiwen.com/i10465542/c116215c8425fd72.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


## 3.1. **正则表达式规则**

正则表达式匹配规则一:

![image.png](https://img.haomeiwen.com/i10465542/e4e4e2add061c4fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


正则表达式匹配规则二:

![image.png](https://img.haomeiwen.com/i10465542/e2910df2924b8e5f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


## 3.2. **正则表达式练习**

判断一个字符串是否全部有数字组成

判断一个字符串是否是手机号码

判断一个字符串是否是18位身份证号码

判断一个字符串是否6到16位,且第一个字必须为字母

public class REDemo {

public static void main(String[] args) throws Exception {

    // 判断一个字符串是否全部有数字组成

    System.out.println("12345678S".matches("\\d"));// false

    System.out.println("12345678".matches("\\d"));// false

    System.out.println("12345678".matches("\\d*"));// true

    System.out.println("1234".matches("\\d{5,10}"));// false

    System.out.println("12345678".matches("\\d{5,10}"));// true

    // 判断一个字符串是否是手机号码

    String regex1 = "^1[3|4|5|7|8][0-9]{9}$";

    System.out.println("12712345678".matches(regex1));// false

    System.out.println("13712345678".matches(regex1));// true

    // 判断一个字符串是否是18位身份证号码

    String regex2 = "\\d{17}[[0-9]X]";

    System.out.println("511123200110101234".matches(regex2));// true

    System.out.println("51112320011010123X".matches(regex2));// true

    System.out.println("51112320011010123S".matches(regex2));// false

    // 判断一个字符串是否6到16位,且第一个字必须为字母

    String regex3 = "^[a-zA-Z]\\w{5,15}$";

    System.out.println("will".matches(regex3));// false

    System.out.println("17will".matches(regex3));// false

    System.out.println("will17willwillwill".matches(regex3));// false

    System.out.println("will17".matches(regex3));// true

}

}


>若要获得最好的学习效果,需要配合对应教学视频一起学习。需要完整教学视频,请参看https://ke.qq.com/course/272077。

相关文章

网友评论

      本文标题:Java中的常用类

      本文链接:https://www.haomeiwen.com/subject/puhggftx.html