Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between enum and Enumeration

Is there any difference between enum datatype and Enumeration Interface. I have become confused between the two.

I got my answer that they aren't related but that brings me to another question.

We cannot instantiate interface . So what is the significance of this line

Enumeration days = dayNames.elements();

Heres the complete code containing that line

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationTester {

   public static void main(String args[]) {
      Enumeration days;
      Vector dayNames = new Vector();
      dayNames.add("Sunday");
      dayNames.add("Monday");
      dayNames.add("Tuesday");
      dayNames.add("Wednesday");
      dayNames.add("Thursday");
      dayNames.add("Friday");
      dayNames.add("Saturday");
      days = dayNames.elements();
      while (days.hasMoreElements()){
         System.out.println(days.nextElement()); 
      }
   }
}
like image 354
chetan mehta Avatar asked Oct 18 '13 08:10

chetan mehta


People also ask

Is enum and enumeration same in Java?

An enumeration (enum for short) in Java is a special data type which contains a set of predefined constants. You'll usually use an enum when dealing with values that aren't required to change, like days of the week, seasons of the year, colors, and so on.

What is difference between enum and enumeration in Swift?

Enumeration is a data type that allows you to define a list of possible values. An enum allows you to create a data type with those set of values so that they can be recognised consistently throughout your app.

Is enum and enumerator same?

In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type.

What is the difference between enum and enum in C#?

lang. Enum is an abstract class, it is the common base class of all Java enumeration types while enum it's a category of classes that extend the Enum base class.


1 Answers

Enumeration is an interface : An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series.

For example, to print all elements of a Vector<E> v:

for (Enumeration<E> e = v.elements(); e.hasMoreElements();)
       System.out.println(e.nextElement());

enum is a data type: An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

For example, you would specify a days-of-the-week enum type as:

 public enum Day {
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
        THURSDAY, FRIDAY, SATURDAY 
    }

   public static void main(String[] args)
  {
     System.out.ptintln(Day.SUNDAY);  // print SUNDAY
  }

Your Second Question:

We cannot instantiate interface . So what is the significance of this line

Enumeration days = dayNames.elements();

dayNames is a Vector, a collection just like List. (There are differences, but that is beyond the scope of the question.). However, when daynames.elements() is called, it returns an enumeration of the components of vector daynames. The returned Enumeration object will generate all items added to this vector. The first item generated is the item at index 0, then the item at index 1, and so on.

like image 128
Sage Avatar answered Oct 11 '22 14:10

Sage