String类的源码分析
之前面试的时候被问到有没有看过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类的源码分析的更多相关文章
- JDK中String类的源码分析(二)
1.startsWith(String prefix, int toffset)方法 包括startsWith(*),endsWith(*)方法,都是调用上述一个方法 public boolean s ...
- JDK中String类的源码分析(一)
1.String类是final的,不允许被继承 /** The value is used for character storage. */ private final char value[]; ...
- Spring-MongoDB 关键类的源码分析
本文分析的是 spring-data-mongodb-1.9.2.RELEASE.jar 和 mongodb-driver-core-3.2.2.jar. 一.UML Class Diagram 核心 ...
- Set集合架构和常用实现类的源码分析以及实例应用
说明:Set的实现类都是基于Map来实现的(HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的). (01) Set 是继承于Collection的接口.它是一个不允许 ...
- String,StringBuffer,StringBuilder源码分析
1.类结构 String Diagrams StringBuffer Diagrams StringBuilder Diagrams 通过以上Diagrams可以看出,String,StringBuf ...
- Mybatis Mapper接口是如何找到实现类的-源码分析
KeyWords: Mybatis 原理,源码,Mybatis Mapper 接口实现类,代理模式,动态代理,Java动态代理,Proxy.newProxyInstance,Mapper 映射,Map ...
- java类uuid源码分析
通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为自由软件基金会组织在分散式计算环境领域的一部份.UUID的目的,是让分散式系统 ...
- 【Cocos2d-x 3.x】 动作类Action源码分析
游戏设计中,动作是不可缺少的,Cocos2d-x中所有的动作都继承自Action类,而Action类继承自Ref和Clonable类,整个动作类继承体系如图: FiniteTimeAction是所有瞬 ...
- String、StringBuffer、StringBuilder源码分析
利用反编译具体看看"+"的过程 1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 int ...
随机推荐
- Go 到底有没有引用传参(对比 C++ )
Go 到底有没有引用传参(对比 C++ ) C++ 中三种参数传递方式 值传递: 最常见的一种传参方式,函数的形参是实参的拷贝,函数中改变形参不会影响到函数外部的形参.一般是函数内部修改参数而又不希望 ...
- lucene&solr-day1
全文检索课程 Lucene&Solr(1) 1. 计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) ...
- 总结各种排序算法【Java实现】
一.插入类排序 1.直接插入排序 思想:将第i个插入到前i-1个中的适当位置 时间复杂度:T(n) = O(n²). 空间复杂度:S(n) = O(1). 稳定性:稳定排序. 如果碰见一个和插入元素相 ...
- js文件引用方式及其同步执行与异步执行
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp74 任何以appendChild(scriptNode) 的方式引入 ...
- Mock Server 入门
Mock Server介绍 什么是mock ? 我在去年的时候介绍一篇幅 python mock的基本使用,http://www.cnblogs.com/fnng/p/5648247.html 主要是 ...
- Vue项目搭建基础之Vue-cli模版测试
第一步安装node,nodejs.org下载node稳定版安装包.node -v (查看node版本)npm install -g vue-cli(安装Vue脚手架环境)vuevue listvu ...
- 201521123083《Java程序设计》第13周学习总结
本次作业参考文件 正则表达式参考资料 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.bai ...
- 软件工程HW1-四则运算软件
题目描述 程序自动生成小学四则运算题目,用户输入每道题的答案之后,将答错的题目标出并计算此次答题的正确率. 项目链接 我的项目 项目运行截图 个人软件过程 此次开发的四个步骤: 1):需求分析 2): ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.4.24
在下午3-4节Linux课结束后,我们teamworkers全体队员留在禹州楼304进行约20分钟的短暂会议,会议讨论关于昨天任务的总结并分配了今天的新任务,大家畅所欲言,情绪高昂,各自阐述了自己不一 ...
- Beta版本冲刺计划及安排(附七天冲刺的博客链接)
Beta版本冲刺计划及安排(附七天冲刺的博客链接) 新增组员 本次换人加入我们团队的新成员是原"爸爸说的都队"的队长念其锋同学,经过我们小组严格的两轮面试,他从几个同样前来面试的同 ...