go to previous page   go to home page   go to next page

Answer:

Yes. It is a String object that happens to contain no characters. It has all the methods that any String object does.


Detecting the Empty String

Here is an outline of our method:

static int length( String str )
{
  if ( str is the empty string )
    return 0;
  
  else
    return 1 + length of all but first character of str;
}

Detecting the empty string is a problem. We can't use

if ( str.length() == 0 )

because the base case of a recursive method should not use the method itself. (Remember: we are pretending that the length() method does not already exist.)

Starting with Java 6.0, the String class has an isEmpty() method which returns true if the string is empty. If your version of Java is earlier than 6.0, you will need to use the equals() method.


QUESTION 6:

If str refers to an empty String object, what is the value of

str.equals("")

and what is the value of

str == ""

go to previous page   go to home page   go to next page