Java 复习 —— String


一、String的实例过程

    Java字符串的维护就是一个享元模式的具体体现,咱们知道String的实例有两种方式分别是:java

String str =  "hello world";
String str2 = new String("hello world");

    其中前者字面量的方式具体流程是这样的:首先查找 String Pool(栈)中是否有“hello world”,若是不存在,那会在String Pool中建立一个“hello world”,同时将这个对象的引用返回给str;若是String Pool中存在,那么直接获取String Pool中的值返回;设计模式

    后者new一个的对象的具体流程是这样的:首先查找 String Pool中是否有“hello world”,若是不存在,那么一样在String Pool中建立一个,而后去堆(Heap)中建立一个“hello world”对象,而且把堆中的引用返回给str2;若是String Pool中存在了这个对象,那么,跳过String Pool中建立,而是直接在堆中去建立一个“hello world”而后把引用返回。数组

    经过以上就会明白:性能

为何ui

"hello world" == "hello world" 为 true ;

可是 this

new String("hello world") == new String("hello wrold") 为 false;

同时 spa

"hello world" == new String("hello world") 也为false;

    另外,String Pool 就是一个字符串的享元工厂,字面量的方式就是一个享元工厂模式。你们都知道StringBuffer的性能要好过 String,StringBuilder的性能要好过StringBuffer,但是为何还要用String呢?缘由就在这里,他们说的性能是字符串 修改的时候,而真正字符的基本操做,以及字符串从系统中获取的时候,String是最快的,维护的对象也最少。设计


二、String的相等判断

    String 重写了equals方法,Object的equals方法是直接比较两个对象的地址,因此使用在字符串上比较意义不是很大,想要判断两个字符串的是否相同,咱们应该这样判断,采用重写的equals方法。具体代码以下:code

     public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
            if (v1[i++] != v2[j++])
            return false;
        }
        return true;
        }
    }
    return false;
    }

三、String 其余注意事项

1)、字符串没有属性暴露,内部有属性通常是私有并且final类型的;好比offset、count、value;对象

2)、字符串重写了equals方法,那么也必定后重写了hashcode方法;

3)、字符串不能被继承是由于他是一个final类型的类;

4)、字符串内容不能更改是由于内部使用一个final类型的char数组存储,因此不能改变;

5)、字符串的优点在于他的速度,由于他的设计是一种享元设计模式。

相关文章
相关标签/搜索