Skip to content

java

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) {
            connection.close();
        }
    } 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 →