Java中字符串相等的测试

Java中测试字符串是否相等的方法是.equals(),如果使用C++中惯用的==运算符,则有可能出错。对于字符串常量,JVM会合理安排它们以便能够共享,这时可以使用==来判定相等性;而字符串如果是+substring等操作的结果,则千万别用==。看下面程序

/*


* Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.


* All rights reserved. Software written by Ian F. Darwin and others.


* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $


* 2005/04/03 Modified By DotJox


*


* Redistribution and use in source and binary forms, with or without


* modification, are permitted provided that the following conditions


* are met:


* 1. Redistributions of source code must retain the above copyright


*     notice, this list of conditions and the following disclaimer.


* 2. Redistributions in binary form must reproduce the above copyright


*     notice, this list of conditions and the following disclaimer in the


*     documentation and/or other materials provided with the distribution.


*


* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''


* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED


* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR


* PURPOSE ARE DISCLAIMED.   IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS


* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR


* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF


* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS


* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN


* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)


* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE


* POSSIBILITY OF SUCH DAMAGE.


*


* Java, the Duke mascot, and all variants of Sun's Java "steaming coffee


* cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,


* pioneering role in inventing and promulgating (and standardizing) the Java


* language and environment is gratefully acknowledged.


*


* The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for


* inventing predecessor languages C and C++ is also gratefully acknowledged.


*/

public class Equality {

  public static void main(String[] args) {

    new Equality().run();

  }

  public void run() {

    String x = "hello";

    String y = "hello";

    // Assuming Equality uses a String constant, this prints true,true


    compare(x, y);

    // A "new String" is uniquely created, so this prints false, true


    compare(x, new String(y));

    // substring操作生成了一个新字符串, prints false, true


    compare(x, "hello world".substring(0, 5));

    // The intern() operator returns a string from the pool of unique strings, so this should print true, true.


    compare(x, new String(x).intern());

 

    /*很奇怪,"he"+"llo"合并字符串操作没生成新的字符串?(The string concatenation operator produces a new String object that contains the concatenation of its operands; the characters of the left operand precede the characters of the right operand in the newly created string.)但输出却是true,true*/


    compare(x,"he"+"llo");

  }

  public void compare(String s1, String s2) {

    System.out.print("==:       ");

    System.out.println(s1 == s2);

    System.out.println(".equals():" + s1.equals(s2));

    System.out.println();

  }

}

PS:当一个String实例str调用intern() 方法时,Java查找字符串池中是否有相同Unicode的字符串常量,如果有,则返回其的引用, 如果没有,则在字符串池中增加一个Unicode等于str的字符串并返回它的引用。字符串池最初是空的,由String类在私下维护。

 

程序输出:

Java中字符串相等的测试_第1张图片

所以,千万不要在Java中使用==来测试字符串的相等性。它只是用来确定字符串是否存储在相同的内存位置,而对于两个相同的字符串存储在不同的位置是完全有可能的。

 

你可能感兴趣的:(Java)