1、String:字符串常量,字符串长度不可变。
string源码及解析
总结:String实质是字符数组,两个特点:1、该类不可被继承;2、不可变性(immutable)。 String 类被final 修饰所以String类不能被继承和重写 public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ //操作字符串 相当对char数组的一系列操作 用于存放字符串的char数组被final修饰 因此只能赋值一次且不能更改 private final char value[]; /** Cache the hash code for the string */ privateinthash; // Default to 0 /** * Initializes a newly created {@code String} object so that it represents * an empty character sequence. Note that use of this constructor is * unnecessary since Strings are immutable. */ 当我们使用 String str = new String("hello"); 相当于 new char[]数组 创建了两个对象,一个存放在字符串池中,一个存在与堆区中;
还有一个对象引用str存放在栈中 public String() { this.value =newchar[0]; }
栈中用来存放一些原始数据类型的局部变量数据和对象的引用(String,数组.对象等等)但不存放对象内容 堆中存放使用new关键字创建的对象. 字符串是一个特殊包装类,其引用是存放在栈里的,而对象内容必须根据创建方式不同定(常量池和堆).有的是编译期就已经创建好,存放在字符串常 量池中,而有的是运行时才被创建.使用new关键字,存放在堆中。 |
待续........