我看JAVA 之 String
我看JAVA 之 String
注:基于jdk11
String
在java语言中用来表示字符串,所有的字符串(比如“abc”)都String的实例对象。 String是常量,一旦创建不可以被修改,可以使用StringBuffer创建可变字符串。 String类提供了字符串比较,查找,拷贝,大小写转换等操作。大小写转换基于标准的Unicode. 字符串拼接”+”:根据不同版本的jdk会有不同实现,如StringBuilder、StringBuffer、StringConcatFactory(invokeDynamic)
实现了如下接口
1. java.io.Serializable 2. Comparable<String> 3. CharSequence 提供对字符数组多种只读形式的统一访问方法规范
几个重点的成员变量
/** * jdk9开始使用byte[]存储字符串,1.8及之前使用char[]保存 */ @Stable private final byte[] value; /** * coder用来表示此字符串使用的编码,coder=0使用LATIN1,coder=1使用UTF16 * * LATIN1 是8比特的字符集,定义了256个字符。前128个字符与ASCII完全一致,即为ASCII的超集 * UTF16 是可变长度编码。可以是一个或二个16比特。 * 根据不同的编码由不同的工具类实现String的内部编码,Latin1对应StringLatin1,UTF16对应StringUTF16 * */ private final byte coder; /** Cache the hash code for the string */ private int hash; // Default to 0 /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; /** * 如果关闭压缩,字符串的bytes使用UTF16编码 * * 如下为jit优化方面,为什么不直接初始化COMPACT_STRINGS的值: * * The instance field value is generally opaque to optimizing JIT * compilers. Therefore, in performance-sensitive place, an explicit * check of the static boolean {@code COMPACT_STRINGS} is done first * before checking the {@code coder} field since the static boolean * {@code COMPACT_STRINGS} would be constant folded away by an * optimizing JIT compiler. The idioms for these cases are as follows. * * For code such as: * * if (coder == LATIN1) { ... } * * can be written more optimally as * * if (coder() == LATIN1) { ... } * * or: * * if (COMPACT_STRINGS && coder == LATIN1) { ... } * * An optimizing JIT compiler can fold the above conditional as: * * COMPACT_STRINGS == true => if (coder == LATIN1) { ... } * COMPACT_STRINGS == false => if (false) { ... } * * @implNote * The actual value for this field is injected by JVM. The static * initialization block is used to set the value here to communicate * that this static final field is not statically foldable, and to * avoid any possible circular dependency during vm initialization. * 事实上,COMPACT_STRINGS的值是由JVM填充的 */ static final boolean COMPACT_STRINGS; static { COMPACT_STRINGS = true; } /** * Class String is special cased within the Serialization Stream Protocol. * * A String instance is written into an ObjectOutputStream according to * <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements"> * Object Serialization Specification, Section 6.2, "Stream Elements"</a> */ private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0]; @Native static final byte LATIN1 = 0; @Native static final byte UTF16 = 1;
几个重要的方法
1. getBytes()相关 /** * getBytes() 将当前字符串转换为当前文件系统默认编码格式的字节数组 * getBytes(charset) 将当前字符串转换为指定编码格式的字节数组 */ public byte[] getBytes(String charsetName) throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException(); return StringCoding.encode(charsetName, coder(), value); } public byte[] getBytes(Charset charset) { if (charset == null) throw new NullPointerException(); return StringCoding.encode(charset, coder(), value); } public byte[] getBytes() { return StringCoding.encode(coder(), value); } 2. length() /** * 返回当前字符串长度,如果是LATIN1字符串长度等于LATIN1格式字节数组长度,否则需要取value.length>>1,长度减半 */ public int length() { return value.length >> coder(); } 3. native intern() /** * 当调用intern方法时,如果常量池中已经存在equal当前String的对象,那么返回String常量池中的字符串。 * 否则,当前String对象会被添加到String常量池并且返回常量池中的String对象引用 * 如果a.intern() == b.intern(),那么a.equal(b) == true */ public native String intern();
几个重要的工具类
1. StringLatin1 提供了启用压缩编码Latin1的情况下的一些常用操作如indexOf、hashcode、replace、trim、strip、compare等 2. StringUTF16 提供了编码为UTF16的情况下的一些常用操作如indexOf、hashcode、replace、trim、strip、compare等 3. StringCoding 提供了为String编解码decode & encode操作
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Spring Cloud Alibaba 新一代微服务解决方案
本篇是「跟我学 Spring Cloud Alibaba」系列的第一篇, 每期文章会在公众号「架构进化论」进行首发更新,欢迎关注。 1、Spring Cloud Alibaba 是什么 Spring Cloud Alibaba 是阿里巴巴提供的微服务开发一站式解决方案,是阿里巴巴开源中间件与 Spring Cloud 体系的融合。 马老师左手双十一,右手阿里开源组件,不仅占据了程序员的购物车,还要攻占大家的开发工具。 先说说 Spring Cloud 提起微服务,不得不提 Spring Cloud 全家桶系列,SpringCloud 是若干个框架的集合,包括 spring-cloud-config、spring-cloud-bus 等近 20 个子项目,提供了服务治理、服务网关、智能路由、负载均衡、断路器、监控跟踪、分布式消息队列、配置管理等领域的解决方案。 Spring Cloud 通过 Spring Boot 风格的封装,屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、容易部署的分布式系统开发工具包。 一般来说,Spring Cloud 包含以下组件,主要以 Netf...
- 下一篇
c++入门教程:c++中的动态数组
接着跟大家更新c++入门教程:c++中的动态数组 C++的new操作符是该语言一个非常好的语法特性,然而实际使用中却发现new操作符有不少限制,为突出的一点便是用new操作符分配多维数组空间时,不能让数组的每一维都动态可变。本文将对此提出一个简单直观的解决方案,在一个实际问题的简化模型中加以说明,并以此释清许多初学者对C++中new操作符与多维数组的误区。 问题的提出--多维可变数组的实际用途下面是实际编程中遇到问题的一个简化模型。ChessBoard是一个棋盘类,其中的m_board是用来保存棋盘上棋子信息的二维数组。DIMENSION是棋盘的尺寸或者维数,因为要用于数组声明,所以它必须是一个编译期间可以确定其值的常量,这里我们使用了无名枚举。对于不同种类棋的棋盘大小是不同的,对于黑白棋,DIMENSION定义为8,对于五子棋,DIMENSION应该为15,而围棋呢,又得是19。对此这段代码采用了条件编译来确定DIMENSION常量的值,以保证这段代码具有较好的可重用性。 由于m_board必须是编译期常量,于是在程序运行时刻m_board数组的大小是不可改变的。如果程序中要同时实现...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Red5直播服务器,属于Java语言的直播服务器
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS6,CentOS7官方镜像安装Oracle11G
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装