fork(68) download
  1. class StringCompareExample {
  2. public static void main(String args[]){
  3. String s1 = "Project"; String s2 = "Sunject";
  4. verboseCompare(s1, s2);
  5. verboseCompare(s2, s1);
  6. verboseCompare(s1, s1);
  7. }
  8.  
  9. public static void verboseCompare(String s1, String s2){
  10. System.out.println("Comparing \"" + s1 + "\" to \"" + s2 + "\"...");
  11.  
  12. int comparisonResult = s1.compareTo(s2);
  13. System.out.println("The result of the comparison was " + comparisonResult);
  14.  
  15. System.out.print("This means that \"" + s1 + "\" ");
  16. if(comparisonResult < 0){
  17. System.out.println("lexicographically precedes \"" + s2 + "\".");
  18. }else if(comparisonResult > 0){
  19. System.out.println("lexicographically follows \"" + s2 + "\".");
  20. }else{
  21. System.out.println("equals \"" + s2 + "\".");
  22. }
  23. System.out.println();
  24. }
  25. }
Success #stdin #stdout 0.06s 380224KB
stdin
Standard input is empty
stdout
Comparing "Project" to "Sunject"...
The result of the comparison was -3
This means that "Project" lexicographically precedes "Sunject".

Comparing "Sunject" to "Project"...
The result of the comparison was 3
This means that "Sunject" lexicographically follows "Project".

Comparing "Project" to "Project"...
The result of the comparison was 0
This means that "Project" equals "Project".