Difference between Vector and ArrayList in java?

Difference between Vector and ArrayList in java?

java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework.
All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized. All the new implementations of java collection framework is not synchronized.
Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.
Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.
Vector or ArrayList? Which is better to use in java?
In general, executing a ‘synchronized’ method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation Vector needs to be used.
Is there an alternate available in java for Vector?
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.
When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.
When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a costlier operation.

What is scoped memory and why java uses it?

In java, a frequent occurence of a common phenomenon related to memory management is creation and destruction of temporary objects. What are java temporary objects? For example in java, when two strings are concatenated, a StringBuffer object is created. As two Java String objects cannot be added directly (immutable property), a helper object StringBuffer is used to construct a resultant java String object. This StringBuffer object is a temporary object, which is of no direct interest to the programmer. Most of the programming constructs are built in this type of model. Result of it is there are lot of garbage left behind.
Now the question is, what happens to the temporary java object and such garbage after the intended operation completes? Will it be saved for the life time of the program, or JVM (java virtual machine) or what is the underlying policy to destroy it? In automatic memory management the hassles of allocating and deallocating memory is no more a part of programmer’s job in java. But the downside of it is you never know, when an object will be freed. The java garbage collector (GC) doesnot provide any defined time boundaries.
This is where the java scoped memory concept comes into picture. Scoped memory concept is declaring to the JVM that, I am going to use this much amount of memory and those can be freed after this period. That is defining a boundary and bringing predictability. This predictability is needed the most for real time programming. Where you need to be certain about the cycles, time and cost of the operation.
In java scoped memory, the memory is preallocated for the task. This will ensure that the program will not get struck in the mid of operation because of memory resource shortage and constraints. Memory is deallocated and freed when the associated task gets completed. Therefore the recycling process of the memory is fast. But, all the objects related to that task and comes under that designated scope will disappear. If some objects are needed for future reference then that particular object needs to be associated to a different memory mechanism.
This is one of the latest feature in java adding power to the real-time java implementation. For more detail look into the Sun Java Real-Time System (Java RTS). It is Sun’s commercial implementation of the Real-Time Specification for Java (JSR-001).

 

0 comments:

Post a Comment