What is the difference between a stack and a queue?

Queue Stack First item to be inserted is the first one to be removed. Allows access to only last item inserted. This mechanism is called First In First Out (FIFO). An item is inserted or removed from one end called the “top” of the stack. This is called Last In First Out (LIFO) mechanism. Placing an item in the queue is called “enqueue or insertion” and removing an item from a queue is called “dequeue or deletion”. Pre J2SE 5.0, you should write your own Queue class with enqueue() and dequeue() methods using an ArrayList or a LinkedList class. J2SE … Click here to continue…..

What is the main difference between an ArrayList and a Vector? What is the main difference between HashMap and Hashtable?

Vector / Hashtable ArrayList / HashMap Original classes before the introduction of Collections API. Vector & Hashtable are synchronized. Any method that touches their contents is thread-safe. So if you don’t need a thread safe collection, use the ArrayList or HashMap. Why pay the price of synchronization unnecessarily at the expense of performance degradation. As a general rule, prefer ArrayList/HashMap to Vector/Hashtable. If your application is a multithreaded application and at least one of the threads either adds or deletes an entry into the collection then use new Java collections API‘s external synchronization facility as shown below to temporarily synchronize your collections as … Click here to continue…..

When to use an interface?

For polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently, you should prefer using interface to abstract. Coding to an interface reduces coupling and interface inheritance can achieve code reuse with the help of object composition. For example: The Spring framework’s dependency injection promotes code to an interface principle. Another justification for using interfaces is that they solve the ‘diamond problem’ of traditional multiple inheritance as shown in the figure. Java does not support multiple inheritance. Java only … Click here to continue…..

Concept of Refactoring

If you read the XP life cycle, one of the things after we implement the task is code refactoring. Refactoring is the process of changing the software in such a way that it does not change the behavior of the code but on the other hand improves the internal structure. No code can be perfect with architecture, coding conventions, and high reusability at the first level itself. Normally it improves over time. It helps developers to implement the necessary solutions today and improve / make the code better tomorrow. Refactoring is the process of changing the system to improve the … Click here to continue…..