"" != null



  • Found in a java source file while doing code cleanup:

    if(strX != "") // check if strX has been assigned a value or not
    {
    /do something here/
    ........
    }
    else
    {
    strX = null;
    }



  • Actually, that's not a test for null - in Java, when you compare an object against another object with == (or !=), you are testing to see if' it's the same INSTANCE, and not equality. So...

      String s ="hello";
    String t = "hello";
    if (s == "hello") // false
    if (t == s) // false
    if (s.equals(t)) // true
    if (s.equals("hello")) // true

    In this case, the code is essentially doing: if (theString is not the literal: "") then xxx else set theString = null;



  • @snoofle said:

    Actually, that's not a test for null - in Java, when you compare an object against another object with == (or !=), you are testing to see if' it's the same INSTANCE, and not equality. So...

      String s ="hello";
    String t = "hello";
    if (s == "hello") // false
    if (t == s) // false
    if (s.equals(t)) // true
    if (s.equals("hello")) // true

    In this case, the code is essentially doing: if (theString is not the literal: "") then xxx else set theString = null;

    bleah - editing timeout...

       String s = "";

       if (s == "") // false - s has the VALUE of  an empty string, but is not the INSTANCE of ""

       if (s.equals("")) // true



  • Yup, that's true. So the code does:

    strX = null;

    Everything in  "do something here" is never executed

    Brillant



    Mike Rod



  • @Mike Rod said:


    Everything in  "do something here" is never executed


    Or is it always executed? Hmmm.......



  • @snoofle said:

      String s ="hello";
      String t = "hello";
      if (s == "hello") // false
      if (t == s) // false
      if (s.equals(t)) // true
      if (s.equals("hello")) // true

     

    clooose but actually you're example would confuse you :P  all of those should evaluate to true :P

    When you define a string at compile time, then java interns it.  This means both s and t are references to the same object in memory.  For this reason s == t will evaluate true.  also, in if(s == "hello") the hello is also a refrence to an interned string.  meaning that will also evaluate to true :P

    however, if you did

    String a = "hel"

    String b = "lo"

    String c = a + b;

    if(s == c) will evaluate false.

    it's wierdness like this that makes java strings confusing to newcomers :P



  • @Mike Rod said:

    Yup, that's true. So the code does:
    strX = null;
    Everything in  "do something here" is never executed

    Not necessarily.  Depends on the context.  Specifically, if the surrounding code looks like this:

    String strX = "";
    if(somecondition()) strX = somefunction();

    if(strX != "") // check if strX has been assigned a value or not
    {
    /do something here/
    ........
    }
    else
    {
    strX = null;
    }

    In this case, /do something here/ is run any time somecondition() is false.  This is amateur code, but it's probably functional.  I'm sure I personally wrote some very similar code when I first started with Java in highschool.



  • @merreborn said:

    @Mike Rod said:
    Yup, that's true. So the code does:
    strX = null;
    Everything in  "do something here" is never executed

    Not necessarily.  Depends on the context.  Specifically, if the surrounding code looks like this:

    String strX = "";
    if(somecondition()) strX = somefunction();

    if(strX != "") // check if strX has been assigned a value or not
    {
    /do something here/
    ........
    }
    else
    {
    strX = null;
    }

    In this case, /do something here/ is run any time somecondition() is false.  This is amateur code, but it's probably functional.  I'm sure I personally wrote some very similar code when I first started with Java in highschool.


    Well, you know, its boolean: If you don't like my comment, just negate it

    Mike Rod



  • @Mike Rod said:

    @merreborn said:
    @Mike Rod said:
    Yup, that's true. So the code does:
    strX = null;
    Everything in  "do something here" is never executed

    Not necessarily.  Depends on the context.  Specifically, if the surrounding code looks like this:

    String strX = "";
    if(somecondition()) strX = somefunction();

    if(strX != "") // check if strX has been assigned a value or not
    {
    /do something here/
    ........
    }
    else
    {
    strX = null;
    }

    In this case, /do something here/ is run any time somecondition() is false.  This is amateur code, but it's probably functional.  I'm sure I personally wrote some very similar code when I first started with Java in highschool.


    Well, you know, its boolean: If you don't like my comment, just negate it

    Mike Rod

    Did someone say Bool? Isn't truth really file-not-found?



  • File-not-found is not true nor false, is a completely new state when a program reaches maximum wtf-ness

    Mike Rod



  • @sparkfizt said:

    @snoofle said:

      String s ="hello";
      String t = "hello";
      if (s == "hello") // false
      if (t == s) // false
      if (s.equals(t)) // true
      if (s.equals("hello")) // true

    clooose but actually you're example would confuse you :P  all of those should evaluate to true :P

    When you define a string at compile time, then java interns it.  This means both s and t are references to the same object in memory.  For this reason s == t will evaluate true.  also, in if(s == "hello") the hello is also a refrence to an interned string.  meaning that will also evaluate to true :P

    however, if you did

    String a = "hel"

    String b = "lo"

    String c = a + b;

    if(s == c) will evaluate false.

    it's wierdness like this that makes java strings confusing to newcomers :P

    Of course you are right - I knew that, got lazy and engaged mouth before brain - gaaaa - I am turning into a human wtf - must....   reboot.... brain..... *****faschizzle*****

    *silence in snoof-land*

     



  • @Mike Rod said:

    File-not-found is not true nor false, is a completely new state when a program reaches maximum wtf-ness

    Mike Rod

    I beg to differ: http://thedailywtf.com/forums/thread/80084.aspx

     



  • I have a state-of-the-art brain - it reboots quickly, but the long-term-storage-recall circuits seem to need some work <wink>



  • @Mike:Yes that's right.
    And what's worse is this.
    strX starts off as null, and is reassigned to null after the test fails. WTF!



  • @snoofle said:

    @Mike Rod said:

    File-not-found is not true nor false, is a completely new state when a program reaches maximum wtf-ness

    Mike Rod

    I beg to differ: http://thedailywtf.com/forums/thread/80084.aspx

     



    Hehe, yes, that's the post I'm talking about: File not found is a third state, and the program clearly is close to maximum wtf-ness. All the conditions are met  :-)

    Mike Rod.


  • Stuff like this is why I hate when I have to use Java. It actually makes me enjoy returning to C++.



  • @Isuwen said:

    Stuff like this is why I hate when I have to use Java. It actually makes me enjoy returning to C++.

    Actually, you can get worse code in C++.

    I used to love operator overloading, until I realized what evil could be done with it :-)

    I've found that most language features that are syntatical sugar tend to be dangerous and misleading at times.  I'd even have prefered that java used something other than "+" as the string concatination operator. # might have been a good choice, since its not used for ANYTHING else in Java (AFAIK).
    I mean, wouldn't "The value of a + b is " # a + b be more understandable?


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.