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

Posted on 2004-11-07
Last Modified: 2010-04-01
In PHP, I could just type:

   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

Question by:mderbin
    LVL 32

    Accepted Solution

    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 ...
    LVL 6

    Expert Comment

    You can see all the string related functions here ...
    LVL 1

    Expert Comment


    >> 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 :)

    LVL 2

    Expert Comment

    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.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    It Is not possible to enable LLDP in vSwitch(at least is not supported by VMware), so in this article we will enable this, and also go trough how to enabled CDP and how to get this information in vSwitches and also in vDS.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now