站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题正确使用String类的几点注意

正确使用String类的几点注意

摘要: 正确使用String类的几点注意

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
<table width="680" border="0"> <tr> <td width="407"> java.lang.String类对大家来说最熟悉不过了,我们写java程序很少能不用String的。本文讲述如何正确的使用String,内容主要涉及初始化、串联和比较等操作。

首先我们必须清楚的一点是String类是final类型的,因此你不可以继承这个类、不能修改这个类。我们使用String的时候非常简单,通常都是String s = "hello",但是Java API中同时提供了一个构造函数为String(String s),因此你也可以这样使用String s = new String("hello"),对于后面这样初始化一个String的方式是不推荐的,因为new操作符意味着将会生成一个新的对象,如果这样的操作发生在一个循环中,那么代价是惨重的。比如

for(int i = 0;i<1000;i++){

String s = new String("hello");
}
</td> <td width="263" align="center"> </td> </tr> </table>
这将会创建1000个String类型的对象,由于String类是final的,因此这样的操作事实上是每次都生成了一个新的String对象的。如果你使用String s = "hello";那么就可以实现复用了,为什么可以复用呢,下面会有解释。

当我们使用"+"实现串联操作的时候,比如String s = "hello"+"world";其实是通过StringBuffer类的append()方法实现的,最后返回String给s。如果有兴趣的话,你可以写一个简单的例子,然后用javap看看虚拟机是如何工作的。在使用串联的时候我们同样应该注意String是final类,如果你需要多次串联比如:
String sql = "xxx";
sql = "xxxx";
sql = "ssssss";
那么为了提高效率节省空间,我们应该自己用StringBuffer来替代"+";

通常对String的比较有两种情况,一个是使用==,另一个是使用equals()方法,注意==是对对象的地址进行比较的,而String中的equals()方法是覆盖了Object类的方法,并且实现为对String对象的内容的比较。所以String s1 = new String("hello");String s2 = new String("hello"),我们对s1和s2进行上述比较的时候,前者应该返回false,因为使用new生成的是两个不同的对象。后者应该返回true因为他们的内容是一样的,都是"hello"。那么如果我们还有一个String s3 = "hello";他和s1的比较应该是什么样子的呢,答案是s1==s3为false,equals的比较为true。事实上String类是维持着一个String池的,这个池初始化为空的,当我们String x = "hello"的时候,hello就会被放入这个池中,当我们再次String y = "hello"的时候,他首先去检查池中是否存在一个和hello内容一样的对象,如果存在的话就会把这个引用返回给y,如果不存在的话,就会创建一个并放入到池中。这样就实现了复用。在String有一个方法intern()他可以把String的对象放入到池冲并返回池中的对象。如果我们对s1(String s1 = new String("hello"))调用intern,s1 = s1.intern()这时候,我们再把s1和s3进行“==”的判断,你会发现结果返回true!
看下面的例子

public class StringTest
{

public static void main(String[] args)
{
String s1 = "hello";
String s2 = new String("hello");
String s3 = new String("hello");

String y="hello";
System.out.println("s1 = y is "+(s1==y));

testString(s1,s2,s3);
s2 = s2.intern();
System.out.println("after s2.intern");
testString(s1,s2,s3);



}

private static void testString(String s1,String s2,String s3)
{

System.out.println("s1 = s2 is "+(s1==s2));
System.out.println("s2 = s3 is "+(s2==s3));
System.out.println("s1 = s3 is "+(s1==s3));
System.out.println("s1.equals(s2) is "+s1.equals(s2));
System.out.println("s2.equals(s3) is "+s2.equals(s3));
}
}
输出结果为

D:\java>java StringTest
s1 = y is true
s1 = s2 is false
s2 = s3 is false
s1 = s3 is false
s1.equals(s2) is true
s2.equals(s3) is true
after s2.intern
s1 = s2 is true
s2 = s3 is false
s1 = s3 is false
s1.equals(s2) is true
s2.equals(s3) is true

D:\java>
</td> </tr> <tr>


↑返回目录
前一篇: 用Java Swing制作欢迎屏幕
后一篇: Java中的随机数应用