what's the deference between str=null and str=" " ????????

Discussion in 'Java' started by David, Aug 3, 2003.

  1. David

    David Guest

    2 definitions:
    String str1=null;
    String str2="";
    what's the deference??

    i use the first definition, and then give str1 a value: str1="myName";
    however, when i send this string from server to client, it displays:
    nullmyName.

    --
    Bi Hongliang / David
    David, Aug 3, 2003
    #1
    1. Advertising

  2. David <> scribbled the following:
    > 2 definitions:
    > String str1=null;
    > String str2="";
    > what's the deference??


    There's a pretty big difference. The first makes str1 refer to no String
    object at all. The second makes str2 to refer to a valid String object,
    which is exactly 0 characters long.

    > i use the first definition, and then give str1 a value: str1="myName";
    > however, when i send this string from server to client, it displays:
    > nullmyName.


    That's because most of the java.io.* classes treat a String value of
    null as a valid String, consisting of the characters 'n' 'u' 'l' 'l'.
    This does *NOT* mean that it *is* that String value.

    See for yourself:

    System.out.println(str2.length()); // prints 0
    System.out.println(str1.length()); // throws a NullPointerException

    You could benefit from reading a textbook about Java programming.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "There's no business like slow business."
    - Tailgunner
    Joona I Palaste, Aug 3, 2003
    #2
    1. Advertising

  3. David <> horrified us with:

    > 2 definitions:
    > String str1=null;
    > String str2="";
    > what's the deference??
    >
    > i use the first definition, and then give str1 a value: str1="myName";
    > however, when i send this string from server to client, it displays:
    > nullmyName.



    Like you're likely to hear from 100 others, str1 is a null reference (think
    /pointer to no object/) and str2 is a reference to an empty string.

    But as you learn about strings, you're gonna come face to face with another
    issue. Because strings are immutable (once created, you cannot change them
    in /any/ way, neither length nor content), the compiler is free (and
    required) to share data whenever possible. Odd? Not really, here:

    String str1 = "hello";
    String str2 = "hello";

    Both of these references point to the /exact/ same string. Same 1 object!
    The compiler shared the data between them. That means that

    (str1 == str2) is true ...and...
    str1.equals(str2) is true

    This can get confusing for a newbie (sorry if you aren't one). Take for
    example the following:

    String str1 = "hello";
    String str2 = "hell";
    str2 += "o";

    They are two /different/ strings, both containing the same value. So:

    (str1 == str2) is false ...and...
    str1.equals(str2) is true

    Sorry if this explanation was beneath you: it seemed like something worth
    saying.
    Thomas G. Marshall, Aug 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web Developer

    reference and deference

    Web Developer, Aug 9, 2003, in forum: C++
    Replies:
    1
    Views:
    396
    Rob Williscroft
    Aug 9, 2003
  2. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,648
    Oliver Wong
    Aug 3, 2006
  3. Replies:
    11
    Views:
    580
    Ben Pfaff
    Sep 16, 2006
  4. Replies:
    0
    Views:
    303
  5. Replies:
    0
    Views:
    291
Loading...

Share This Page