Study like there’s no tomorrow because if you keep putting off your studies for tomorrow, you’ll probably be too late.
--Your friends at LectureNotes

Note for Advanced Java and J2EE - j2ee By vtu rangers

  • Advanced Java and J2EE - j2ee
  • Note
  • Jawaharlal Nehru Technological University Anantapur (JNTU) College of Engineering (CEP), Pulivendula, Pulivendula, Andhra Pradesh, India - JNTUACEP
  • Computer Science Engineering
  • 14 Topics
  • 274 Offline Downloads
  • Uploaded 1 year ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-3

Advanced Java and J2EE SL No Table of Contents Module Description 15CS553 Page No 1 Module 1 – Enumeration, AutoBoxing and Annotations 1-23 2 Module 2 – Collection Framework 24-57 3 Module 3 – String Methods 58-119 4 Module 4 – Servlets and JSP 120-149 5 Module 5 – JDBC Methods 150-166 Dept. of CSE, SJBIT Page iii

Text from page-4

Advanced Java and J2EE Module – 1 Enumerations, Autoboxing and Annotations 15CS553 Enumerations Enumerations was added to Java language in JDK5. Enumeration means a list of named constant. In Java, enumeration defines a class type. An Enumeration can have constructors, methods and instance variables. It is created using enum keyword. Each enumeration constant is public, static and final by default. Even though enumeration defines a class type and have constructors, you do not instantiate an enum using new. Enumeration variables are used and declared in much a same way as you do a primitive variable. How to Define and Use an Enumeration 1. An enumeration can be defined simply by creating a list of enum variable. Let us take an example for list of Subject variable, with different subjects in the list. enum Subject //Enumeration defined { Java, Cpp, C, Dbms } 2. Identifiers Java, Cpp, C and Dbms are called enumeration constants. These are public, static and final by default. 3. Variables of Enumeration can be defined directly without any new keyword. Subject sub; 4. Variables of Enumeration type can have only enumeration constants as value. We define an enum variable as enum_variable = enum_type.enum_constant; sub = Subject.Java; 5. Two enumeration constants can be compared for equality by using the = = relational operator. Example: if(sub == Subject.Java) { ... } Dept. of CSE, SJBIT Page 1

Text from page-5

Advanced Java and J2EE Example of Enumeration 15CS553 enum WeekDays { sun, mon, tues, wed, thurs, fri, sat } class Test { public static void main(String args[]) { WeekDays wk; //wk is an enumeration variable of type WeekDays wk = WeekDays.sun; //wk can be assigned only the constants defined under //enum type Weekdays System.out.println("Today is "+wk); } } Output : Today is sun The enum can be defined within or outside the class because it is similar to a class. enum Season { WINTER, SPRING, SUMMER, FALL } class EnumExample2{ public static void main(String[] args) { Season s=Season.WINTER; System.out.println(s); }} Output:WINTER class EnumExample3{ enum Season { WINTER, SPRING, SUMMER, FALL;}//semicolon; is optional here public static void main(String[] args) { Season s=Season.WINTER;//enum type is required to access WINTER System.out.println(s); }} Output:WINTER Example of Enumeration using switch statement class EnumExample5{ enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} public static void main(String args[]){ Day day=Day.MONDAY; Dept. of CSE, SJBIT Page 2

Text from page-6

Advanced Java and J2EE switch(day){ case SUNDAY: System.out.println("sunday"); break; case MONDAY: System.out.println("monday"); break; default: System.out.println("other day"); } }} Output:monday 15CS553 Values( ) and ValueOf( ) method All the enumerations has predefined methods values() and valueOf(). values() method returns an array of enum-type containing all the enumeration constants in it. Its general form is, public static enum-type[ ] values() valueOf() method is used to return the enumeration constant whose value is equal to the string passed in as argument while calling this method. It's general form is, public static enum-type valueOf (String str) Example of enumeration using values() and valueOf() methods: enum Restaurants { dominos, kfc, pizzahut, paninos, burgerking } class Test { public static void main(String args[]) { Restaurants r; System.out.println("All constants of enum type Restaurants are:"); Restaurants rArray[] = Restaurants.values(); //returns an array of constants of type Restaurants for(Restaurants a : rArray) //using foreach loop System.out.println(a); r = Restaurants.valueOf("dominos"); System.out.println("I AM " + r); } } Output: All constants of enum type Restaurants are: dominos Dept. of CSE, SJBIT Page 3

Lecture Notes