A data type that contains a fixed set of constants
Enums
Good to use when you already know all possibilities of the values or instances of the class
Can be declared on their own or within another class
To create an enum
1. Declare a type with the enum keyword
2. Declare an identifier for the type
3. Declare a list of values called enum constants
Enum methods
toString() - Returns the name of the calling constant object
ordinal() - Returns an integer that represents the constant's position in the list of constants; the first position is 0
equals() - Returns true if its argument is equal to the calling object's value
compareTo() - Returns a negative integer if the calling object's ordinal value is less than that of the argument, 0 if they are the same, and a positive integer if the calling object's ordinal value is greater than that of the argument
Enum static methods
valueOf() - Accepts a string parameter and returns an enumeration constant
values() - Returns an array of the enumerated constants
Type-safe
Used to describe a data type that allows only appropriate behaviors
Types of nested classes
Static member class - has access to all static methods of the top-level class
Non-static member class (inner class) - requires an instance; it has access to all data and methods of the top-level class
Local class - defined within a method body
Anonymous class - a special case of a local class that has no identifier
The most common reason for nesting a class inside another is because the inner class is used only by the top-level class
Regular expression (regex)
A character or a sequence of characters that represent a string or multiple strings
Regular expressions are part of the java.util.regex package
Regular expressions
Allow for quicker, easier searching, parsing, and replacing of characters in a string
Are used to ensure that strings contain specific contents
String.matches(String regex)
Returns true if a string matches the given regular expression
equals() method
Compares one (1) string to another by comparing a single argument
matches() method
Allows you to determine what the string should match against and allows you to specify multiple values, providing much more flexibility in your code
Pipe (|) symbol
Allows the matches() method to check if a string is equal to "cat" or "dog" and return true accordingly
Square brackets []
Used in regular expressions to allow for character variability
Square brackets are not restricted to two-character options, they can be combined with a hyphen to include any range of characters
Dot (.)
The wildcard operator that represents any single character in regular expressions
Repetition operators
* - 0 or more occurrences
? - 0 or 1 occurrence
+ - 1 or more occurrences
{x} - x occurrences
{x,y} - Between x & y occurrences
{x,} - x or more occurrences
Regular expressions allow for very powerful validation of strings without having to write much code
The dot (.) represents any character, while the asterisk (*) represents any number of occurrences of the character preceding it. Hence, ".*" means any number of any characters in a sequence will return true
Pattern class
Stores the format of a regular expression
Pattern.compile()
Compiles the given regular expression into a pattern. A compiled regex pattern can speed up your program when the pattern is used frequently
Matcher class
Stores a possible match between a pattern and a string
Matcher.matches()
Checks if the regular expression given in the Pattern declaration matches a given string
find()method
Scans the input sequence looking for the next subsequence that matches the pattern
split()method
Splits the string around matches of the given regular expression
replaceAll() method
Replaces all the occurrences of the defined regular expression found in the string with another string