Search This Blog

Monday, November 28, 2011

What is Enum ?

enum एक प्रकार का डाटाटाइप है जिसके पास एक पूर्व से निर्धारित लिस्ट होती है | यह लिस्ट हम 
enum को देक्लारे करते वक़्त ही उसमे स्टोर करा देते है | कही मायनों में enum boolean डाटाटाइप
से काफी मिलता है क्यों की उसमे भी दो पूर्व निर्धारित values जैसे true और false पहले 
से स्टोर रहती है| लेकिन boolean पहले से प्रोग्रम्मिंग भाषा में होता है जबकि enum नहीं | 
निचे हम एक उदहारण से देखेंगे की इसको प्रोग्राम में कैसे प्रयोग किया जाता है | 
enum main routine के बहार भी declare हो सकता है |
 public class EnumUdaharan  {
 
       // यहाँ पर दो enum types का declaration है  
       // यहाँ हमने इसकी definition को main() routine से बहार रखा है 
  
    enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
      
    enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }
     
    public static void main(String[] args) {
       
         Day tgif;     // Day टाइप का variable declaration .
         Month libra;  // Month टाइप का variable declaration .
       
         tgif = Day.FRIDAY;    // tgif को value डे टाइप की value assign करते हुए 
         virgo = Month.AUG;    // लिब्रा में month टाइप की वलुए स्टोर करते हुए 
           
         System.out.print("Meri rashi Virgo hai, kyu ki mera janm");
         System.out.println(virgo);   // यहाँ output AUG होगा   
         System.out.print("mein hua hai");  
         System.out.print("Yeh saal ka ");
         System.out.print( virgo.ordinal() ); // क्रम संख्या को प्रिंट करना 
         System.out.println("-th mahina hai.");
         System.out.println("   (0 Say count kiya hai!)");
         
         System.out.print("Isn't it nice to get to ");
         System.out.println(tgif);   // Output value will be:  FRIDAY
          
         System.out.println( tgif + " is the " + tgif.ordinal() 
                                            + "-th day of the week.");
              // You can concatenate enum values onto Strings!
         
    }
   
}
 
ऊपर दिए प्रोग्राम का output कुछ इस प्रकार होगा : 
 
Meri rashi Virgo hai, kyu ki mera janm AUG mein hua hai
Yeh saal ka 9-th month of the year.(0 Say count kiya hai!)
Isn't it nice to get to FRIDAY
FRIDAY is the 5-th day of the week. 

No comments:

Post a Comment