Compare equality of char[] in C

char charTime[] = “TIME”; char buf[] = “SOMETHINGELSE”; C++ and C (remove std:: for C): bool equal = (std::strcmp(charTime, buf) == 0); But the true C++ way: std::string charTime = “TIME”, buf = “SOMETHINGELSE”; bool equal = (charTime == buf); Using == does not work because it tries to compare the addresses of the first … Read more

How can Python compare strings with integers?

Python is not C. Unlike C, Python supports equality testing between arbitrary types. There is no ‘how’ here, strings don’t support equality testing to integers, integers don’t support equality testing to strings. So Python falls back to the default identity test behaviour, but the objects are not the same object, so the result is False. … Read more

Why does `Array(0,1,2) == Array(0,1,2)` not return the expected result?

Scala 2.7 tried to add functionality to Java [] arrays, and ran into corner cases that were problematic. Scala 2.8 has declared that Array[T] is T[], but it provides wrappers and equivalents. Try the following in 2.8 (edit/note: as of RC3, GenericArray is ArraySeq–thanks to retronym for pointing this out): import scala.collection.mutable.{GenericArray=>GArray, WrappedArray=>WArray} scala> GArray(0,1,2) … Read more

How default .equals and .hashCode will work for my classes?

Yes, the default implementation is Object’s (generally speaking; if you inherit from a class that redefined equals and/or hashCode, then you’ll use that implementation instead). From the documentation: equals The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this … Read more