Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: string handling

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default string handling

    can someone please make me understand when to initialise a string 'null' and when not..??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: string handling

    null is a value that object reference variables can have. Since a String class instance is an object, any variable that is a reference to a String can have a null value.

    The compiler wants local (defined inside a method) variables to be initialized to a value. Class level variables are given default values which is null for object reference variables.

    It's best to always initialize variables.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shilpy (September 24th, 2014)

  4. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: string handling

    Quote Originally Posted by shilpy View Post
    can someone please make me understand when to initialise a string 'null' and when not..??
    When you need the String variable to be null you should set its value to null. If you need it to be a String you should set it to a String.

    But more seriously, we can not answer your question because it depends on what you want and what you need, and the answer changes depending on the rest of your program.

  5. The Following User Says Thank You to Cornix For This Useful Post:

    shilpy (September 24th, 2014)

  6. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: string handling

    1. String a = "";
    2. String b = null;

    Line 1 create a String object and assigns it the reference 'a'.
    Line 2 only creates a reference( 'b' ) to a String object. It will need to be assigned a String object before it can be used. If you try to use before-hand, you'll get a null pointer exception.

  7. The Following User Says Thank You to kaufenpreis For This Useful Post:

    shilpy (September 24th, 2014)

  8. #5
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default Re: string handling

    Quote Originally Posted by kaufenpreis View Post
    1. String a = "";
    2. String b = null;

    Line 1 create a String object and assigns it the reference 'a'.
    Line 2 only creates a reference( 'b' ) to a String object. It will need to be assigned a String object before it can be used. If you try to use before-hand, you'll get a null pointer exception.
    NULL POINTER EXCEPTION ??? MEANS

    --- Update ---

    i still don't get it..!!

  9. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: string handling

    NullPointerException is a java class that is documented in the API doc. Open the API doc page at: Java Platform SE 7
    find NullPointerException in the lower left frame, click on the link and get the doc for that class in the main frame.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #7
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: string handling

    Quote Originally Posted by Norm View Post
    null is a value that object reference variables can have. Since a String class instance is an object, any variable that is a reference to a String can have a null value.

    The compiler wants local (defined inside a method) variables to be initialized to a value. Class level variables are given default values which is null for object reference variables.

    It's best to always initialize variables.
    Maybe not _always_.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: string handling

    If you always do it, then the chances of not doing it when it is needed is removed. It removes one more decision to make when writing code.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: string handling

    Quote Originally Posted by Norm View Post
    If you always do it, then the chances of not doing it when it is needed is removed. It removes one more decision to make when writing code.
    There isn't a hard-and-fast rule, but arbitrarily assigning defaults to those properties/fields that are already set to defaults for you makes larger code.

    If you use an IDE it will warn you of possible null dereferences or using an unassigned variable.

    For objects this is silly anyway, as you are probably going to assign it to null (unless you are statically initializing it to something reasonable, which is a different case) in which case, nothing changes at runtime with respect to accidentally using it.

    So, this leaves us with non-object fields. Which should either be initialized in a constructor, statically if they are defaults or final (but then they are defaults or constants anyway, so initialization makes sense) or via lazy initialization in a getter (or an explicit setter, of that makes sense.)

    The point I'm making is that this is contextual, and it is up to the big throbbing brain this sits between the computer and the keyboard to make the right choices. The code should reflect the intent of the coder, and non-meaningful defaults do not reflect how those fields are to be used.

    Finally, default initializing can lead to costly and obscure runtime bugs that I'd rather find at compile time, or explicitly exercise with unit tests. That is, the assertion that initializing fields lowers instances of defects is not true; you just introduce a different set of bugs.

    I'll point out this is exactly the sort of thing that does not happen with C# properties. You can't just define a property and abuse it like this; you have to declare the property and force yourself to refer to it only in this manner.

    My advice is to be disciplined in this case, and treat your fields as fields. Even if the language allows you to be lazy.

Similar Threads

  1. Help with exception handling.
    By K0414 in forum Exceptions
    Replies: 3
    Last Post: April 19th, 2013, 02:18 PM
  2. I/O AND EXCEPTION HANDLING need help
    By awood in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 3rd, 2013, 04:22 PM
  3. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  4. Need help with exception handling for a string
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2011, 06:59 AM
  5. Output String Handling in Java
    By merry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2010, 09:59 PM