1 / 17

6.4 Comparing objects

6.4 Comparing objects. Comparing Strings. The general subject of comparing objects, or object references, can be introduced concretely with strings. Recall that String is a class, and so strings themselves are instances of this class. The String class implements a method called equals().

israel
Download Presentation

6.4 Comparing objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6.4 Comparing objects

  2. Comparing Strings • The general subject of comparing objects, or object references, can be introduced concretely with strings. • Recall that String is a class, and so strings themselves are instances of this class. • The String class implements a method called equals(). • This is the method that checks to see whether the contents of two different string objects are the same.

  3. Comparing Equal Strings • Let two strings be declared and initialized as follows: • String myString = “Hello World”; • String yourString = “Hello World”; • The situation can be diagrammed in this way:

  4. Comparing Equal Strings • Two separate objects exist, containing the same sequence of characters. • Here is an if statement containing a call to the equals() method. • In this situation the condition evaluates to true. • if(myString.equals(yourString))

  5. Comparing Unequal Strings • If these string values were assigned to the two references, however, the equals() method would return false: • myString = “Tra-la-la”; • yourString = “La-de-dah”;

  6. Complication when comparing Strings • The complication comes when you consider the use of the operator ==. • This tests for equality of reference, not equality of contents. • In other words, it tests to see whether the objects themselves are the same, not whether their contents are the same. • I consider this to be a major pitfall for new programmers, so make sure to understand this.

  7. Comparing Strings with == • Let the two strings now be declared and initialized as follows: • String myString = “Hello World”; • String yourString = myString; • if(myString == yourString) • The situation can be diagrammed in this way:

  8. Comparing Strings with ==, cont. • Only one object exists, with two references to it. • Here is an if statement containing a test of equality of reference. • In this situation the return value would be true. • if(myString == yourString)

  9. Other methods for comparison • There are other methods in the String class that support various kinds of comparisons. • Among them are compareTo() and equalsIgnoreCase(). • The first of these allows you to compare the contents of strings to see which might come first alphabetically. • The second method allows you to check for equality of contents of strings where small and capital letters don’t make a difference. This is done by returning a boolean result.

  10. Other methods for comparison, cont. • This can be useful when taking in input from users. • Complete information on each of these methods can be found in the Java API documentation.

  11. Comparing Equality for Objects • The same observations that were made for comparing equality of strings can be made for comparing equality of objects in general. • Suppose you have the following two declarations and initializations: • Point2D.Double myPoint = new Point2D.Double(10, 20); • Point2D.Double yourPoint = new Point2D.Double(30, 40);

  12. Comparing Equality for Objects, cont. • The contents are different and the objects are different. Both the equals() method and the == would return false. • Suppose instead that the initializations were as follows: • Point2D.Double myPoint = new Point2D.Double(10, 20); • Point2D.Double yourPoint = new Point2D.Double(10, 20);

  13. Comparing Equality for Objects, cont. • Now there are two different objects with the same contents. • The equals() method would return true while the == would return false. • Finally consider this case: • Point2D.Double myPoint = new Point2D.Double(10, 20); • Point2D.Double yourPoint = myPoint;

  14. Comparing Equality for Objects, cont. • Now both equals() and == would return true. • The references refer to the same object, and by definition the contents of the object referred to by the references are the same.

  15. equals() and == for your own classes • This description of equals() and == applies to system supplied classes. • However, beware: equals() will not work this way with classes you have written. • For example, suppose I have the following objects, created using my own class: • Shampoo myShampoo = new Shampoo(); • Shampoo yourShampoo = new Shampoo();

  16. equals() and == for your own classes, cont. • No matter what the contents of these objects might be, the equals() method will work exactly like the ==. • In other words, for your classes it is possible to call the equals() method, but it only tests for equality of reference, not equality of contents.

  17. equals() and == for your own classes, cont. • Your class inherits the method equals() from a class above it in the hierarchy. • However, the inherited implementation does not work in the desired way. • How to write an equals() method will be covered in a future unit.

More Related