def a = new HashSet()def str1 = "str1"def str2 = "str2"def b = "$str1-$str2"def c = "str1-str2"println "b: $b"println "c: $c"println "b.equals(c): " + (b.equals(c))println "b == c: " + (b == c)println "b.compareto(c): " + (b.compareto(c))a.add(b)println "a.contains(c): " + a.contains(c)
使用Groovy 1.8和JDK 1.6.0_14运行时,其输出如下:
b: str1-str2 c: str1-str2b.equals(c): falseb == c: trueb.compareto(c): 0a.contains(c): false
两个字符串“b”和“c”打印相同的字符序列,但是b.equals(c)是假的.根据JDK 1.6手册,equals()函数应该返回:
Compares this string to the specifIEd object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
为什么equals()不返回上面记录和演示的值?奇怪的是,compareto()返回0!
解决方法 问题在 Groovy GString page上得到解答.我需要在GString上调用toString(). 总结以上是内存溢出为你收集整理的奇怪的Groovy / Java String比较行为全部内容,希望文章能够帮你解决奇怪的Groovy / Java String比较行为所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)