---
title: "Java中字符串的应用"
author: "lfl2011"
date: "2014-12-12T20:54:49Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2014-12-12T20:56:36Z"
id: 2165958
url: "https://lfl2011.iteye.com/blog/2165958"
---

intern 方法   返回从字符串常量池中唯一的字符串。

 

所有的字符串和字符串常量表达式的值都是interned。

即在Java中所有的编译时字符串常量都自动使用了intern()方法是interned，

”abc”  和 String s = “abc”  都是interned。

Java中其它的基本数据类型 比如(any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127)  都是interned。

 

Integer类型示例：

 

 

```
1:  Integer i1 = 3;
    Integer i2 = Integer.valueOf(3);
    Integer i3 = new Integer(3);

i1 == i2   true    i1 == i3   false    i2 == i3   false   3 == i3  true



2:    Integer m1 = 390;
      Integer m2 = Integer.valueOf(390);
      Integer m3 = new Integer(390);

m1 == m2   true    m1 == m3   false    m2 == m3   false    390 == m3  true
```

 

  

字符串比较：

字符串类型示例：

 

```
1：     long t1 = System.currentTimeMillis();
        String ss1 = "asd";
        String ss2 = new String("asd");
        for (int i = 0; i < 100000000; i++) {
            if (ss1.equals(ss2)) {
                
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println(t2 - t1);
     
   
2：     long t3 = System.currentTimeMillis();
        String ss3 = "jkl";
        String ss4 = new String("jkl").intern();
        for (int i = 0; i < 100000000; i++) {
            if (ss3 == ss4) {
                
            }
        }
        long t4 = System.currentTimeMillis();
        System.out.println(t4 - t3);
        
        
        
 3：    long t5 = System.currentTimeMillis();
        String ss5 = "fgh";
        String ss6 = new String("fgh");
        for (int i = 0; i < 100000000; i++) {
            if (ss5.equalsIgnoreCase(ss6)) {
                
            }
        }
        long t6 = System.currentTimeMillis();
        System.out.println(t6 - t5);
```

 

 

 

示例1,2,3测试3次的平均运行所需时间为1285.6ms，32.6ms，1869.3ms,

 

可以看出示例1大概是示例2的40倍，示例1大概是示例3的2/3倍。

 

 

 

equals()：先是比较比较两个对象是否相等，如果不相等再逐个字符进行比较。

 

 

 

intern()后再 == ：字符串或字符串常量表达式是直接生成在JVM常量池中的字符串。intern()方法执行时先是查看常量池中有没有相同的字符串，如果有返回已存在的字符串，如果没有则把字符串放入常量池，再返回该字符串，每个字符串在常量池中都有唯一的标识符，比较时就是使用这个唯一标识符经行比较。new String() 是在JVM堆中和常量池中各一份， 比较的时候是和堆中的字符串对象进行比较。

 

 

 

equalsIgnoreCase()：先是比较比较两个对象是否相等，如果不相等再逐个字符进行比较是否相等，如果不相等再转为大写字符进行比较，对于特殊的格鲁吉亚字母如果不相等再转为小写字符进行比较。

 

 

 

字符串连接：参见\[1]

 

 

```
package testPackage; 
class Test { 
public static void main(String[] args) { 
String hello = "Hello", lo = "lo"; 
System.out.print((hello == "Hello") + " "); 
System.out.print((hello == ("Hel"+"lo")) + " "); 
System.out.print((hello == ("Hel"+lo)) + " "); 
System.out.println(hello == ("Hel"+lo).intern()); 
} 
} 
```

 

输出：true  true   false  true

第二个是在编译时的常量表达式进行连接，编译器进行优化使用同样的字符串；

第三个是在运行时连接重新生成新的字符串。

  

CharSequence   是个接口开始于Java 1.4，文档中的解释是可读的字符序列，   它的直接子类有CharBuffer, Segment, String, StringBuffer, StringBuilder。因此当你希望你的API接受这些类时CharSequence是个不错的选择。CharSequence的实现类没有全部实现equals/hashCode方法，当你需要Map或者依赖equals/hashCode时，应当使用String或者把CharSequence转为String。在涉及密码等安全信息是应当使用CharSequence或者char而不是String。

在Java8 之前CharSequence的作用可能非常有限，一般应该被用在字符串处理操作中。一个CharSequence不能保证是不可变的，而String是可以保证不可变的。

 

 

StringTokenizer

StringCharacterIterator

 

 

1.  Jls8 3.10.5 String Literals

 

2.  [http://stackoverflow.com/questions/13234584/when-to-use-charsequence](http://stackoverflow.com/questions/13234584/when-to-use-charsequence)

 

3.  [http://stackoverflow.com/questions/11323962/exact-difference-between-charsequence-and-string-in-java](http://stackoverflow.com/questions/11323962/exact-difference-between-charsequence-and-string-in-java)

 

4.  [http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords)

 

5.  [http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext-in-java](http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext-in-java)