What is the difference between JDK and JRE?

The JRE is the Java Runtime Environment. It is a package of everything necessary to run a compiled Java program, including the Java Virtual Machine (JVM), the Java Class Library, the java command, and other infrastructure. However, it cannot be used to create new programs. The JDK is the Java Development Kit, the full-featured SDK … Read more

Difference between HashMap, LinkedHashMap and TreeMap

I prefer visual presentation: Property HashMap TreeMap LinkedHashMap Iteration Order no guaranteed order, will remain constant over time sorted according to the natural ordering insertion-order Get / put / remove / containsKey O(1) O(log(n)) O(1) Interfaces Map NavigableMap, Map, SortedMap Map Null values/keys allowed only values allowed Fail-fast behavior Fail-fast behavior of an iterator cannot … Read more

StringBuilder vs String concatenation in toString() in Java

Version 1 is preferable because it is shorter and the compiler will in fact turn it into version 2 – no performance difference whatsoever. More importantly given we have only 3 properties it might not make a difference, but at what point do you switch from concat to builder? At the point where you’re concatenating … Read more

How can I generate an MD5 hash in Java?

The MessageDigest class can provide you with an instance of the MD5 digest. When working with strings and the crypto classes be sure to always specify the encoding you want the byte representation in. If you just use string.getBytes() it will use the platform default. (Not all platforms use the same defaults) import java.security.*; .. … Read more

:: (double colon) operator in Java 8

Usually, one would call the reduce method using Math.max(int, int) as follows: reduce(new IntBinaryOperator() { int applyAsInt(int left, int right) { return Math.max(left, right); } }); That requires a lot of syntax for just calling Math.max. That’s where lambda expressions come into play. Since Java 8 it is allowed to do the same thing in … Read more

Static Classes In Java

Java has static nested classes but it sounds like you’re looking for a top-level static class. Java has no way of making a top-level class static but you can simulate a static class like this: Declare your class final – Prevents extension of the class since extending a static class makes no sense Make the … Read more

Download a file with Android, and showing the progress in a ProgressDialog

There are many ways to download files. Following I will post most common ways; it is up to you to decide which method is better for your app. Use AsyncTask and show the download progress in a dialog ============================================================= This method will allow you to execute some background processes and update the UI at the … Read more

What is the difference between canonical name, simple name and class name in Java Class?

If you’re unsure about something, try writing a test first. I did this: class ClassNameTest { public static void main(final String… arguments) { printNamesForClass( int.class, “int.class (primitive)”); printNamesForClass( String.class, “String.class (ordinary class)”); printNamesForClass( java.util.HashMap.SimpleEntry.class, “java.util.HashMap.SimpleEntry.class (nested class)”); printNamesForClass( new java.io.Serializable(){}.getClass(), “new java.io.Serializable(){}.getClass() (anonymous inner class)”); } private static void printNamesForClass(final Class<?> clazz, final String label) … Read more