javacposixlibcmemcmp

equivalent of memcmp() in Java?


If I have two byte[] arrays, is there a built-in function to compare them ala C's memcmp() ?


Solution

  • There's Arrays.equals().

    I don't know whether the JVM implementation actually optimizes this if a corresponding instruction exists in the hardware, but I doubt it.

    Also, if I remember my C correctly, strcmp works up to a null terminator (making it useful for C strings), The Arrays version will compare the entire array since Java programmers rarely bother with null-terminated arrays. You could easily write your own function, though, if you care about the null-terminator.