-4
private static int myCompare(String a, String b) {

    /* my version of the compareTo method from the String Java class */

    int len1 = a.length();
    int len2 = b.length();

    if (len1 == len2) { 

        for (int i = 0; i < a.length(); i++) { 

            int intValueOfStringA = (int) a.charAt(i); 
            int intValueOfStringB = (int) b.charAt(i); 

            if (intValueOfStringA != intValueOfStringB) {

                return intValueOfStringA - intValueOfStringB;
            }
        }
    }
    return -1;
}

EDIT:

I apologize for not framing my question properly the first time around. Going forth, I'll be more careful to precisely frame up my question. For my project, I needed a method that compared two strings to sort them alphabetically, so instead of googling (like most sane people) I quickly wrote one (and not a good one too). Going through the String Class, specifically the compareTo method, I found that it used more variables than I did. So, I was confused as to how that method was efficient than mine. Later on, I understood that it handles sorting much more efficiently than mine did. To elaborate, if two strings were of different length then my code would automatically return -1, which gives no information on whether or not the second string would precede or follow the first string.

Further, thanks to DM for pointing out that a String object obviously has direct access to its private variable and hands only a copy while making the toCharAt call, driving home the point that the compareTo method is indeed efficient and offers more utility than my temerarious myCompare method.

I was in a real hurry but also curious as to why the compareTo method was better than myCompare method, causing me to inadvertently rush with my question without any research or thought. LOL, the creators do know their methods. Like I said, in future, I'll research first and post better questions.

Avid Programmer
  • 1,081
  • 1
  • 11
  • 20
  • 1
    You can profile the code yourself and perform benchmarking tests... Or are you asking us to do that for you? – OneCricketeer Feb 06 '17 at 19:04
  • do you know the memory size difference between an int and a char and other variables? knowing that will help you to understand the memory cost difference – RAZ_Muh_Taz Feb 06 '17 at 19:05
  • I could be mistaken but I would imagine these have the same O(N) linear complexity as far as time is concerned. Memory, I'm not sure. – Alex Feb 06 '17 at 19:06
  • @cricket_007 How do I profile and benchmark? – Avid Programmer Feb 06 '17 at 19:07
  • There are several [java profilers](http://stackoverflow.com/questions/948549/open-source-java-profilers) – OneCricketeer Feb 06 '17 at 19:08
  • 1
    You can't compare efficiency of code that doesn't produce the same result. What would be the point? Apples and Orange!! If you don't believe me when I say *different* result, try comparing `"AA"` and `"B"`. – Andreas Feb 06 '17 at 19:14
  • Andreas is right: the two methods do not compare String objects in the same way. Were they supposed to? If so then you need to make yourself acquainted with unit testing and learn how to write a suite full of tests which attack your method from all angles (good inputs, bad inputs, big, small, left, right, positive, negative, whatever inputs) and confirm that your code does exactly what you intend it to do. Then, once you've got something that you can prove is working, and only then, might you want to look into JMH (Java Microbenchmark Harness). – Bobulous Feb 06 '17 at 19:33

1 Answers1

0

Well, it appears that code 2 does use more memory. But that's because it uses toCharArray() which copies the contents of the Strings. The String compareTo() method has access to the private variables of String, and doesn't have to do that.

Other than that, they are very similar, although your method sorts primarily on String length instead of alphabetical order. I believe they would both be O(n) - they have to potentially check each character of both Strings. Code 2 also involves copying the characters in both Strings, but those are also O(n) operations, and O(n) + O(n) = O(n).

D M
  • 1,410
  • 1
  • 8
  • 12
  • Nice explanation. Could you elaborate on what you meant by String has access to private variables of String object? I think this is what I was going after. Thanks! – Avid Programmer Feb 06 '17 at 22:02
  • Most relevantly, it has access to String's private `value` variable, which is the character array that contains the actual characters in the String. (It's private because if it gave direct access to the array, other code might *change* what's in the array, and that would break the guarantee that Strings are immutable. That's why everyone else can only get a copy of that array with `toCharArray()`- changing the copy won't change the original.) – D M Feb 06 '17 at 22:21