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

1.构造器(构造方法)

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

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

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

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

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

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

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

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

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

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

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

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

2.成员方法

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

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

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

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

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

         String string = "hello";
System.out.println(string.equals("hello")); //true String s1 = "hello";
System.out.println(string.equals(s1)); //true String s2 = new String("hello");
String s3 = new String("hello");
System.out.println(s2.equals(s3)); //true /* 注意:StringBuffer和StringBuilder都没有equals()方法
所以调用equals()方法时,比较的是引用变量的地址,所以结果均为false*/
StringBuffer s4 = new StringBuffer("hello");
StringBuilder s5 = new StringBuilder("hello");
StringBuffer s6 = new StringBuffer("hello");
StringBuilder s7 = new StringBuilder("hello");
System.out.println(s1.equals(s4)); //false
System.out.println(s2.equals(s5)); //false
System.out.println(s4.equals(s5)); //false
System.out.println(s4.equals(s6)); //false
System.out.println(s5.equals(s7)); //fals
         String string = "hello";
System.out.println(string.equalsIgnoreCase("Hello")); //true
  • toCharArray()  字符串转换为数组,返回值为一个char类型的数组

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

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

  • 此外,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()

String类的源码分析的更多相关文章

  1. JDK中String类的源码分析(二)

    1.startsWith(String prefix, int toffset)方法 包括startsWith(*),endsWith(*)方法,都是调用上述一个方法 public boolean s ...

  2. JDK中String类的源码分析(一)

    1.String类是final的,不允许被继承 /** The value is used for character storage. */ private final char value[]; ...

  3. Spring-MongoDB 关键类的源码分析

    本文分析的是 spring-data-mongodb-1.9.2.RELEASE.jar 和 mongodb-driver-core-3.2.2.jar. 一.UML Class Diagram 核心 ...

  4. Set集合架构和常用实现类的源码分析以及实例应用

    说明:Set的实现类都是基于Map来实现的(HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的). (01) Set 是继承于Collection的接口.它是一个不允许 ...

  5. String,StringBuffer,StringBuilder源码分析

    1.类结构 String Diagrams StringBuffer Diagrams StringBuilder Diagrams 通过以上Diagrams可以看出,String,StringBuf ...

  6. Mybatis Mapper接口是如何找到实现类的-源码分析

    KeyWords: Mybatis 原理,源码,Mybatis Mapper 接口实现类,代理模式,动态代理,Java动态代理,Proxy.newProxyInstance,Mapper 映射,Map ...

  7. java类uuid源码分析

    通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为自由软件基金会组织在分散式计算环境领域的一部份.UUID的目的,是让分散式系统 ...

  8. 【Cocos2d-x 3.x】 动作类Action源码分析

    游戏设计中,动作是不可缺少的,Cocos2d-x中所有的动作都继承自Action类,而Action类继承自Ref和Clonable类,整个动作类继承体系如图: FiniteTimeAction是所有瞬 ...

  9. String、StringBuffer、StringBuilder源码分析

    利用反编译具体看看"+"的过程 1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 int ...

随机推荐

  1. sdram控制2

    芯片手册要求sdram需要在64ms内刷新8K次,否则里面的数据会丢失,因此在64ms分成8192次,每次刷新充一次电,然后给两次自动刷新命令即可. /*----------------------- ...

  2. Maven 项目 @Override must override a superclass method` 问题

    问题 Maven 项目 @Override must override a superclass method` 原因 JDK 在1.5以上的版本,才支持@Override 注解 解决方法 (1)po ...

  3. python+selenium自动化软件测试(第15章):基础实战(2)

    #coding:utf-8 #for windows/py2.7 from time import sleep from selenium import webdriver browser = web ...

  4. 简单易上手的Bootstrap

    什么是Bootstrap? Bootstrap是一个web框架.Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT ...

  5. LNMP1.4 PHP升级脚本

    升级PHP前,请确认你的网站程序是否支持升级到的PHP版本,防止升级到网站程序不兼容的PHP版本,具体可以去你使用的PHP程序的官网查询相关版本支持信息.v1.3及以后版本大部分情况下也可以进行降级操 ...

  6. 关于sys.argv

    sys.argv[]用来获取命令行参数,sys.argv[0]表示代码本身的文件路径.比如在命令行输入‘python test.py -version',sys.argv[0]的值即为test.py, ...

  7. Web桌面应用框架2:著名的WEB桌面应用分析

    前一篇文章里,分析了包括NW.js和electron这种纯JS框架在内的几种Web桌面应用开发方式,实际上还有一种最古老的方式,那就是嵌入WebView的方式. 嵌入WebView的方式和整个程序都是 ...

  8. vue :class的动态绑定

     动态绑定class 写在指令中的值会被视作表达式,如javascript表达式,因此v-bind:class接受三目运算: 1 2 3 4 HTML代码: <div :class=" ...

  9. 第1阶段——uboot分析之查找命令run_command函数和命令定义过程(6)

    本节主要学习,run_command函数命令查找过程,命令生成过程 1.run_command函数命令查找过程分析:在u-boot界面中(main_loop();位于u-boot-1.1.6/comm ...

  10. 教程,Python图片转字符堆叠图

    Python 图片转字符画 一.实验说明 1. 环境登录 无需密码自动登录, 2. 环境介绍 本实验环境采用带桌面的UbuntuLinux环境,实验中会用到桌面上的程序: LX终端(LXTermina ...