Posts

Differences Between Enumeration And Iterator In Java

1) Introduction Iterator  interface is introduced from JDK 1.2 where as  Enumeration  interface is there from JDK 1.0. 2) remove() method This is the main difference between  Enumeration  and  Iterator  interface.  Enumeration  only traverses the  Collection  object. You can’t do any modifications to  Collection  while traversing the  Collection  using  Enumeration . Where as  Iterator  interface allows us to remove an element while traversing the  Collection  object.  Iterator  has  remove()  method which is not there in the  Enumeration  interface. Below is the list of  Enumeration  and  Iterator methods. Iterator Enumeration hasNext() hasMoreElements() next() nextElement() remove() (Not Available) 3) Legacy Interface Enumeration  is a legacy interface used to traverse only the legacy classes like  Vect...

Difference between Runnable vs Callable interface

1) Existence and Availability First and most important difference between Runnable and Callable interface is that Runnable is available in Java right from the beginning i.e. JDK 1.0 while Callable was later added to Java 5, so you cannot use Callable before Java 5. 2) Result The second key difference between Callable and Runnable is that Callable can return the result of parallel  processing  of a task. It returns a  Future object , which represents the  lifecycle  of a task and provides methods to check if the task has been completed or canceled, retrieve the result or cancel the task. Since return type of  Runnable' s  run()  method  is void, it cannot return anything. 3) Checked Exception Another worth noting difference between these two interfaces is that Callable's  call()  method can throw  Checked exception  while Runnable's  run()  method cannot throw checked exception. 4) The call() vs run() methods I...

Difference between Comparable and Comparator

Comparable Comparator 1) Comparable provides a  single sorting sequence . In other words, we can sort the collection on the basis of a single element such as id, name, and price. The Comparator provides  multiple sorting sequences . In other words, we can sort the collection on the basis of multiple elements such as id, name, and price etc. 2) Comparable  affects the original class , i.e., the actual class is modified. Comparator  doesn't affect the original class , i.e., the actual class is not modified. 3) Comparable provides  compareTo() method  to sort elements. Comparator provides  compare() method  to sort elements. 4) Comparable is present in  java.lang  package. A Comparator is present in the  java.util  package. 5) We can sort the list elements of Comparable type by  Collections.sort(List)  method. We can sort the list elements of Comparator type by  Collections.sort(List, Comparator)  method...

Can we Overload or Override static methods in java ?

Let us first define Overloading and Overriding. Overriding  : Overriding is a feature of OOP languages like Java that is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in superclass (or base class). The implementation to be executed is decided at run-time and decision is made according to the object used for call. Note that signatures of both methods must be same. Refer  Overriding in Java  for details. Overloading : Overloading is also a feature of OOP languages like Java that is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input paramaters. Note that in both C++ and Java,  methods cannot be overloaded according to return type. Can we overload static methods? The answer is ‘Yes’. We can have two ore more static methods with same name, but differences in input...