0

I'm learning java now. I have a book which is up to SE6 date. Now there is an exercise that asks me:

Write a method that takes two String arguments and uses all boolean comparisons to compare the two Strings and print the results. In main(); call your method with some different String objects.

When I tried:

public static void compare(String a, String b){
    System.out.println(a>b);
}

I got error said > operator is not valid for type String

Now my question is - if the book is out of date and something changed since, or am I misunderstanding something in the task?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • simply use a.equals(b) – Pavneet_Singh Oct 05 '16 at 17:04
  • The boolean operators are indeed not valid for String objects, so no surprises there. The exercise does state Boolean comparisons, not operators, so I imagine the intent is to look at how the String Boolean methods work (equals, contains, startsWith, etc.) – K Richardson Oct 05 '16 at 17:06
  • u cant use > , < on objects. String is an object in Java. So use .compareTo if you want to compare. (corrected my comment after reading the Matthew comment) – PKR Oct 05 '16 at 17:06
  • 1
    Why is everybody recommending `equals`? `compareTo` is what you are looking for. – Matthew Diana Oct 05 '16 at 17:07
  • `a > b` was never designed to work with Strings – TheLostMind Oct 05 '16 at 17:09
  • 1
    "and uses all boolean comparisons" since you can't simply use `<`, `>` on objects (which Strings are) and [`==` doesn't necessary do what you may think it does for references](http://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) you can try using these boolean operators not on strings directly, but on characters they hold since there is no problem with using boolean comparisons with `char`. – Pshemo Oct 05 '16 at 17:11
  • It’s a bit unclear. My guess is that they are after the semantics of =, !=, >, <, >= and <=, knowing those operators won’t work. If my guess is correct, @MatthewDiana is leading you on the right track. – Ole V.V. Oct 05 '16 at 17:32
  • A different interpretation would be that they are after `startsWith()`, `equalsIgnoreCase()` and the other instance methods that take a String argument and return a boolean. Can you tell from the context in the book in which they give the exercise? – Ole V.V. Oct 07 '16 at 21:41

2 Answers2

1

I guess this task was meant for you to provide your own implementation of string comparison, so for example:

for (int i = 0; i < Math.max(a.length(), b.lentgh(); i++) {
  if (a[i] < b[i]) {
    System.out.println("a < b");
    return;
  }
  // ... 

Strings are objects, not primitives, and cannot be compared with comparison operators. There is a compareTo(String anotherString) method build into String which returns a number depending on which string is lexically greater.

Tomasz R
  • 300
  • 1
  • 11
  • 1
    Leaving aside the guesses about the interpretation of the assignemnt, I think you should at least use `Math.min(a.length(), b.lentgh())` rather than max, or there’s a great risk of a `StringIndexOutOfBoundsException`. – Ole V.V. Oct 05 '16 at 17:34
0

You cannot use relational operators( <, <=, >,>=) on Strings in Java. Java does not rely on operator overloading.

You can use compareTo method for comparing Strings: The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.