String 有关基础问题
String:实际上是字符串常量
常量的理解就非常的明确
- /** The value is used for character storage. */
- 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. */
- private final int count;
而String 实例化过程如下: String a = "123"; 用以上这种方法进行String创建时,实际java首先在内存中寻找"123"字符串,如果有,就把"123"的 地址给它,若没有则创建, 但是以下面这种方式创建 String a = new String("123"); 是不管内存中有没有"123" 都开辟一块新内存保存它
- /** The value is used for character storage. */
- public static void main(String[] args) {
- String a = "123";
- System.out.println(a.hashCode());
- String c = "123";
- System.out.println(c.hashCode());
- }
输出结果为 48690 48690 他们结果是一样,他们都是由相同的指针指向