Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 15 ■ GENERICS AND REGULAR EXPRESSIONS


Summary


This chapter covered the things that benefit from pattern matching: generics and regular expressions.
About generics, we learned that:


  • We can specify the kind of content that goes into a collection.

  • Thanks to an improvement introduced in Java 7, we can use the diamond specifier
    (<>) to shorten our code a bit, so long as the compiler can determine the type from
    earlier in the line.

  • Generics can have multiple parameters.

  • We can nest generic parameters to ensure we get the proper kinds of objects at
    any depth.

  • We can use wildcards within generic parameters, to accommodate similar objects
    (any object that extends a particular class or implements a particular set of
    interfaces or both).

  • Generics let us catch problems at coding time rather than at run time, saving time
    and embarrassment.


About regular expressions, we learned:


  • How to instantiate the member classes (Matcher and Pattern) of the
    java.util.regex package.

  • What each of the metacharacters does.

  • How to combine the metacharacters in a number of useful ways.

  • How to make a pattern be reluctant (match the fewest possible characters) rather
    than greedy (match the most possible characters).

  • That regular expressions can become very complex and a bit about how to
    manage that complexity.


This chapter covered two language features that I hope you will find useful as you develop your own
programs. I especially hope that you'll use generics any time you use a collection, as you should embrace
best practices whenever you can. As for regular expressions, remember that they are supposed to make
things simpler. If you find that a regular expression is too hard to figure out, break it up with other String
operations and use multiple regular expressions rather than one big one.
Free download pdf