Java中字符串相等的测试

原创 2006年06月05日 11:39:00

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中使用==来测试字符串的相等性。它只是用来确定字符串是否存储在相同的内存位置,而对于两个相同的字符串存储在不同的位置是完全有可能的。

 

java后台获取页面下拉框的选项只值,判断字符串是否相等

一, //  String serviceName = request.getParameter("serviceName");          if ("RetrieveDocumentSet"....

JAVA Android 字符串是否相等使用equals方法

1 总体来说java中字符串的比较是==比较引用,equals 比较值的做法。(equals 对于其他引用类型比较的是地址,这是因为object的equals方法比较的是引用),但是不同的声明方法字符...

Java中字符串相等与大小比较

在C++中,两个字符串比较的代码可以为: (string1==string2) 但在java中,这个代码即使在两个字符串完全相同的情况下也会返回false Java中必须使用string1.eq...

Java判断字符串相等的办法(不能直接用==判断)

用惯了“==”来判断变量的值是否相等,这回,判断两个字符串变量的内容是否相同,毫不犹豫就想到了“==”了。       调试内容是这样的,我们用户注册页面有两个框分别是输入密码(设为变量a)和密...
  • educast
  • educast
  • 2013年10月20日 10:47
  • 3639

Java中字符串比较相等问题

Java中String比较equals()方法浅析

Java中的字符串比较相等与大小

在C++中,两个字符串比较的代码可以为: (string1==string2) 但在java中,这个代码即使在两个字符串完全相同的情况下也会返回false Java中必须使用string1...

【转】在Java里判断字符串是否相等(字符串池)

CSDN发现了一个问题。类是这样写的:public   class   Sub{   String   str=new   String( "good ");   //一个地址的传递,new出来的对象...
  • zw_2011
  • zw_2011
  • 2011年05月19日 09:18
  • 1207

字符串的截取,判断字符串是否相等和字符串是否为空的方法总结Java,Android

一、可以用subSequence方法截取任意长度的字符 二、判断字符串是否相等 三、判断字符串是否为空的四种方法: 最近做项目Android时遇到了字符串的截取,判断字符串是否相等和字符串是否为空问题...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java中字符串相等的测试
举报原因:
原因补充:

(最多只允许输入30个字)