Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When "" == s is false but "".equals( s ) is true

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?

like image 846
OscarRyz Avatar asked Jul 10 '09 18:07

OscarRyz


People also ask

Is it possible for equals () to return false even if contents of two objects are same?

Yes. You can also override the equals() method and play with it.

How to check if 2 strings are equal or not?

Java String equals() Method The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.

What makes two objects equal in Java?

In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.

How to check equality of two strings in Java?

You can check the equality of two Strings in Java using the equals() method. This method 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.


1 Answers

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 to new String().

like image 156
jjnguy Avatar answered Sep 19 '22 02:09

jjnguy