Using == when really should be comparing objects with equals()
String a = "aa";
String b = "bb";
if( a == b ) // this is wrong
{
// do something
}
The previous example of course really should be like this:
if( a.equals( b ) ) // this is correct
{
// do something
}
Here's an additional tip, when comparing string literals to some String variable its quite useful to write the comparison like this:
"foobar".equals( a );
This makes it possible to leave out the null check, which in turn results in cleaner code.
Tags: Java Programming Software
No comments:
Post a Comment