当“ == s是假,但是“”等于真 [英] When "" == s is false but "".equals( s ) is true
问题描述
EDIT
感谢您的快速回复。请看看真正的问题是什么。
我明白==和.equals之间的区别。所以,这不是我的问题(我实际上添加了一些上下文)
空字符串:
if(== value){
//是空字符串
}
在从数据库获取值或反序列化另一个对象时的过去节点,此测试失败,因为两个字符串实例确实是不同的对象引用,尽管它们包含相同的数据。
这些情况是
if(.equals(value)){
//空字符串
}
这是清楚地了解。
今天这种情况再次发生,但令人困惑的是,因为这次应用程序是一个非常小的独立应用程序,不使用
所以问题是:
其中其他情况:
==值//产生假
和
equals(value)// yield true
pre>
对于本地独立应用程序?
我确定新的String()未在代码中使用。
字符串引用可能是的唯一方法是因为它直接在代码中分配(或者我的想法):
String a =;
String b = a;
assert== b; //这是真的
无论如何(读取代码后,我有一个线索)两个不同的空字符串对象引用已创建,我想知道如何
jjnguys行中的更多回答:
字节!
编辑:结束 ve找到原因。
在jjnguy建议后,我可以用不同的眼睛看看代码。
有罪的方法:StringBuilder .toString()
一个新的String对象被分配并初始化为包含此对象当前表示的字符序列。 em>
Doh!...
StringBuilder b = new StringBuilder(h);
b.eleteCharAt(0);
System.out.println(== b.toString()); // prints false
Mystery解决了。
代码使用StringBuilder来处理不断增长的字符串。事实证明,有人做了:
public void someAction(String string){
if( == string){
return;
}
deleteBankAccount(string);
}
并使用
someAction(myBuilder.toString()); //错误介绍。
p.s。我最近读了太多的CodingHorror?为什么我觉得需要在这里添加一些有趣的动物图片?
解决方案
String s =
String s2 = someUserInputVariale.toLowercase(); //用户在中输入的位置
这样会导致
== s2
以评估为false。
许多代码sill create new
Strings
而不会暴露对new String()
的调用。EDIT Thanks for the prompt responses. Please see what the real question is. I have made it bold this time.
I do understand the difference between == and .equals. So, that's not my question (I actually added some context for that)
I'm performing the validation below for empty strings:
if( "" == value ) { // is empty string }
In the past when fetching values from the db or deserializing objects from another node, this test failed, because the two string instances were indeed different object references, albeit they contained the same data.
So the fix for those situations was
if( "".equals( value ) ) { // which returns true for all the empty strings }
I'm fine with that. That's clearly understood.
Today this happened once again, but it puzzled me because this time the application is a very small standalone application that doesn't use network at all, so no new string is fetched from the database nor deserizalized from another node.
So the question is:
Under which OTHER circumstances:
"" == value // yields false
and
"".equals( value ) // yields true
For a local standalone application?
I'm pretty sure new String() is not being used in the code.
And the only way a string reference could be "" is because it is being assigned "" directly in the code (or that's what I thought) like in:
String a = ""; String b = a; assert "" == b ; // this is true
Somehow (after reading the code more I have a clue) two different empty string object references were created, I would like to know how
More in the line of jjnguys answer:
Byte!
EDIT: Conclusion
I've found the reason.
After jjnguy suggestion I was able to look with different eyes to the code.
The guilty method: StringBuilder.toString()
A new String object is allocated and initialized to contain the character sequence currently represented by this object.
Doh!...
StringBuilder b = new StringBuilder("h"); b.deleteCharAt( 0 ); System.out.println( "" == b.toString() ); // prints false
Mystery solved.
The code uses StringBuilder to deal with an ever growing string. It turns out that at some point somebody did:
public void someAction( String string ) { if( "" == string ) { return; } deleteBankAccount( string ); }
and use
someAction( myBuilder.toString() ); // bug introduced.
p.s. Have I read too much CodingHorror lately? Or why do I feel the need to add some funny animal pictures here?
解决方案String s = ""; String s2 = someUserInputVariale.toLowercase(); // where the user entered in ""
Something like that would cause
s == s2
to evaluate to false.Lots of code sill create new
Strings
without exposing the call tonew String()
.这篇关于当“ == s是假,但是“”等于真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!