typeof in Java 8

You can use the getClass() method to get the type of the object you are using: This will generate the following output: As you see it will show the type of the object which is referenced by the variable, which might not be the same as the type of the variable (Object in this case). For primitive types … Read more

java.lang.ClassCastException

According to the documentation: Thrown to indicate that the code has attempted to cast an Object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

Collision resolution in Java HashMap

When you insert the pair (10, 17) and then (10, 20), there is technically no collision involved. You are just replacing the old value with the new value for a given key 10 (since in both cases, 10 is equal to 10 and also the hash code for 10 is always 10). Collision happens when multiple keys hash to the … Read more

Best way to Format a Double value to 2 Decimal places

No, there is no better way. Actually you have an error in your pattern. What you want is: Note the “00”, meaning exactly two decimal places. If you use “#.##” (# means “optional” digit), it will drop trailing zeroes – ie new DecimalFormat(“#.##”).format(3.0d); prints just “3”, not “3.00”.

“NoClassDefFoundError: Could not initialize class” error

NoClassDefFound error is a nebulous error and is often hiding a more serious issue. It is not the same as ClassNotFoundException (which is thrown when the class is just plain not there). NoClassDefFound may indicate the class is not there, as the javadocs indicate, but it is typically thrown when, after the classloader has loaded the bytes for the … Read more

How do I save a String to a text file using Java?

If you’re simply outputting text, rather than any binary data, the following will work: Then, write your String to it, just like you would to any output stream: You’ll need exception handling, as ever. Be sure to call out.close() when you’ve finished writing. If you are using Java 7 or later, you can use the “try-with-resources statement” … Read more

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