String类

基础概念

  • jeeString类又称作不可变字符序列
  • String类位于java.lang包中,java程序默认导入java.lang包下的所有类
  • Java字符串就是Unicode字符序列,例如字符串”Java”就是4个Unicode字符”J”、”a”、”v”、”a”组成的
  • Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义的类String,每个双引号扩展起来的字符串都是String类的一个实例
  • 双引号引起来的字符串均存放在字符串常量池中 ```java String s1 = “abc”; // 字符串常量,放置于字符串常量池 String s2 = new String(“abc”); // 建立一个新的字符串对象 String s3 = “abc”; String s3 = “aBC”;

System.out.println(s1 == s2); // false System.out.println(s2 == s3); // true System.out.println(s1.equals(s3)); // true 判断字符串的值是否相等 System.out.println(s1.equalsIgnoreCase(s4)); // true 判断字符串忽略大小写值相等

  1. <a name="tozJM"></a>
  2. ## String类方法列表
  3. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/1683338/1642386031494-d42f9076-9971-4aca-b723-0bd967bb5d17.png#clientId=u40775942-8f81-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=529&id=ue6764e7e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=775&originWidth=1093&originalType=binary&ratio=1&rotation=0&showTitle=false&size=544468&status=done&style=none&taskId=u44d1c8bd-06fc-45ef-be48-fe8dba81a5c&title=&width=746.5)
  4. <a name="w51Pw"></a>
  5. # StringBuffer、StringBuilder类
  6. <a name="Lpg9w"></a>
  7. ## 基础概念
  8. | **String** | **StringBuffer** | **StringBuilder** |
  9. | --- | --- | --- |
  10. | 不可变字符序列 | 可变字符序列 | 可变字符序列 |
  11. | | 线程安全,做线程同步检查,效率较低 | 线程不安全,不做线程同步检查,因此效率高。建议使用该类。 |
  12. <a name="I8GwU"></a>
  13. ## 常用方法
  14. - StringBuilder与StringBuffer的常用方法是一致的
  15. - append("a"),追加
  16. - insert(0,"a"),插入
  17. - delete(0,2),删除
  18. - deleteCharAt(0),删除
  19. - reverse(),逆序
  20. <a name="vbvyV"></a>
  21. # 包装类
  22. <a name="QyTBr"></a>
  23. ## 八种基本数据类型的包装类
  24. | 基本数据类型 | byte | boolean | short | char | int | long | float | double |
  25. | --- | --- | --- | --- | --- | --- | --- | --- | --- |
  26. | 包装类 | Byte | Boolean | Short | Char | **Integer** | Long | Float | Double |
  27. <a name="Wbbyw"></a>
  28. ## 包装类的使用方式
  29. ```java
  30. // 基本数据类型转化成Integer对象
  31. Integer int1 = Integer.valueOf(100);
  32. // 包装类对象转成基本数据类型
  33. int int2 = int1.intValue();
  34. long long1 = int1.longValue();
  35. // 字符串转Integer对象
  36. Integer int3 = Integer.parseInt("321");
  37. // 类型最大数
  38. System.out.println("int能表示的最大整数:" + Integer.MAX_VALUE);

自动装箱和自动拆箱

  1. Integer a = 100; // 自动装箱,编译器完成正常类型转换操作
  2. int b = a; // 自动拆箱
  • 整型、char类型所对应的包装类在自动装箱时,对于-128到127之间的值会进行缓存处理,以提高效率

    1. Integer a = 100;
    2. Integer b = Integer.valueOf(100);
    3. System.out.println(a == b); // true a b 是同一个类,由IntegerCache类处理
    4. Integer c = 300;
    5. Integer d = Integer.valueOf(300);
    6. System.out.println(c == d); // false c d 不是一个类

    源码解读Integer类

    ```java public class MyInteger { public static final int LOW_VALUE = -128; public static final int MAX_VALUE = 127;

    private int value; private static MyInteger[] integerCache = new MyInteger[256];

    static {

    1. for (int i = MyInteger.LOW_VALUE; i <= MyInteger.MAX_VALUE; i++) {
    2. integerCache[i + (-MyInteger.LOW_VALUE)] = new MyInteger(i);
    3. }

    }

    public static MyInteger valueOf(int val){

    1. if(val>= MyInteger.LOW_VALUE && val<= MyInteger.MAX_VALUE){
    2. return integerCache[val + (-MyInteger.LOW_VALUE)];
    3. }else {
    4. return new MyInteger(val);
    5. }

    }

    public int intValue(){

    1. return value;

    }

    public String toString(){

    1. return value + "";

    }

    private MyInteger(int val){

    1. value = val;

    }

  1. public static void main(String[] args) {
  2. MyInteger a = MyInteger.valueOf(100);
  3. MyInteger b = MyInteger.valueOf(100);
  4. System.out.println(a == b);
  5. MyInteger c = MyInteger.valueOf(300);
  6. MyInteger d = MyInteger.valueOf(300);
  7. System.out.println(c == d);
  8. }

}

  1. <a name="H9uE9"></a>
  2. # 时间相关类
  3. <a name="XuNJG"></a>
  4. ## Date
  5. ```java
  6. public static void testDate(){
  7. // 返回当前毫秒数
  8. long nowMillis = System.currentTimeMillis();
  9. Date d1 = new Date();
  10. Date d2 = new Date(1000L * 3600 * 24 * 100);
  11. }

DateFormat

  • 时间和字符串之间互相转换

    1. public static void testDateFormat(){
    2. // 日期对象转字符串
    3. DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    4. Date d2 = new Date(1000L * 3600 * 24 * 100);
    5. Sting strD2 = df.format(d2);
    6. // 字符串转日期对象
    7. Sting strD3 = "2049-10-1 10:10:20";
    8. Date d3 = df.parse(str3);
    9. // 获取当前时间是当年的第几天
    10. Dateformat df2 = new SimpleDateFormat("D");
    11. String theDay = df2.format(d2);
    12. }

    image.png

    Calendar

    1. public static void testCalendar(){
    2. // 月份 0-11一月到十二月 星期1-7 周日到周六
    3. Calendar calendar = new GregorianCalendar(1989,3,14,8,8,8);
    4. int year = calendar.get(Calendar.YEAR);
    5. int month = calendar.get(Calendar.MONTH)
    6. calendar.set(Calendar.YEAR,2017)
    7. // 返回的日期对象
    8. Date d1 = calendar.getTime();
    9. // 返回的毫米数
    10. long millis1 = calendar.getTimeInMillis();
    11. // 日期计算,增加1000天
    12. calendar.add(Calendar.DATE,1000)
    13. // 日期计算,减少30年
    14. calendar.add(Calendar.YEAR,30)
    15. }