Both of the following do the same
Option1
Given the following String somestring; void dosomething(){}
Consider the following code
if (somestring!=null&&!"".equals(somestring))
{
//if the string is not empty
dosomething();
}
Option 2
Create a function as follows
boolean isValid(String somestring)
{
if (somestring == null)
{
//the string is null. so it is not a valid string
return false;
}
//the string is not null
//eliminate empty characters at both ends
String newstring = somestring.trim();
if (newstring.equals(""))
{
//the string is an empty string
//an empty string is (not) valid
return false;
}
//it is a valid string because it is not null, it is not empty
return true;
}
Then you can do
if (isValid(somestring))
{
//if the string is not empty
dosomething();
}
To contrast
if (somestring!=null&&!"".equals(somestring))
{
//if the string is not empty
dosomething();
}
if (isValid(somestring))
{
//if the string is not empty
dosomething();
}