Thursday, 22 December 2011

1. Which is the best?


Qus:
if(object.equals("hello"))
{
//do something;
}

if("hello".equals(object.toString()))
{
//do something;
}

sol:
The second one is good for every time. Because  if "object" may have "null" then when compare by taking first one it throws a NullPointerException.

If we compare with second one it gives "TRUE/FALSE". Means never Fail this condition. Like this we have to check conditions.

No comments:

Post a Comment