Thursday, April 16, 2009

What to know as a Java Developer?

For a Software profession, every day there are new things to learn. With constant updating of technologies surrounding our technical and domain expertise, we should have a strong base knowledge around with we have to build our technical skills in line with new developments.

For a java developer, we should build our base knowledge around fundamentals of Java language and be strong in java interfaces, generics, annotations, concurrency etc., It's easy to learn quickly new technologies when you master the fundamentals.

Here is a list of things which (out of many things) a java developer must be aware of.

1. How does JVM work?
2. Improvements on language from major version changes (Generics, Annotations, Enums ...).
3. What are the OOPS concepts and how do they Implemented in Java?
4. Understand design pattern
5. Coding Conventions.
6. Build tool (Ant) or Project Management Tool (Maven).
7. At least basic knowledge of some popular frameworks such as Struts, springs, hibernate.
8. Writing test cases using Junit.
9. Good knowledge of Eclipse ( or any popular IDE ).
10. Knowledge of using log4j.

Saturday, April 4, 2009

why not ALWAYS use ArrayLists in Java?

If you need a collection of primitives, then an array may well be the best tool for the job. Boxing is a comparatively expensive operation. For a collection (not including maps) of primitives that will be used as primitives, I almost always use an array to avoid repeated boxing and unboxing.

I rarely worry about the performance difference between an array and an ArrayList, however. If a List will provide better, cleaner, more maintainable code, then I will always use a List (or Collection or Set, etc, as appropriate, but your question was about ArrayList) unless there is some compelling reason not to. Performance is rarely that compelling reason.

Using Collections almost always results in better code, in part because arrays don't play nice with generics, as Johannes Weiß already pointed out in a comment, but also because of so many other reasons:

  • Collections have a very rich API and a large variety of implementations that can (in most cases) be trivially swapped in and out for each other
  • A Collection can be trivially converted to an array, if occasional use of an array version is useful
  • Many Collections grow more gracefully than an array grows, which can be a performance concern
  • Collections work very well with generics, arrays fairly badly
  • As TofuBeer pointed out, array covariance is strange and can act in unexected ways that no object will act in. Collections handle covariance in expected ways.
  • arrays need to be manually sized to their task, and if an array is not full you need to keep track of that yourself. If an array needs to be resized, you have to do that yourself.

All of this together, I rarely use arrays and only a little more often use an ArrayList. However, I do use Lists very often (or just Collection or Set). My most frequent use of arrays is when the item being stored is a primitive and will be inserted and accessed and used as a primitive. If boxing and unboxing every become so fast that it becomes a trivial consideration, I may revisit this decision, but it is more convenient to work with something, to store it, in the form in which it is always referenced. (That is, 'int' instead of 'Integer'.)

Array or List in Java. Which is faster ?

The Java way is that you should consider what data abstraction most suits your needs. Remember that in Java a List is an abstract, not a concrete data type. You should declare the strings as a List, and then initialize it using the ArrayList implementation.

List<String> strings = new ArrayList<String>();

This separation of Abstract Data Type and specific implementation is one the key aspects of object oriented programming.

An ArrayList implements the List Abstract Data Type using an array as its underlying implementation. Access speed is virtually identical to an array, with the additional advantages of being able to add and subtract elements to a List (although this is an O(n) operation with an ArrayList) and that if you decide to change the underlying implementation later on you can. For example, if you realize you need synchronized access, you can change the implementation to a Vector without rewriting all your code.

In fact, the ArrayList was specifically designed to replace the low-level array construct in most contexts. If Java was being designed today, it's entirely possible that arrays would have been left out altogether in favor of the ArrayList construct.

Since arrays keep all the data in a contiguous chunk of memory (unlike Lists), would the use of an array to store thousands of strings cause problems ?

In Java, all collections store only references to objects, not the objects themselves. Both arrays and ArrayList will store a few thousand references in a contiguous array, so they are essentially identical. You can consider that a contiguous block of a few thousand 32-bit references will always be readily available on modern hardware. This does not guarantee that you will not run out of memory altogether, of course, just that the contiguous block of memory requirement is not difficult to fufil.