Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128?

There’s a striking difference here. valueOf is returning an Integer object, which may have its values cached between -128 and 127. This is why the first value returns true – it’s cached – and the second value returns false – 128 isn’t a cached value, so you’re getting two separate Integer instances. It is important … Read more

Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

When you compile a number literal in Java and assign it to a Integer (capital I) the compiler emits: Integer b2 =Integer.valueOf(127) This line of code is also generated when you use autoboxing. valueOf is implemented such that certain numbers are “pooled”, and it returns the same instance for values smaller than 128. From the … Read more

How do you compare two version Strings in Java?

Another solution for this old post (for those that it might help) : public class Version implements Comparable<Version> { private String version; public final String get() { return this.version; } public Version(String version) { if(version == null) throw new IllegalArgumentException(“Version can not be null”); if(!version.matches(“[0-9]+(\\.[0-9]+)*”)) throw new IllegalArgumentException(“Invalid version format”); this.version = version; } @Override … Read more

How to efficiently compare two unordered lists (not sets)?

O(n): The Counter() method is best (if your objects are hashable): def compare(s, t): return Counter(s) == Counter(t) O(n log n): The sorted() method is next best (if your objects are orderable): def compare(s, t): return sorted(s) == sorted(t) O(n * n): If the objects are neither hashable, nor orderable, you can use equality: def … Read more

Use ‘=’ or LIKE to compare strings in SQL?

LIKE and the equality operator have different purposes, they don’t do the same thing: = is much faster, whereas LIKE can interpret wildcards. Use = wherever you can and LIKE wherever you must. SELECT * FROM user WHERE login LIKE ‘Test%’; Sample matches: TestUser1 TestUser2 TestU Test

Best way to compare 2 XML documents in Java

Sounds like a job for XMLUnit http://www.xmlunit.org/ https://github.com/xmlunit Example: public class SomeTest extends XMLTestCase { @Test public void test() { String xml1 = … String xml2 = … XMLUnit.setIgnoreWhitespace(true); // ignore whitespace differences // can also compare xml Documents, InputSources, Readers, Diffs assertXMLEqual(xml1, xml2); // assertXMLEquals comes from XMLTestCase } }

How can I compare two floating point numbers in Bash?

More conveniently This can be done more conveniently using Bash’s numeric context: if (( $(echo “$num1 > $num2” |bc -l) )); then … fi Explanation Piping through the basic calculator command bc returns either 1 or 0. The option -l is equivalent to –mathlib; it loads the standard math library. Enclosing the whole expression between … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)