Results 1 to 8 of 8

Thread: String question

  1. #1
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default String question

    Is str.length() == 0 the same as str = null?

    edit: also another question, will "test".substring(1,1) throw an error because of the same start & end index?
    Last edited by Blasz; 09-16-2010 at 02:01 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,407
    Rep Power
    25

    Default

    No. The first requires that there be a String object. The second tests if a String reference variable points at a object or not.

    str = ""; // set String with length 0

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    No.
    If str was null then that line (str.length() == 0) would throw a NullPointerException.

  4. #4
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Thanks guys.
    I editted by post with another question btw

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    You could read the API docs you know.
    It's just as easy (or even easier) as posting here.

  6. #6
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Sorry for being a nuisance, I actually checked the page on the function to see what it does but didn't look at the throws part for some reason...All good now.

  7. #7
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Try it out.

    Java Code:
    System.out.println("test".substring(1,1));
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Quote Originally Posted by Blasz View Post
    Sorry for being a nuisance, I actually checked the page on the function to see what it does but didn't look at the throws part for some reason...All good now.
    The exception is pretty irrelevant to the bit of code you posted, though.

Similar Threads

  1. A beginner's question on String matching
    By nassar in forum New To Java
    Replies: 28
    Last Post: 05-23-2010, 02:44 PM
  2. String question
    By Raphy00 in forum New To Java
    Replies: 5
    Last Post: 05-12-2010, 08:53 AM
  3. JOptionPane String to int question
    By blackstormattack in forum New To Java
    Replies: 3
    Last Post: 03-08-2009, 12:14 AM
  4. String to Expression? calculator app question
    By Belbis in forum New To Java
    Replies: 2
    Last Post: 12-24-2008, 07:13 AM
  5. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •