Comparing Strings

Comparing Strings
Slide Note
Embed
Share

Strings in Java are compared using the lexicographical order, where each character is compared index by index. Learn about the rules for comparing strings, including the Unicode character set used in Java. Understand how to compare primitive types and objects in Java, and the importance of implementing the Comparable interface for custom classes.

  • Java Programming
  • String Comparison
  • Lexicographical Order
  • Primitive Types
  • Comparable Interface

Uploaded on Feb 26, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Comparing Strings The compareTo interface

  2. Lexicographical Order Really means, alphabetical order , only our alphabet is much larger For Strings, the order of each character is:

  3. Comparing Strings Strings are compared index by index until one of the characters is found to be different testing vs. testinG The first letters that do not match are g and G G comes before g so testinG would come first If two strings match for some portion, Canucks vs. Canuck The empty character always wins, Canuck would come first It is usually a good idea to write strings on top of each other when comparing: C O M P A R I S O N C O M P A R I N G First character that does not match: N comes before S, so COMPARING is would come first

  4. Lexicographical Rules Java uses the Unicode character set for its strings. There are 65536, so a convenient summary is: Numbers come before letters Capitals before lower case Lower case are after capitals That s enough for most applications done by hand

  5. Which Comes First? 3456 or 56 First place they don t match: index 0 3 comes before 5, therefore, 3456 is less than 56 Remember, this is String comparison not numbers! abc or 123 First place they don t match: index 0 1 comes before a, therefore, 123 is less than abc Cat or Cat woman Cat is a match with Cat woman Cat woman still has characters remaining, Cat is less than Cat woman aaAA or aA First place they don t match: index 1 A comes before a, therefore, aA is less than aaAA

  6. Primitive Types vs. Objects You may have noticed so far that we ve only been comparing, int and double values. In java, the comparison operators only work for primitive types Objects that can be compared will have member functions instead For example, in the String class, all String objects have the methods: compareTo and equals All objects in Java that implement the Comparable interface have the method compareTo

  7. The Comparable Interface The AP subset includes the Comparable<T> interface There is only one method public int compareTo(Object other) Classes you create need to implements Comparable<YOUR_CLASS> public int compareTo(YOUR_CLASS other) implements Comparable public int compareTo(Object other) Then, in the method definition, (YOUR_CLASS)other; This interface lets you tap into methods in the java library that require comparisons Searching and sorting are the most common

  8. Comparing Strings Java can determine lexicographic order by using the compareTo method The compareTo method is not unique to String compareTo has the following requirements returns a positive value when this object is larger than the parameter given returns a negative value when this object is smaller than the parameter given returns zero when this object is equivalent to the parameter given (not necessarily the same object)

  9. compareTo abc .compareTo( def ); Returns a negative to indicate that abc is less than def def .compareTo( abc ); Returns a positive to indicate that def is larger than abc abc .compareTo( abc ); Returns 0 to indicate that the strings are equal compareTo returns 3 values: An integer less than 0 to indicate the calling string is less than the argument string An integer greater than 0 to indicate the calling string is greater than the argument string 0 to indicate the calling string is equal to the argument string

  10. compareTo Consider two Strings: String one = new String( one ); String two = new String( two ); if( one < two ) Compares the physical address of one and two Useful in comparing references with == and != Instead you have to say: if( one.compareTo( two ) < 0 )// one < two There is also an equals method to compare two strings

  11. compareTo Primitive Types Objects one < two one.compareTo( two ) < 0 one <= two one.compareTo( two ) <= 0 one > two one.compareTo( two ) > 0 one >= two one.compareTo( two ) >= 0 one == two one.compareTo( two ) == 0 one != two one.compareTo( two ) != 0 Always being compared with 0 Operator stays the same

More Related Content