• String类的源码分析


    之前面试的时候被问到有没有看过String类的源码,楼主当时就慌了,回来赶紧补一课。

    1.构造器(构造方法)

    String类提供了很多不同的构造器,分别对应了不同的字符串初始化方法,此处从源码中摘录如下:

    其中蓝色的实心三角表示no modifier(没有修饰符,friendly),表示只能被同一个包中的所有类访问,而不同包中的类不能访问。

    这里举了一些示例,来说明这些构造器的用法:

    1         String string = new String();
    2         System.out.println(string.isEmpty());

     

    1         String string = new String("hello world");
    2         System.out.println(string);

    1         char[] arr = {'A','B', 'C', '1', '2', '3'};
    2         String arrString = new String(arr);
    3         System.out.println(arrString);

     

    1         char[] arr = {'A','B', 'C', '1', '2', '3'};
    2         String arrString = new String(arr, 1, 4);
    3         System.out.println(arrString);

     

    1         int[] codepoints = {101, 97, 98, 99};
    2         String string = new String(codepoints, 0, 3);
    3         System.out.println(string);

     

    1        StringBuffer buffer = new StringBuffer("hello");
    2        buffer.append(" world");
    3        String string = new String(buffer);
    4        System.out.println(string);

     

    1         StringBuilder builder = new StringBuilder("hello");
    2         builder.append(" world");
    3         String string = new String(builder);
    4         System.out.println(string);

    这里提到构造器,笔者想补充一个问题:构造器真的没有返回值吗?既然没有返回值,那么为什么不能用void关键字来修饰?

    解析:其实这只是Java语法上的一个规定。实际上,类的构造器是有返回值的,当我们用new关键字来调用构造器时,会返回一个该类的实例对象,并将这个实例对象在堆内存中的地址赋给了一个该类类型的引用变量。因此,构造器的返回值类型总是当前类,所以就无须定义返回值类型。但必须注意的是,不能在构造器里显式地使用return关键字来返回当前类的对象,因为构造器的返回值是隐式的。

    2.成员方法

    • charAt(int index)   返回字符串中下标为index的字符,返回值为char型
    1         String string = new String("hello world");
    2         System.out.println(string.charAt(0));

    • codePointAt(int index)  返回下标为index的字符的unicode码
    • codePointBefore(int index)  返回下标为index-1的字符的unicode码
    • codePointCount(int beginIndex, int endIndex)   返回下标从beginIndex到endIndex的字符数
    1         String string = "hello world";
    2         System.out.print(string.charAt(4)+" ");
    3         System.out.println(string.codePointAt(4));
    4         System.out.print(string.charAt(4)+" ");
    5         System.out.println(string.codePointBefore(5));
    6         System.out.println(string.codePointCount(0, 6));

     

    • equals(Object obj)  比较两个字符串是否相同,返回值为true或者false,此外还有equalsIgnoreCase(String anotherString),即忽略大小写的比较

        注意:1.字符串之间的比较时,比较的是字符串的内容而不是地址,并且只能用于比较String类型,因为StringBuffer和StringBuilder都没有equals()方法;

           2.非字符串之间的比较时,比较的是引用的地址而不是内容,可以用于StringBuffer和StringBuilder类型。

     1         String string = "hello";
     2         System.out.println(string.equals("hello")); //true
     3         
     4         String s1 = "hello";
     5         System.out.println(string.equals(s1));    //true
     6         
     7         String s2 = new String("hello");
     8         String s3 = new String("hello");
     9         System.out.println(s2.equals(s3));     //true
    10         
    11 /*        注意:StringBuffer和StringBuilder都没有equals()方法
    12             所以调用equals()方法时,比较的是引用变量的地址,所以结果均为false*/
    13         StringBuffer s4 = new StringBuffer("hello");
    14         StringBuilder s5 = new StringBuilder("hello");
    15         StringBuffer s6 = new StringBuffer("hello");
    16         StringBuilder s7 = new StringBuilder("hello");
    17         System.out.println(s1.equals(s4));    //false
    18         System.out.println(s2.equals(s5));    //false
    19         System.out.println(s4.equals(s5));    //false
    20         System.out.println(s4.equals(s6));    //false
    21         System.out.println(s5.equals(s7));    //fals
    1         String string = "hello";
    2         System.out.println(string.equalsIgnoreCase("Hello")); //true
    • toCharArray()  字符串转换为数组,返回值为一个char类型的数组

        注意:字符数组转换为字符串可以用构造器String(char[]) 实现

    1         String string = "hello world";
    2         char[] charArr = string.toCharArray();
    3         for(char ch: charArr){
    4             System.out.print(ch+" ");
    5         }

    • 此外,String类还有很多成员方法,这里简单列举一些常用的:

    startsWith(String prefix)  endsWith(String suffix)  indexOf(int ch)  indexOf(int ch, int fromIndex)  lastIndexOf(int ch)  lastIndexOf(int ch, int fromIndex)  indexOf(String str)  indexOf(String str, int fromIndex)  substring(int beginIndex)  substring(int beginIndex, int endIndex)  replace(char oldChar, char newChar)  matches(String regex)  contains(CharSequence s)    replaceAll(String regex, String replacement)  split(String regex)  toLowerCase()  toUpperCase()  trim()

  • 相关阅读:
    使用jQuery插件时避免重复引入jquery.js文件
    读书笔记《集体智慧编程》Chapter 2 : Make Recommendations
    数据挖掘学习07 《数据挖掘导论》第二章:数据
    推荐2款在线Ascii画图工具
    数据挖掘学习08 实验:使用R评估kmeans聚类的最优K
    数据挖掘学习05 使用R对文本进行hierarchical cluster并验证结果
    Apache alias目录配置
    数据挖掘学习06 《数据挖掘导论》导读
    Unix网络编程 3rd vol1 读书笔记
    关于Xcode
  • 原文地址:https://www.cnblogs.com/Wilange/p/7574512.html
Copyright © 2020-2023  润新知