Skip to content


What data can be stored in a collection?

You can store anything but primitives. To store primitives consider using wrappers (Integer for int, Double for double, etc…).
For example, you can store your own classes:

List<MyClass> myClassObjects = new LinkedList<MyClass>();

To store primitives:

Set<Integer> ints = new TreeSet<Integer>();

Notice, that you can add primitives, since it will be wrapped automatically:

ints.add(1); // Correct

What benefits does JCF provide?

JCF provides the following benefits:

  • Reduces programming effort: you don’t need to invent the wheel anymore, JCF provides useful data structures and algorithms.
  • Increases program speed and quality: JCF provides high-performance, high-quality implementations of useful data structures and algorithms. Programs can be easily tuned by switching collection implementation. It is possible because interface implementations are interchangeable. When you use the Collections Framework you don’t need to waste time by writing your own data structures, so you’ll have more time to write and improve your business logic.
  • Allows interoperability among unrelated APIs: you can use collections in different applications and be sure that it will work fine. You can exchange your collections between applications seamlessly.
  • Reduces effort to learn and to use new APIs: many APIs take collections on input and return them as output. In the past, each API could implement its own collections and you needed to learn it. Now we have a universal solution – JCF.
  • Reduces effort to design new APIs: this is the reverse side of the previous benefit. A programmer don’t have to invent and to implement his own collections.
  • Fosters software reuse: if you implement your own collection with a JCF interface, you will get a reusable code. You can use collections utilities on your implementation as well.

Whas is a collection?

Collection is an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data.
Live example: several Employee objects can be grouped into a collection:

List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(“John”));
employees.add(new Employee(“Mary”));

Java has a Java Collection Framework (JCF), which consists of:

Read More →

Java exceptions handling order

Consider there is a method which can throw IOException and FileNotFoundException. Is there a difference in which order we catch these exceptions in a single try-catch block?
Will this code work correctly?

try {
    // potential FileNotFoundException or IOException
} catch (IOException e) {
    // handling IOException
} catch (FileNotFoundException e) {
    // handling FileNotFoundException

Read More →

Can I use try-finally block (without catch)?

Yes, you can use the try-finally construction. As a use case, we can try to connect to a database and finally we will close the connection:

Connection connection = null;

try {
    connection = Database.getConnection();
    // some operations
} finally {
    try {
        if (connection != null) {
    } catch(SomeException e) {
        // what to do

This code looks a bit redundant. But in Java 7 we can use a try-with-resources block to handle our resources closing in a more convinient way…

Read More →