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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值