How do you figure out of the string isn't equal to anything

In PHP, I could just type:

if(!variable){
   do something
}

And that meant that if there wasn't anything in the variable, then it would do what I wanted.

Can we do that is JSP (especially for strings)?
Similarly, can I make an if statement that check to see if a string IS NOT equal to something?

I know I can;t type:

if(variable != "xyz"){
   do something
}

Thanks,
MD
mderbinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ldbkuttyCommented:
In JSP, you do it in this ways:

>> Can we do that is JSP (especially for strings)?
if(variable == null || variable.equals("")) {
  // something ...
}

>> can I make an if statement that check to see if a string IS NOT equal to something?
if(!variable.equals("xyz")) {
  // something ...
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodingExpertsCommented:
You can see all the string related functions here ...
http://www.acunia.com/wonka/javadoc-0.9.5/java/lang/String.html
0
KartikShahCommented:
Hi,

>> if(!variable){
>>    do something
>> }

PHP, in the above code check for two cases:
1. Whether the variable has been declared or no
2. If it is declared, then whether it is null or no

In Java/JSP, the first condition is not possible, as Java needs the variables to be declared before hand.

For the second case, you can do the check as:

if(variable == null || "".equals(varialble))
{
    //do something
}
else (!"xyz".equals(variable))
{
   //do something
}

You will notice that I am comparing a string litral to the variable instead of the variable to the string literal, thats just because of good coding practices :)

Kartik
0
siliconeagleCommented:
Another equivalet way is :-
if((variable!=null) && !variable.equals("")){...}
because the && oprerator is optimised, if the first condition is false the the second need not be evaluated so the code above does not generate a nullPointerException even when variable is null.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.