阅读目录(Content)

  • 一、String简介
    • 1.1、String(字符串常量)概述
    • 1.2、分析String源码
  • 二、创建字符串对象两种方式的区别
    • 2.1、直接赋值方式创建对象
    • 2.2、通过构造方法创建字符串对象
    • 2.3、两种实例化方式的比较
  • 三、String常用的方法
    • 3.1、String的判断功能
    • 3.2、String类的获取功能
    • 3.3、String的转换功能
    • 3.4、其他常用方法
  • 四、String的不可变性
    • 4.1、前言
    • 4.2、分析
    • 4.3、String不可变的好处
  • 五、字符串常量池
    • 5.1、字符串常量池概述
    • 5.2、亨元模式
    • 5.3、详细分析

前言

  在我们开发中经常会用到很多的常用的工具类,这里做一个总结。他们有很多的方法都是我们经常要用到的。所以我们一定要把它好好的掌握起来!

1.1、String(字符串常量)概述

  在API中是这样描述:

    String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。 
    字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享。

  java.lang.String:

    Java-14常用类-03=String类详解

1.2、分析String源码

  1)String的成员变量

Java-14常用类-03=String类详解
Java-14常用类-03=String类详解
 /** String的属性值 */  
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    /**数组被使用的开始位置**/
    private final int offset;

    /** The count is the number of characters in the String. */
    /**String中元素的个数**/
    private final int count;

    /** Cache the hash code for the string */
   /**String类型的hash值**/
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;
    /**
     * Class String is special cased within the Serialization Stream         Protocol.
     *
     * A String instance is written into an ObjectOutputStream according to
     * <a href="{@docRoot}/../platform/serialization/spec/output.html">
     * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
     */

  private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];
Java-14常用类-03=String类详解

相关文章: