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) {
            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 →

JavaScript scope mini-task

What we will see in the console?

var obj = {
    heroName: "Gandalf",
    func: function() {
        var that = this;
        console.log("outer func:  this.heroName = " + this.heroName);
        console.log("outer func:  that.heroName = " + that.heroName);
        (function() {
            console.log("inner func:  this.heroName = " + this.heroName);
            console.log("inner func:  that.heroName = " + that.heroName);
        }());
    }
};
obj.func();

 

The result will be:

Read More →

The wind is rising… We must try to live!

The Wind Rises

I haven’t written anything for a long time. There were nothing special for last two months in my life. I continue studying Java on workdays and go home on weekends. Actually, this autumn I hope I will find time to describe GOF patterns in Java on this site.

Last weekend we watched a film by Japanese animator Hayao Miyazaki called The Wind Rises. I love almost all his films (almost — because I haven’t watched all of them, I think 😌). Each frame of these films is a masterpiece.

Read More →

Today I met the ghost rider 😌 Where is Nicolas Cage? 😁

Ghost rider aerography