字符串操作

来源:互联网 发布:线条随鼠标特效源码 编辑:程序博客网 时间:2024/05/26 02:55

String的创建

String的创建方式有一下集中:
1. 直接使用“”创建
2. 直接使用new String()创建
3. 使用new String("sth")创建以及其他的一些重载的函数创建
4. 使用重载的字符串连接符"+"进行创建。

其中,方式1

String s = "hello";

JVM首先会根据内容”hello”查找对象,如果没有找到,则在heap上创建新对象,并将其赋予s1,否则使用已经存在的对象。
方式2

String s = new String(“hello”);

JVM直接咋heap上创建新的对象,所以在heap中会出现内容相同而地址不同的String对象。
并且,这种方式会创建出两个对象出来。

方式4

String str ="a" + "b" + "c" + "d";

上面的代码只会创建出一个对象,”a”、“b”等由于都是常亮,因此他们存在与常量池中,不会去创建新的对象,因此就只会创建出最后的对象"acbd".

String对象的比较

==是比较地址,equals是比较内容。

public static void main(String[] args) {        String s1 ="hello";        String s2 = "hello";        String s3 = new String("hello");        System.out.println(s1==s2);//true        System.out.println(s1==s3);//false        System.out.println(s1.equals(s2));//true        System.out.println(s1.equals(s3));//true    }

intern()方法

查找相同内容(equals())的字符串,举例如下:

    public static void main(String[] args) {        String s1 = "hello";        String s2 = new String("hello");        System.out.println(s1 == s2);//true        System.out.println(s1.equals(s2));//true        s2 = s2.intern();//找到内容相同的对象,此时只有s1,因此将s1赋给s2        System.out.println(s1 == s2);//true    }

程序的执行结果如下:
enter description here

String和StringBuffer

例子1:

public static void main(String[] args) {        long start  = System.nanoTime();        for(int i=0;i<100000;i++){            StringBuffer result2 = new StringBuffer().append("hello").append("world");        }        System.out.println(System.nanoTime()-start);        start  = System.nanoTime();        for(int i=0;i<100000;i++){            String result1 = "hello" + "world";        }        System.out.println(System.nanoTime()-start);    }

程序的运行时间如下
可以发现:
使用”+”的形式,效率会更好,因为JVM会进行如下处理:

  • 将result1字符串进行“hello” + “world”处理,然后才赋值给result。只开辟了一次内存段。
  • 编译StringBuffer后还要进行append处理,花的时间要长一些。

enter description here

另外String 、StringBuffer、StringBuilder三者的效率对比如下:

enter description here

实例2:

public static void main(String[] args) {        STRClass strTest = new STRClass();        long start  = System.nanoTime();        for(int i=0;i<10000;i++){            strTest.getString1("hello","world");        }        System.out.println(System.nanoTime()-start);        start  = System.nanoTime();        for(int i=0;i<10000;i++){            strTest.getString1("hello","world");        }        System.out.println(System.nanoTime()-start);    }    public String getString1(String s1, String s2) {        return s1+s2;    }    public String getString2(String s1, String s2) {        return String.valueOf(new StringBuffer().append(s1).append(s2));    }

程序的执行结果是:

enter description here
从上面的运行结果可以看到,两者的效率基本上是差不多

StringBuffer常见的问题:
(1)不要认为.append()操作的效率优于”+”
(2)不要使用new 创建String
(3)注意.intern()的使用
(4)在编译期间能够确定字符串值的情况下,使用"+"效率最高。
(5)避免使用”+=”来构造字符串。
(6)在声明StringBuffer对象的时候,制定合适的capacity,不要使用默认的值。
(7)注意以下二者的区别,后者开辟了两个内存段。

 String s = "a"  +  "b"; String s = "a";           s += "b";

示例3:

package com.company;/** * Created by shugen on 17-3-4. */public class HowManyClassWillCreate {    public static void main(String[] args) {        String A, B, C;        A = "a";//将会创建一个对象“a”,A是一个引用变量        B = "b";//同A一样        A= A + B;//此处床架了一个新的对象,由A重新引用        StringBuffer D = new StringBuffer("abc");//这里将会创建两个变量"abc"和D(D是new出来的)        D.append("def");//"def"又是一个对象    }}

上面的程序将会创建5个对象。

有一下几个需要说明的问题:

引用变量和对象的区别。

A a;

上面的语句创建的是一个引用变量,而对象的创建是通过new关键字来进行创建的,示例如下:

A a = new A();

Java中的字符串文字的说明

java中的所有的字符串文字都是一个String的对象。
因此,下面的代码先是创建了一个引用变量A,再创建了一个字符串对象"a"

String A;A = "a";

Java字符串对象的创建。

由于字符串对象的大量使用,Java为了节省内存空间和运行时间,再编译阶段就把所有的字符串文字放到了poll of literal strings中,也就是常量池的一部分。字符串池的好处就是池中的所有相同的字符串常量将会被何必,从而只占用一个空间。

0 0
原创粉丝点击