You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jagdish Arora <ja...@hotmail.com> on 2003/01/17 09:30:49 UTC

java.lang.String question disguised as an ActionForm question

ok, though this problem came up dealing with ActionForms, I completely
understand that this is a fundamental Java question, nothing more, so I will
pose it as that only.  Also, I am prepared for taking some
thisQuestion(OrYou)DoesntDeserveToBeHere flak:

        String s1 = "" + null;
        System.out.println ("s1: " + s1);
        if (s1 == null)
            System.out.println ("s1 is null");
        else
            System.out.println ("s1 is not null");

produces output:
        s1: null
        s1 is not null

While if I replace the first line by, String s1 = "";
then it produces output:
        s1:
        s1 is not null

(which I have no problem with).

Question: Why does the 1st line of the output of the first case read s1:
null  ?

Amrinder

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: java.lang.String question disguised as an ActionForm question

Posted by Mark Galbreath <ma...@qat.com>.
That's pretty curious; I would never have thought to do this, and wonder why
you are.  Anyway, it seems pretty obvious that s1 == <empty String> + null
and s1 != null, so it fails the s1 == null test.  You, of course, cannot see
the System.out.println of an empty String, with or without a null
concatenated.

Mark

-----Original Message-----
From: Jagdish Arora [mailto:jagdishsarora@hotmail.com] 
Sent: Friday, January 17, 2003 3:31 AM

ok, though this problem came up dealing with ActionForms, I completely
understand that this is a fundamental Java question, nothing more, so I will
pose it as that only.  Also, I am prepared for taking some
thisQuestion(OrYou)DoesntDeserveToBeHere flak:

        String s1 = "" + null;
        System.out.println ("s1: " + s1);
        if (s1 == null)
            System.out.println ("s1 is null");
        else
            System.out.println ("s1 is not null");

produces output:
        s1: null
        s1 is not null

While if I replace the first line by, String s1 = "";
then it produces output:
        s1:
        s1 is not null

(which I have no problem with).

Question: Why does the 1st line of the output of the first case read s1:
null  ?

Amrinder

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: java.lang.String question disguised as an ActionForm question

Posted by Andrew Hill <an...@gridnode.com>.
Because in a string concatenation operation null will be converted to "null"
automatically.
Useful in debugging this is.
ie:
log.debug("value of bob=" + bob);

is a lot easier than always having to write

if(bob == null)
  log.debug("value of bob=null");
else
  log.debug("value of bob=" + bob);

Which you would have to do if it was converted to an empty string, or threw
a NullPointerException if you tried to output it...

-----Original Message-----
From: Jagdish Arora [mailto:jagdishsarora@hotmail.com]
Sent: Friday, 17 January 2003 16:31
To: Struts Users Mailing List
Subject: java.lang.String question disguised as an ActionForm question



ok, though this problem came up dealing with ActionForms, I completely
understand that this is a fundamental Java question, nothing more, so I will
pose it as that only.  Also, I am prepared for taking some
thisQuestion(OrYou)DoesntDeserveToBeHere flak:

        String s1 = "" + null;
        System.out.println ("s1: " + s1);
        if (s1 == null)
            System.out.println ("s1 is null");
        else
            System.out.println ("s1 is not null");

produces output:
        s1: null
        s1 is not null

While if I replace the first line by, String s1 = "";
then it produces output:
        s1:
        s1 is not null

(which I have no problem with).

Question: Why does the 1st line of the output of the first case read s1:
null  ?

Amrinder

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>