If I have two byte[]
arrays, is there a built-in function to compare them ala C's memcmp()
?
views:
480answers:
4
+4
A:
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.
Uri
2009-07-07 02:31:46
I'm curious for a case where someone would null terminate their own array in Java
matt b
2009-07-07 04:07:26
I never tried that, but I would assume that maybe if one invoked a native method in C and used a buffer...
Uri
2009-07-07 04:08:51
Where did strcmp come from? The question was about memcmp, and I can't see strcmp in edit histories either.
laalto
2009-07-07 10:50:21
+1
A:
[Arrays.equals][1]
[1]: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#equals(byte[], byte[])
oykuo
2009-07-07 02:32:21
Probably better to ask as a separate question or search SO for an existing answer...
Peter
2009-07-07 02:36:03
+2
A:
The java.util.Arrays.equals(byte[], byte[]) method is your friend.
lavinio
2009-07-07 02:47:47