Java Enum
Java Enum
Versions prior to JDK 5 lacked one feature that many programmers felt
was needed: enumerations.
In its simplest form, an enumeration is a list of named constants.
Although Java offered other features that provide somewhat similar
functionality, such as final.
In their simplest form, Java enumerations appear similar to
enumerations in other languages.
This keyword can be used similar to the static final constants.
Syntax:
enum enum_name
{ Data values }
Example: enum Days
{ Sunday, Monday, Tuesday, Wednesday, Thrusday, Friday,
Saturday
}
The identifiers Sunday, Monday, and so on, are called enumeration constants.
Each is implicitly declared as a public, static final member of Days.
Once you have defined an enumeration, you can create a variable of that type.
However, even though enumerations define a class type, you do not
instantiate an enum using new.
Instead, you declare and use an enumeration variable in much the same way as
you do one of the primitive types.
For example, this declares dy as a variable of enumeration type Days.
Days dy;
Because dy is of type Days, the only values that it can be assigned (or can
contain) are those defined by the enumeration.
For example, this assigns dy the value Sunday:
dy = Days.Sunday
The values( ) and valueOf( ) Methods:
All enumerations automatically contain two predefined methods:
values( ) and valueOf( ).
Their general forms are shown here:
public static enum-type[ ] values( )
public static enum-type valueOf(String str)
The values( ) method returns an array that contains a list of the
enumeration constants.
The valueOf( ) method returns the enumeration constant whose value
corresponds to the string passed in str.
• Two enumeration constants can be compared for equality by using the
= = relational operator.
• For example, this statement compares the value in dy with the
Saturday constant:
• if(dy == Days.Saturday)
• An enumeration value can also be used to control a switch statement.
• Of course, all of the case statements must use constants from the same
enum as that used by the switch expression.
• For example, this switch is perfectly valid:
// Use an enum to control a switch statement.
switch(dy) {
case Sunday:
// ...
case Monday:
// ...
String Handling:
• In Java a string is a sequence of characters.
• But, unlike many other languages that implement strings as
character arrays,
• Java implements strings as objects of type String.
• Java has methods to compare two strings, search for a substring,
concatenate two strings, and change the case of letters within a
string.
• when you create a String object, that cannot be changed.
• That is, once a String object has been created, you cannot
change the characters that comprise that string.
• At first, this may seem to be a serious restriction. So strings are
called immutable strings.
• Java provides two options: StringBuffer and StringBuilder.
• Both hold strings that can be modified after they are created.
• The String, StringBuffer, and StringBuilder classes are defined in
java.lang.
The String Constructors:
• The String class supports several constructors.
• To create an empty String, you call the default constructor.
• Eg: String s = new String();
• will create an instance of String with no characters in it.
• The String class provides a variety of constructors to handle this.
• To create a String initialized by an array of characters, use the
constructor shown here:
String(char chars[ ])
• Eg: char chars[ ] = { 'a', 'b', 'c' };
String s = new String(chars);
• This constructor initializes s with the string “abc”
• You can specify a sub range of a character array as an initializer using
the following constructor:
String(char chars[ ], int startIndex, int numChars)
• Here, startIndex specifies the index at which the sub range begins,
and numChars specifies the number of characters to use.
• Here is an example:
char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String s = new String(chars, 2, 3);
• This initializes s with the characters “cde”.
• You can construct a String object that contains the same character
sequence as another String object using this constructor:
String(String strObj)
• Here, strObj is a String object.
char ch [ ] = {‘E’,’C’,’E’};
String s1 = new String(ch);
String s2 = new String(s1);
String Methods:
concat() substring()
length() toLowerCase()
charAt() toUpperCase()
compareTo() trim()
compareToIgnoreCase() split()
equals() getChars()
equalsIgnoreCase()
startsWith()
endsWith()
indexOf()
lastIndexOf()
replace()
concat():
This methods is used to concatenation of two strings.
Syntax:
String concat(String str); // str is string object