Equality of Objects

One caution must be observed with Strings vs. Objects:

Suppose we had a function member which tests membership:

static boolean member(int sought, IntList L)
{
while( !isEmpty(L) )
  {
  if( sought == first(L) )
    return true;	// sought is found
  L = rest(L);
  }
return false;		// sought never found
}

 

This definition would not work well for Strings:

static boolean member(String sought, StringList L)
{
while( !isEmpty(L) )
  {
  if( sought == first(L) )
    return true;	// sought is found
  L = rest(L);
  }
return false;		// sought never found
}

Why not?

Next Slide Previous Slide Contents