Hashcode of an int

For the hashCode of an int the most natural choice is to use the int itself. A better question is what to use for the hashCode of a long since it doesn’t fit into the int-sized hashcode. Generally, your best source for that—and all hashCode-related questions—would be Effective Java. Here’s what Effective Java recommends (and … Read more

Casting a double as an int, does it round or just strip digits?

It does not round, it just returns the integral part before the decimal point. Reference (thanks Rawling) Explicit Numeric Conversions Table: When you convert a double or float value to an integral type, this value is rounded towards zero to the nearest integral value. You can try simple issues like this by yourself by writing … Read more

Converting Int to Bool

No, there is and has never been an explicit built in option for conversion of Int to Bool, see the language reference for Bool for details. There exists, still, however, an initializer by NSNumber. The difference is that implicit bridging between Swift numeric type and NSNumber has been removed in Swift 3 (which previously allowed … Read more

Why do these two multiplication operations give different results?

long oneYearWithL = 1000*60*60*24*365L; long oneYearWithoutL = 1000*60*60*24*365; Your first value is actually a long (Since 365L is a long, and 1000*60*60*24 is an integer, so the result of multiplying a long value with an integer value is a long value. But 2nd value is an integer (Since you are mulitplying an integer value with … Read more

What protocol should be adopted by a Type for a generic function to take any number type as an argument in Swift?

Update: The answer below still applies in principle, but Swift 4 completed a redesign of the numeric protocols, such that adding your own is often unnecessary. Take a look at the standard library’s numeric protocols before you build your own system. This actually isn’t possible out of the box in Swift. To do this you’ll … Read more

Is it valid to compare a double with an int in java?

Yes, it’s valid – it will promote the int to a double before performing the comparison. See JLS section 15.20.1 (Numerical Comparison Operators) which links to JLS section 5.6.2 (Binary Numeric Promotion). From the latter: Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules: If either … Read more

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