Top 10 questions about Java Collections

The following are the most popular questions of Java collections asked and discussed on Stackoverflow. Before you look at those questions, it’s a good idea to see the class hierarchy diagram. 1. When to use LinkedList over ArrayList? ArrayList is essentially an array. Its elements can be accessed directly by index. But if the array … Read more

Top 10 Methods for Java Arrays

The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow. 0. Declare an array String[] aArray = new String[5]; String[] bArray = {"a","b","c", "d", "e"}; String[] cArray = new String[]{"a","b","c","d","e"};String[] aArray = new String[5]; String[] bArray = {"a","b","c", "d", "e"}; String[] cArray = new String[]{"a","b","c","d","e"}; 1. Print an … Read more

Why String is immutable in Java?

String is immutable in Java. An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the information can not be modified. There are many advantages of immutable classes. This article summarizes why String is designed to be immutable. This post illustrate the immutability concept in the perspective of memory, synchronization and data structures.

Read more

Java double Example

Have you ever met the situation that you get an integer but you really want a double? For the following method, devide(2,3) will return 0.0. public static double devide(int x, int y){ return x/y; }public static double devide(int x, int y){ return x/y; } The problem is that x/y does int division. If you want … Read more

Loop an Iterable in Java

If you use some Java API and it returns an Iterable, such as Iterable, you may want to loop this iterable. The method is pretty simple as follows: Iterable<String> result = … //result returned from some method. for(String s: result){ System.out.println(s); }Iterable<String> result = … //result returned from some method. for(String s: result){ System.out.println(s); } … Read more

java io class hierarchy diagram

The large number of classes in the Java IO package is overwhelming and annoying. However, if we use Java, we still need to understand those classes. In fact, the classes in Java IO package is not very complex, but we need a good way to learn those. There are two important factors for understanding the … Read more

Swing vs. SWT – Side-by-side comparison

In many articles, developers have compared Swing with SWT with a lot of details. However, there is not a side-by-side comparison by using a diagram which may give a direct impression of how they are different. In this post, I will compare Swing and SWT side by side and get a taste of the difference … Read more

Java Access Level for Members: public, protected, private

Java access level contains two parts: 1) access level for classes and 2) access level for members. For class access level, the keyword can be public or no explicit modifier(package-private). For member access level, the keyword can be public, protected, package-private (no explicit modifier), or private. The following table summarizes the access level of different … Read more