Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.


Published on

Published in: Technology, Business
  • Be the first to comment

  1. 1. Professional Java® JDK® 6 EditionW. Clay Richardson, Donald Avondolio, Scot Schrager, Mark W. Mitchell, and Jeff Scanlon
  2. 2. Professional Java, JDK® 6 Edition ®Published byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.comCopyright © 2007 by Wiley Publishing, Inc., Indianapolis, IndianaPublished simultaneously in CanadaISBN-13: 978-0-471-77710-6ISBN-10: 0-471-77710-2Manufactured in the United States of America10 9 8 7 6 5 4 3 2 11O/RU/RS/QW/INNo part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by anymeans, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, orauthorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 RosewoodDrive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should beaddressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317)572-3447, fax (317) 572-4355, or online at OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP-RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON-TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUTLIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATEDOR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINEDHEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTAND-ING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PRO-FESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENTPROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BELIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE ISREFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMA-TION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THEORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READ-ERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED ORDISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.For general information on our other products and services or to obtain technical support, please contact our Cus-tomer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002.Library of Congress Cataloging-in-Publication DataProfessional Java JDK, 6 Edition / W. Clay Richardson ... [et al.]. p. cm. Includes index. ISBN-13: 978-0-471-77710-6 (paper/website) ISBN-10: 0-471-77710-2 (paper/website)1. Java (Computer program language) I. Richardson, W. Clay, 1976- QA76.73.J38P7623 2007 005.13’3—dc22 2006032740Trademarks: Wiley and related trade dress are registered trademarks of Wiley Publishing, Inc., in the United Statesand other countries, and may not be used without written permission. All other trademarks are the property of theirrespective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not beavailable in electronic books.
  3. 3. This book is dedicated to all those who make the daily sacrifices,especially those who have made the ultimate sacrifice, to ensure our freedom and security.
  4. 4. About the Authors W. Clay Richardson is a software consultant concentrating on agile Java solutions for highly specialized business processes. He has fielded many Java solutions, serving in roles including senior architect, development lead, and program manager. He is a co-author of More Java Pitfalls and Professional Portal Development with Open Source Tools (Wiley), and Professional Java, JDK 5 Edition. As an adjunct professor of computer science for Virginia Tech, Richardson teaches graduate-level coursework in object-oriented development with Java. He holds degrees from Virginia Tech and the Virginia Military Institute. Donald Avondolio currently serves in a lead position as an architect/developer on an enterprise devel- opment project. In his spare time, Donald loves fly-fishing, watching baseball and lacrosse, running triathlons (not very well), and sitting around his house complaining about things. Scot Schrager has consulted extensively in the domains of pharmaceuticals, supply chain management, and the national security market. He has led and participated in various project teams using Java and Object Oriented Analysis & Design techniques. Most recently, Schrager has been focused on distributed application architecture using J2EE technology. Mark W. Mitchell has extensive experience in enterprise application integration, particularly Web Services integration between Java and the Microsoft platform. He has developed and deployed several mission-critical web applications. Mitchell holds a degree in computer science from the University of Virginia. Jeff Scanlon is a software development consultant from Virginia. He holds both the Sun Certified Java Developer and Microsoft Certified Solutions Developer certifications, and has been published in Software Development magazine.
  5. 5. CreditsAcquisitions Editor Vice President and Executive PublisherRobert Elliott Joseph B. WikertDevelopment Editor Project CoordinatorBrian Herrmann Kristie ReesTechnical Editor Graphics and Production SpecialistsDavid Parks Carrie A. Foster Brooke GracyzkProduction Editor Denny HagerKathryn Duggan Joyce Haughey Barbara MooreCopy Editor Barry OffringaKim Cofer Alicia B. South Ronald TerryEditorial ManagerMary Beth Wakefield Quality Control Technicians John GreenoughProduction Manager Charles SpencerTim Tate Brian H. WallsVice President and Executive Group Publisher Proofreading and IndexingRichard Swadley Techbooks and Stephen Ingle Anniversary Logo Design Richard Pacifico
  6. 6. Contents Acknowledgments xv Introduction xviiPart I: Thinking Like a Java Developer 1Chapter 1: Key Java Language Features and Libraries 3 Introducing Derby 3 Using Derby 4 Language Features Added in Java 5 7 Generics 7 Enhanced for Loop 16 Additions to the Java Class Library 17 Variable Arguments 18 Boxing and Unboxing Conversions 19 Static Imports 21 Enumerations 24 Metadata 26 Important Java Utility Libraries 34 Java Logging 35 Java Preferences 70 Summary 77Chapter 2: Tools and Techniques for Developing Java Solutions 79 Principles of Quality Software Development 80 Habits of Effective Software Development 81 Communicate 81 Model 81 Be Agile 81 Be Disciplined 82 Trace Your Actions to Need 82 Don’t Be Afraid to Write Code 83 Think of Code as a Design, not a Product 83
  7. 7. Contents Read a Lot 84 Build Your Process from the Ground Up 84 Manage Your Configuration 84 Unit Test Your Code 85 Continuously Integrate 85 Maintaining Short Iterations 86 Measure What You Accomplished — Indirectly 87 Track Your Issues 87 Development Methodology 88 Waterfall Methodology 88 Unified Process 90 Extreme Programming 91 Observations on Methodology 92 Practical Development Scenarios 93 Ant 93 Maven 2 101 TestNG 106 XDoclet 110 JMeter 117 Summary 120Chapter 3: Exploiting Patterns in Java 123 Why Patterns Are Important 124 Keys to Understanding the Java Programming Language 124 Keys to Understanding Tools Used in Java Development 125 Keys to Developing Effective Java Solutions 126 Building Patterns with Design Principles 127 Designing a Single Class 127 Creating an Association between Classes 128 Creating an Interface 129 Creating an Inheritance Loop 129 Important Java Patterns 131 Adapter 131 Model-View-Controller 134 Command 142 Strategy 146 Composite 150 Summary 154viii
  8. 8. ContentsPart II: A Broad Understanding of Java APIs,Tools, and Techniques 155Chapter 4: Developing Effective User Interfaces with JFC 157 Layout Managers 158 BorderLayout 158 BoxLayout 164 FlowLayout 173 GridLayout 177 GridBagLayout 189 SpringLayout 194 CardLayout 202 GroupLayout 208 Mustang Release Desktop Enhancements 214 Managing Navigation Flows in Swing Applications 225 Summary 235Chapter 5: Persisting Your Application Using Files 237 Application Data 237 Saving Application Data 239 Sample Configuration Data Model for an Application 239 Java Serialization: Persisting Object Graphs 241 Key Classes 242 Serializing Your Objects 243 Extending and Customizing Serialization 257 When to Use Java Serialization 261 JavaBeans Long-Term Serialization: XMLEncoder/Decoder 262 Design Differences 262 Serializing Your JavaBeans 265 When to Use XMLEncoder/Decoder 269 Flexible XML Serialization: Java API for XML Binding (JAXB) 270 Sample XML Document for the Configuration Object 271 Defining Your XML Format with an XML Schema 273 JAXB API Key Classes 280 Marshalling and Unmarshalling XML Data 281 Creating New XML Content with JAXB-Generated Classes 283 Using JAXB-Generated Classes in Your Application 283 ix
  9. 9. Contents Annotating Existing Java Classes for Use with JAXB 290 When to Use JAXB 307 Where JAXB Fits in the JDK 308 Summary 308Chapter 6: Persisting Your Application Using Databases 311 JDBC API Overview 312 Setting Up Your Environment 313 JDBC API Usage in the Real World 313 Understanding the Two-Tier Model 313 Understanding the Three-Tier Model 314 Effectively Using JDBC 4.0 315 Overview 315 Managing Connections 316 Understanding Statements 318 Utilizing Result Sets 328 Advanced Concepts 333 Hibernate 335 Hibernate Components 336 Hibernate Example 341 Summary 354Chapter 7: Developing Web Applications Using the Model 1 Architecture 355 What Is Model 1? Why Use It? 356 JSP 2.0 Overview 357 Integrated Expression Language (EL) 365 JSTL 1.1 Overview 366 Developing Your Web Application Visualizations with JSTL 1.1 370 Developing Your Web Application Visualizations with JSP 2.0 376 AJAX 381 Summary 391Chapter 8: Developing Web Applications Using the Model 2 Architecture 393 The Problem 393 What Is Model 2? 393 Why Use Model 2? 395 Developing an Application with WebWork 396 What Is Inversion of Control and Why Is it Useful? 397 Architecture 399x
  10. 10. Contents Extending the Framework to Support Hibernate 402 Preventing the Hanging Session 403 Defining Your Domain Model 405 Implementing Your Use Cases with Actions 412 Developing Your Views 415 Configuring Your Application 422 Adapting to Changes 424 Summary 426Chapter 9: Interacting with C/C++ Using Java Native Interface 427 A First Look at Java Native Interface 427 Creating the Java Code 428 Creating the Native Code and Library 429 Executing the Code 431 Java Native Interface 432 Data Types 432 Strings in JNI 432 Arrays in JNI 436 Working with Java Objects in C/C++ 442 Handling Java Exceptions in Native Code 449 Working with Object References in Native Code 451 Advanced Programming Using JNI 455 Developing an Email Client 460 System Design 460 User Interface 461 Summary 471Chapter 10: EJB 3 and the Java Persistence API 473 New Features 474 Java Persistence API (JPA) 475 Entities 475 Query Language 475 EntityManager 476 What Are Session Beans? The Demise of Entity Beans? 480 Interceptors 481 EJB 3 and Java Persistence API Web Component Examples 485 Summary 517 xi
  11. 11. ContentsChapter 11: Communicating between Java Components andComponents of Other Platforms 519 Component Communication Scenarios 520 News Reader: Automated Web Browsing 520 A Bank Application: An EJB/Java EE Client 520 A Portal: Integrating Heterogeneous Data Sources and Services 521 Overview of Interprocess Communication and Basic Network Architecture 521 Sockets 522 The Java Socket API 523 Implementing a Protocol 530 Remote Method Invocation 542 Core RPC/RMI Principles 542 Common Object Request Broker Architecture 547 CORBA Basics 547 RMI-IIOP: Making RMI Compatible with CORBA 551 How to Turn an RMI Object into an RMI-IIOP Object 551 When to Use CORBA 553 Distributed File System Notifications: An Example CORBA System 554 Web Services 563 564 Platform-Independent RPC 566 Summary 597Chapter 12: Service Oriented Integration 599 Service Oriented Architecture 599 Enabling Technology 600 Java Management Extensions 600 Why Is JMX Important? 601 The JMX Architecture 601 Creating and Managing a Standard MBean 602 JMX Management 606 Java Messaging Service 610 Why Is JMS Important? 610 Endpoints: Queues and Topics 610 Sending and Receiving Messages 611 System Integration Patterns 619 Processing Chain 619 Request-Reply 619 Split-Aggregate 621 Summary 623xii
  12. 12. ContentsChapter 13: Java Security 625 Java Cryptography Architecture and Java Cryptography Extension (JCA/JCE) 625 JCA Design and Architecture 626 Java Cryptography Extension 656 Program Security Using JAAS 666 User Identification 667 Executing Code with Security Checks 668 Authorization 672 Summary 673Chapter 14: Packaging and Deploying Your Java Applications 675 Examining Java Classpaths 675 Investigating the Endorsed Directory 680 Exploring Java Archives 681 Manipulating JAR Files 681 Examining the Basic Manifest File 684 Examining Applets and JARs 685 Signing JAR Files 686 Examining the JAR Index Option 690 Creating an Executable JAR 691 Analyzing Applets 691 Basic Anatomy of an Applet 692 Packaging an Applet for Execution 693 Examining Applet Security 694 Exploring Web Applications 694 Examining the WAR Directory Structure 695 Understanding the WAR Deployment Descriptor 696 Packaging Enterprise JavaBeans 698 Inspecting Enterprise Archives 699 The EAR Descriptor File 700 Deployment Scenario 700 Jumping into Java Web Start 702 Examining the TicTacToe Example 702 Summarizing Java Web Start 709 Using Ant with Web Archives 709 Installing Ant 709 Building Projects with Ant 710 Summary 713 Index 715 xiii
  13. 13. AcknowledgmentsFirst, I could not have had any chance of actually getting this book done without the support of mywonderful wife, Alicia. She and my daughter Jennifer, who has far less sophisticated expectations of myliterary skills, are the joy in my life and I look forward to spending more time with them. I love both ofyou more than words can describe. Stephanie, we love you and will never forget you. I would like tothank our technical editor, David Parks for the outstanding job he did on this project—you had NOIDEA what you were agreeing to do when I recruited you into this job! My fellow authors—Donnie,Mark, Scot, and Jeff—have been terrific with their hard work on a demanding project. I appreciate eachof your contributions to this book. I would like to thank Bob Elliott and Brian Herrmann for all of theirhard work and perseverance while working with us on this project. I would like to acknowledge myleadership, Joe Duffy, Bruce Feldman, Jim Moorhead, Don Heginbotham, Jon Grasmeder, and AugieDunheimer, for their dedication to the simple concept of doing the right thing for the right people. It isvery refreshing to work at a company that exercises the inverse of the cynical “zero sum game.” I wouldlike to thank my parents, Bill and Kay, my in-laws, Stephen and Elaine Mellman, my sister Kari, mybrother-in-law Grayson, my brother Morgan, and my stepfather Dave for always being there. I wouldlike to acknowledge my grandmothers, Vivian and Sophie, for being what grandmothers should be.I would also like to acknowledge my team members for the great things they do every day to make theworld a better place: Jon Simasek, Rob Brown, Keith Berman, Mauro Marcellino, Terry Trepel (welcomeback from Iraq!), Marshall Sayen, Joe Sayen, Hanchol Do, Scot Schrager, Don Avondolio, Brian Stearns,Cliff Toma, Mike Clarke, Brad Phillips, Jeff Lane, Nhon Pham, Julia Lee, Vic Fraenckel (welcome backfrom the dead!), Morgan Ruther, Lonnie Haaland, George Burgin, and Mark (Mojo) Mitchell. Matt Tyrrell,I was going to write something witty or amusing, but I think Jennifer put it best, “What time is UncleMatt coming over?”—WCRFirst, I’d like to thank these people for inspiring me in the workplace: Swati Gupta, Chi Louong,Bill Hickey, and Chiming Huang. Thanks to all of the great professors at the Virginia Tech ComputerScience/Information Technology Departments: Shawn Bohner, Tarun Sen, Stephen Edwards, and JohnViega. I am indebted to all of my students who taught me so much through their dedication, hard work,and insight, which has allowed me to incorporate their development wisdom for instruction in thisbook. Appreciation goes out to the sponsors, volunteers, and organizers of The Great Cow Harbor Run(Northport, NY) and The Columbia Triathlon (Columbia, MD) for organizing world-class events I liketo participate in, but more importantly for inspiring me to be a more disciplined and focused person.Special thanks to my friends, the Wieczoreks, Devaneys, Keanes, O’Donohoes, Howards, and Pujols.Lastly, I wish to thank all of the co-authors, who are fun guys to work with and be around: Jeff, Mark,Scot, and Clay and my co-workers: Mauro Marcellino, Joe and Marshall Sayen, Jon Simasek, Terry Trepeland his wonder-dog Ontio, Hanchol Do, Keith Berman, Rob Brown, Dave Parks, Brian Stearns, MikeClarke, Morgan Ruther, Cliff Toma, Matt Tyrrell, the Thoman family (Brettie-boy, Cindy, and babyZoe),Vic Fraenckel, Nhon Pham, Julia Lee, and to my fishing buddy George Burgin. To all of my family:Mom, Dad, Michael, John, Patricia, Keil, Jim, Sue, Reenie, Donna, Kelly, Stephen, Emily, Jack, and Gillian,Matt and Danielle, you guys are great. To my wife Van, whom I love more than anything for her continualsupport during the writing of this book.—DJA
  14. 14. Acknowledgments The first person I would like to thank is Heather. Seven years together and a wonderful eleven-month- old son, you have made me the luckiest guy on earth. Thanks for saying, “I do.” I also need to thank my parents. First I’d like to thank my mom, because she has always been on my side, in support of me and my decisions. And my dad, because he’s the kind of dad I’d like to one day become. I would also like to thank my family who supported me; my sister and her family; Fern, Gary, and Isabel. In addition, I would thank my extended family, Joe, Sabina, Robin, Peter, Brandon, Abby, Christiana, Joe Jr., Chris, Ann, Paige, and Liam. I also need to thank my co-workers who make every day an experience. Clay, thanks for providing the vision and drive to keep this work interesting. We are not in the one’s and zero’s business, we solve problems. Don, thanks for putting up with me. Dave, thanks for putting up with Don and me. Cliff, thanks for doing all the hard work, and please tell Gerry thanks too. I’d also like to thank Marty, Melinda, Brett, Mike, Mark, Terry, Mauro, Marshall, and Keith.—SRS I would like to first thank my wife, Elisa, for supporting me through this book. Projects like this always seem like they will take less time than they actually do, and I thank her for supporting me through my optimistic time estimates. I’d like to thank Clay for giving me the opportunity to write with him, and Don for guiding me through it. I’d also like to thank our technical and development editors, Dave and Brian, for helping me with my chapters. I would like to thank the people I have worked closely with recently: Keith, Jeff, Jon, Terry, Nhon, Matt, Marshall, Joe, Brad, Carlton, Todd, Bryan, Hanchol, Vic, and everyone I have worked with in the past. I have learned a lot simply through watching and listening. There is no greater work atmosphere than the one where you are the least experienced—there is some- thing to be learned from everyone every day. I’d like to thank my parents; my dad for sparking my inter- est in computer science, and my mom for helping me learn to write. Most of all I would like to thank God, as writing this book has been an exercise in faith and trust. Last but not least I would like to thank all of my family and friends for supporting me around book deadlines and understanding where all my time was going.—MWM The following deserve acknowledgment: Dave Nelson for introducing me to programming and for being the main reason I am where I am; my parents and family; our editors at Wiley, Brian Herrmann and Robert Elliott; and Dave Parks, our technical reviewer. To my most important teachers: Alfred Martin and Paul D’Andrea. And finally, to Phil Bickel, Eric Anderton, John Tarcza, Keith Obenschain, Robert Burtt, Joseph Kapp, Randy Nguyen, Leo Pak, Mark Orletsky, Randy Shine, David Hu, Min Soo Yi, and Corey Chang for their support.—JSxvi
  15. 15. Introduction Professional Java Programming provides a bridge from the “how to” language books that dominate the Java space (Teach Yourself Hello World in Java in 24 Hours) and the more detailed, but technologically stove-piped books on topics such as EJB, J2EE, JMX, JMS, and so on. Most development solutions involve using a mix of technologies, and the books for all of these technologies would stand several feet tall. Furthermore, the reader needs but a fraction of the overall content in these books to solve their specific problems. Professional Java Programming provides background information on the technology, practical examples of using the technology, and an explanation of where the reader could find more detailed information. It strives to be a professional reference for the Java developer.Who This Book Is For This book serves three types of readers: ❑ The newly introduced reader who has graduated from Beginning Java, by covering more advanced Java solutions and language features. ❑ The Java developer who needs a good all-purpose reference and a first source when tackling new Java problems that may be outside their technological experience. ❑ The developer who has already had experience with certain solutions, but may not, for example, think it worthwhile to read 500 pages on JMS alone to see if JMS could fit into their solution space. This book can provide reduced barriers to technological entry for these developers.What This Book Covers Professional Java JDK 6 Edition builds upon Ivor Horton’s Beginning Java 2, to provide the reader with an understanding of how professionals use Java to develop software solutions. It starts with a discussion of the tools and techniques of the Java developer, continues with a discussion of the more sophisticated and nuanced parts of the Java SDK, and concludes with several examples of building real Java solutions using Java APIs and open source tools. Professional Java JDK 6 Edition leaves the reader with a well-rounded sur- vey of the professional Java development landscape, without losing focus in exhaustive coverage of indi- vidual APIs. This book is the bridge between Java language texts, methodology books, and specialized Java API books. For example, once you have mastered the basics of the Java language, you will invariably encounter a problem, like building a database-driven web site, which requires you to use a collection of technologies like JSP, and tools like Hibernate; this book provides a concrete solution that integrates both of them. The following figure provides a context to this book’s coverage in relation to other Java books. As you start with the beginning Java books, you would use this book as a solution primer to introduce you to more in-depth books on a particular subject, such as patterns, Web Services, or JDBC.
  16. 16. Introduction Methodology, Patterns, and API Books Professional Java Development Beginning Java Books Figure Intro-1How This Book Is Structured Working as an effective professional Java developer requires two major skills: thinking like a Java devel- oper and having a broad understanding of Java APIs, tools, and techniques to solve a wide variety of Java problems. Reviewing the structure of the book, you can see how the chapters help you realize the goal of improving these skills.Part I: Thinking Like a Java Developer Experienced Java developers recognize that there is a particular mindset among effective Java developers. The first three chapters provide you with strong coverage of topics that will help you achieve that mindset.Chapter 1: Key Java Language Features and Libraries Any introductory Java book will cover the features of the Java programming language. This chapter picks up where those books leave off by focusing on a number of the key sophisticated Java language features such as regular expressions, preferences, and Java logging. Most importantly, this chapter intro- duces Derby, a lightweight database new to Java 6, and reviews language features that were introduced in Java 2 Standard Edition 5.0. These features include generics, metadata, autoboxing, and more.Chapter 2: Tools and Techniques for Developing Java Solutions Making the jump from someone who knows the Java language to a Java developer is an interesting tran- sition. Typically, developers find books that teach the language and books that teach the methodologies.xviii
  17. 17. Introduction Furthermore, methodology books are often written defensively, as if they are defending a dissertation or prescribing a diet. These books often prescribe ritualistic adherence to their methodology, lest you risk failure. New developers can find this approach quite exhausting, because rarely do you start in a posi- tion where you can dictate a team’s process. In this book, you will find a developer’s focused view on methodology and tools with practical insights into how to allow tools to make your work easier and more productive.Chapter 3: Exploiting Patterns in Java Patterns provide an invaluable resource to developers in trying to communicate solutions to common problems. However, as software problems are generally very abstract, understanding common solutions to them—or even the value of the approach—can be a very overwhelming experience. However, as you might imagine, there are some key problems that recur throughout the Java solution space, and therefore, frameworks and APIs are built upon patterns. As such, having a utilitarian under- standing of patterns is invaluable, and arguably unavoidable in becoming an effective Java developer. This chapter explains the critical importance of patterns, provides a practical understanding of patterns, and demonstrates examples of common patterns found in the Java world.Part II: A Broad Understanding of Java APIs, Tools, andTechniques The Java platform has extended beyond being a simple applet development language at its inception to three distinct editions targeted at three different platforms. Not only has the platform evolved into a huge undertaking, but the open source movement and the Java community have also added features and tools that provide even more options to the Java developer. Therefore, you can find yourself easily overwhelmed. This part of the book provides a series of common problems across the Java development space. In each area, you will be introduced to a problem, and a focused solution to that problem. These solutions do not attempt to provide comprehensive coverage of all of the involved APIs, but rather a primer needed to solve that problem. From there, you could bridge into a book with more specialized coverage. The primary intent is to not require a three-foot tall stack of books to address a simple end-to-end solution to a common development problem.Chapter 4: Developing Effective User Interfaces with JFC Commonly referred to simply as Swing, the Java Foundation Classes provide the functionality to build user interfaces and desktop applications. As these classes frequently make up most of the logical exam- ples within introductory Java books, it makes logical sense to start with a Swing example. However, this chapter covers the intricacies of Swing in more detail, including some advanced topics like Layout Managers and Java 2D.Chapter 5: Persisting Your Application Using Files One of the more important things for any application to be able to do is persist (that is, save) its state. In this chapter, you will discover techniques to implement save and restore functionality, using two differ- ent methods, Java object serialization and the Java API for XML Binding (JAXB). xix
  18. 18. IntroductionChapter 6: Persisting Your Application Using Databases Files are traditionally used to share data in a single-threaded mode—one user at a time. When data must be shared throughout the enterprise, you use a database. In this chapter, you learn the more advanced features of the Java Database Connectivity API (JDBC) 4.0. Furthermore, this chapter addresses one of the more popular object persistence frameworks (and the foundation for the development of the new EJB 3.0 specification)—Hibernate.Chapter 7: Developing Web Applications Using the Model 1Architecture Those who have been developing web applications for a long time recognize that the page-centric paradigm, also known as the Model 1 Architecture, has been used across many technology platforms (ASP, Cold Fusion, Perl, and so on) to develop web applications. Java supports this paradigm through its Java Server Pages and Java Standard Tag Library specifications. In this chapter, you learn about these frameworks as well as other best practices in developing web applications within the Model 1 Architecture.Chapter 8: Developing Web Applications Using the Model 2 Architecture As web applications have evolved, there has been recognition of some weaknesses in the page-centric approach of the Model 1 Architecture. In this chapter, you learn about these weaknesses and how they gave rise to the Model 2 Architecture, which is component-centric. You will see how using a component framework like WebWork allows for easy integration of other components like Hibernate.Chapter 9: Interacting with C/C++ Using Java Native Interface Frequently, you have application components that are regrettably not written in the Java programming language, often not alleviating the need for those components to be accessible by your application. The solution to this problem is the Java Native Interface. This chapter explains the intricacies of JNI, as well as a number of the potential pitfalls.Chapter 10: EJB 3 and the Java Persistence API Enterprise JavaBeans (EJB) is Java’s distributed component technology and the cornerstone of the Java 2 Enterprise Edition platform. EJB 3.0 represents a significant improvement in the Java language by lever- aging the Plain Old Java Object (POJO) paradigm with the Java Persistence API to provide reliable, robust, and transparent object persistence. This chapter explains the EJB 3 specification and the Java Persistence API and demonstrates their utility to developing enterprise Java solutions.Chapter 11: Communicating between Java Components andComponents of Other Platforms While RMI has proven to be a good solution for Java-to-Java communication, there are still a tremen- dous number of needs to access (or provide access to) components of other platforms. This is particu- larly true of the Microsoft .NET platform. This chapter explains the basics of interprocess communication, discusses several techniques for interprocess communication, and culminates in an example using Web Services.xx
  19. 19. IntroductionChapter 12: Service Oriented Integration When performing enterprise application integration of components distributed across many machines and platforms, it is often necessary for you to be able to spread the workload across many different steps. There are two APIs that are particularly useful in this regard, the Java Message Service (JMS) and the Java Management Extensions (JMX). In this chapter, you see the core of these two APIs tied together to provide a highly useful architecture.Chapter 13: Java Security Information security is tremendously important to Java development. In this chapter, you see how your application can be secured using the Java Authorization and Authentication Service (JAAS) and your data can be secured using the Java Cryptography Extensions (JCE). Also detailed in this chapter is the new XML digital signature support introduced in Java 6, useful in building security for Web Services.Chapter 14: Packaging and Deploying Your Java Applications One of the trickiest and most painful things about developing Java applications, whether they are enterprise or desktop applications, is packaging and deploying your application. There are a multitude of deployment descriptors and packaging rules that exist in many of the Java APIs. There are JARs, WARs, EARs, and more on the way. Often you get cursory understanding of these formats and specifications within each of the stovepipe books. In this chapter, you learn about a number of the packaging mechanisms that exist in Java, as well as descriptions of the deployment descriptors for each of those mechanisms.What You Need to Use This Book This book is based upon Java 2 Standard Edition version 6.0. You might find it helpful to have an Integrated Development Environment (IDE) of your choice—Eclipse is a very good and popular one ( Furthermore, depending on the chapter, you may need to use an application server like JBoss ( or Tomcat ( The need to download an application server, as well as any other downloads (of APIs and so on), is addressed in each chapter.Conventions To help you get the most from the text and keep track of what’s happening, a number of conventions are used throughout the book. Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text. Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this. xxi
  20. 20. Introduction As for styles in the text, the following are standard for the book: ❑ Important words are highlighted when they are introduced. ❑ Keyboard strokes are shown like this: Ctrl+A. ❑ File names, URLs, and code within the text are like so: ❑ Code is presented in two different ways: In code examples, new and important code is highlighted with a gray background. The gray highlighting is not used for code that’s less important in the present context, or has been shown before.Source Code As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is avail- able for download at Once at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book. Because many books have similar titles, you may find it easiest to search by ISBN; for this book the ISBN is 978-0-471-77710-6. Once you download the code, just decompress it with your favorite compression tool. Alternatively, you can go to the main Wrox code download page at to see the code available for this book and all other Wrox books.Errata We make every effort to ensure that there are no errors in the text or in the code. However, no one is per- fect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information. To find the errata page for this book, go to and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list includ- ing links to each book’s errata is also available at If you don’t spot the error you are experiencing on the Book Errata page, go to techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subse- quent editions of the book.xxii
  21. 21. For author and peer discussion, join the P2P forums at The forums are a web- based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of inter- est of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums. At you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps: 1. Go to and click the Register link. 2. Read the terms of use and click Agree. 3. Complete the required information to join as well as any optional information you wish to pro- vide and click Submit. 4. You will receive an e-mail with information describing how to verify your account and com- plete the registration process. You can read messages in the forums without joining P2P, but to post your own messages, you must join. Once you join, you can post new messages and respond to messages other users post. You can read mes- sages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing. For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to ques- tions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page. xxiii
  22. 22. Part I: Thinking Like a Java DeveloperChapter 1: Key Java Language Features and LibrariesChapter 2: Tools and Techniques for Developing Java SolutionsChapter 3: Exploiting Patterns in Java
  23. 23. Key Java Language Features and Libraries The past two major releases of the JDK have seen some significant changes. JDK 5 introduced new features at the language level, something that has not happened since Java was first released. Some of the most significant features added to the language are generics (parameterized types), enumerations, and metadata. With JDK 6, one of the biggest changes is the inclusion of a lightweight database known as Derby, which is from the Apache Database project. The first half of this chapter introduces Derby and reviews the new language features from JDK 5. The second half of this chapter details certain key utility packages in the java.util branch of the class library that are useful for professional programming in Java.Introducing Derby New to Java 6 is a lightweight database called Derby, a product of the Apache Database project. Derby is a transactional, relational database and provides a small footprint on disk. As of the July 13, 2006 release of the Java 6 JDK, Derby is installed (by default) in C:Program FilesJava jdk1.6.0db and includes the core libraries, demonstration programs, and an example database. Derby started its life as CloudScape, a product IBM acquired with Informix. In 2004, IBM decided to open source CloudScape and it became an incubator project under the Apache Software Foundation with the name Derby. The real benefits to using Derby are that it has minimal adminis- tration needs and a small footprint. The databases are small on disk, roughly 2MB for a basic database. The fact that administration is minimal allows you, as a developer, to easily create and use databases in code. This speeds up development. Deployment is made that much easier because Derby supports storage of a database archived in a JAR file, allowing you to simply dis- tribute the JAR file. Because Derby is an involved topic, this section serves only to introduce Derby and its features, using the included command-line tool and a brief exploration of using the JDBC driver. Derby is revisited later in this book.
  24. 24. Part I: Thinking Like a Java DeveloperUsing Derby As mentioned, Derby is automatically installed as part of the JDK. Derby provides a command-line tool called ij, which is an abbreviation for interactive JDBC scripting tool. This tool provides a way to con- nect to and manipulate Derby databases. You must have the following JAR files in your classpath before using this tool. The derby.jar file contains the JDBC drivers, and derbytools.jar contains the ij tool itself: c:Program FilesJavajdk1.6.0dblibderby.jar c:Program FilesJavajdk1.6.0dblibderbytools.jar After your classpath is configured, start the tool and connect to the example database (toursdb) included with Derby: c:>java ij version 10.2 ij> connect ‘jdbc:derby:c:Program FilesJavajdk1.6.0dbdemodatabasestoursdb’; ij> Don’t forget the semicolon at the end of a command. If you leave this off, ij may seem like it’s process- ing a command, but it isn’t. This provides for ease of entering multiline commands such as creating tables or complicated select statements. These semicolons are confined to ij and are not passed to the database. The tool works much as you would expect it to, such as issuing a select statement to retrieve a partial listing of data from the countries table (a table that is part of the example toursdb): ij> select * from countries where country like ‘A%’; COUNTRY |C&|REGION -------------------------------------------------------- Afghanistan |AF|Asia Albania |AL|Europe Algeria |DZ|North Africa American Samoa |AS|Pacific Islands Angola |AO|Africa Argentina |AR|South America Armenia |AM|Europe Australia |AU|Australia and New Zealand Austria |AT|Europe Azerbaijan |AZ|Central Asia To create a new database from ij, include the parameter create=true to the connection string. Because you’re already connected to the toursdb, first disconnect. The select statement proves you’re discon- nected. Then issue the new connect statement: ij> disconnect; ij> select * from countries; IJ ERROR: Unable to establish connection ij> connect ‘jdbc:derby:DerbyTestDB;create=true’; ij>4
  25. 25. Chapter 1: Key Java Language Features and LibrariesThe database name (in this case, DerbyTestDB) is created as a subdirectory of the directory where youstarted the ij tool. The database appears on disk in the directory C:DerbyTestDB. Exploring thisdirectory is strictly for curiosity’s sake — you should never have to modify any file in this directory,including the file that may seem tempting to play with. The creation of adatabase also creates a derby.log file at the same level as the DerbyTestDB, so the file in this case isC:derby.log. This log file is an error log and is useful to check to get more information about theinevitable problems that arise during real development. If you create multiple databases they will sharethis log file.Now that you have a new database, create a table, insert some data, and query it: ij> create table zipcodes(zipcode varchar(5), city varchar(20), state varchar(2)); 0 rows inserted/updated/deleted ij> insert into zipcodes values (‘22030’, ‘Fairfax’, ‘VA’); 1 row inserted/updated/deleted ij> insert into zipcodes values (‘22003’, ‘Annandale’, ‘VA’); 1 row inserted/updated/deleted ij> insert into zipcodes values (‘90210’, ‘Beverly Hills’, ‘CA’); 1 row inserted/updated/deleted ij> select * from zipcodes; ZIPC&|CITY |STA& ------------------------------- 22030|Fairfax |VA 22003|Annandale |VA 90210|Beverly Hills |CA 3 rows selected ij>By default, auto-commit is on from the ij tool, so you don’t have to issue the commit; command tosave changes to the database. You can control auto-commit by issuing the command “autocommit on;”or “autocommit off;” Type “exit;” to exit from the ij tool.Now that you have seen the basics of using the ij tool, look at an example of querying your newly cre-ated database from a Java program using the JDBC driver. Because the standard JDBC mechanism isused, there are no surprises with the import statements: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DerbyTestDBClient { public static void main(String[] args) { DerbyTestDBClient testClient = new DerbyTestDBClient(); testClient.showZipCodes(); } 5
  26. 26. Part I: Thinking Like a Java Developer The showZipCodes method actually opens the connection and performs the query. The driver used is org.apache.derby.jdbc.EmbeddedDriver. Derby also includes a ClientDriver for connecting to Derby in network mode, where Derby runs a network server providing for a client/server approach to using Derby: public void showZipCodes() { try { String driver = “org.apache.derby.jdbc.EmbeddedDriver”; Class.forName(driver).newInstance(); Connection conn = null; conn = DriverManager.getConnection(“jdbc:derby:DerbyTestDB”); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery(“SELECT city, state, zipcode FROM zipcodes”); while( { System.out.println(“City : “ + rs.getString(1)); System.out.println(“State : “ + rs.getString(2)); System.out.println(“Zipcode: “ + rs.getString(3)); System.out.println(); } rs.close(); s.close(); conn.close(); } catch(Exception e) { System.out.println(“Exception: “ + e); e.printStackTrace(); } } } Here’s the output from the previous code: c:>java DerbyTestDBClient City : Fairfax State : VA Zipcode: 22030 City : Annandale State : VA Zipcode: 22003 City : Beverly Hills State : CA Zipcode: 90210 Derby is a thriving project and continues to implement more features. You can keep an eye on its devel- opment at
  27. 27. Chapter 1: Key Java Language Features and LibrariesLanguage Features Added in Java 5 Several useful syntactic elements were introduced in Java 5. All these features are supported by an updated compiler, and all translate to already defined Java bytecode, meaning that virtual machines can execute these features with no need for an update: ❑ Generics: A way to make classes type-safe that are written to work on any arbitrary object type, such as narrowing an instance of a collection to hold a specific object type and eliminating the need to cast objects when taking an object out of the collection. ❑ Enhanced for loop: A cleaner and less error-prone version of the for loop for use with iterators. ❑ Variable arguments: Support for passing an arbitrary number of parameters to a method. ❑ Boxing/unboxing: Direct language support for automatic conversion between primitive types and their reference types (such as int and Integer). ❑ Type-safe enumerations: Clean syntax for defining and using enumerations, supported at the language level. ❑ Static import: Ability to access static members from a class without need to qualify them with a class name. ❑ Metadata: Coupled with new tools developed by third-party companies, saves developers the effort of writing boilerplate code by automatically generating the code. These features update the Java language to include many constructs developers are used to in other lan- guages. They make writing Java code easier, cleaner, and faster. Even if you choose not to take advan- tage of these features, familiarity with them is vital to read and maintain code written by other developers.Generics Java 5 introduced generics, also known as parameterized types. Generics allow you to write a class that can operate on any type but that specific type is not specified until declaration of an instance of the class. Because this type is not specified as part of the class definition, the class becomes generic, gaining the ability to work on any type specified. The most obvious example, and a great use of generics, is the col- lection classes. The ArrayList class, for example, was written to hold, simply, Object. This means objects lose their type when added to the ArrayList and a cast is needed when accessing an element of the ArrayList. However, code that uses a generic version of the ArrayList can say “I want this ArrayList to hold only Strings.” This adds additional type-safety to Java because, if anything other than a String is added to the collection, the compiler will catch it. This also means that a cast is no longer needed when accessing elements — the compiler knows it only holds Strings and will produce an error if the elements are treated as anything other than a String. Specifying String as the parame- terized type is as easy as placing the type in angle brackets: ArrayList<String> listOfStrings; // <TYPE_NAME> is new to the syntax String stringObject; listOfStrings = new ArrayList<String>(); // <TYPE_NAME> is new to the syntax listOfStrings.add(new String(“Test string”)); // Can only pass in String objects stringObject = listOfStrings.get(0); // no cast required 7
  28. 28. Part I: Thinking Like a Java Developer Generics are also known as parameterized types where a type is the parameter. As can be seen in the previ- ous example, String is the formal type parameter. This same parameterized type must be used when instantiating the parameterized type. Because one of the goals of the new language features in Java 5 was to not change the Java instruction set, generics are, basically, syntactic sugar. When accessing elements of the ArrayList, the compiler automatically inserts the casts that you now don’t have to write. It’s also possible to use the primitive data types as a parameterized type, but realize that these incur boxing/unboxing costs because they are implicitly converted to and from Object. Nonetheless, there are benefits in increased type-safety and increased program readability.Type Erasure A generic type in Java is compiled to a single class file. There aren’t separate versions of the generic type for each formal parameterized type. The implementation of generics utilizes type erasure, which means the actual parameterized type is reduced to Object. Strangely, the decision to use erasure, although requiring no bytecode changes, hobbles the generics mechanism in its determination to maintain strong typing, as you’ll soon see. By way of example, the following code will not compile: interface Shape { void draw(); } class Square implements Shape { public String name; public Square() { name = “Square”; } public void draw() { System.out.println(“Drawing square”); } } public class ErasureExample { public static <T> void drawShape(T shape) { shape.draw(); } public static void main(String args[]) { Square square = new Square(); } }8
  29. 29. Chapter 1: Key Java Language Features and LibrariesThe compiler issues the following error: cannot find symbol symbol : method draw() location: class java.lang.Object shape.draw(); ^ 1 errorIf you replace the drawShape method with the following, the compiler is now happy to compile theprogram: public static <T> void drawShape(T shape) { System.out.println(“Hashcode: “ + shape.hashCode()); }Why this discrepancy? It’s the result of type erasure. The hashCode method belongs to Object, how-ever the draw method belongs only to objects of type Shape. This little experiment demonstrates thatthe parameterized type is actually reduced to an Object. The next example shows how this relates touse of a generic class with different parameterized types.Start with a new generic class to hold a data item of arbitrary type: public class CustomHolder<E> { E storedItem; public E getItem() { return(storedItem); } public void putItem(E item) { System.out.println(“Adding data of type “ + item.getClass().getName()); storedItem = item; } }By convention, single letters are used for formal type parameters, usually E for element and T for type.Add a main method to this class: public static void main(String args[]) { CustomHolder<String> stringHolder = new CustomHolder<String>(); CustomHolder<Object> objectHolder = new CustomHolder<Object>(); String str = new String(“test string”); String str2; stringHolder.putItem(str); 9
  30. 30. Part I: Thinking Like a Java Developer objectHolder.putItem(str); str2 = stringHolder.getItem(); //str2 = objectHolder.getItem(); } Look at the last two lines. Retrieving an element from stringHolder and assigning it to a string is fine. However, if you uncomment the second line, which tries to access the same string in the objectHolder, you get the following compiler error. c:>javac incompatible types found : java.lang.Object required: java.lang.String str2 = objectHolder.getItem(); ^ 1 error This makes sense because the actual type parameter (in this case, String or Object) dictates the type. When you add a String to the objectHolder, it is simply stored as an Object. When you attempt to assign the Object to the String (in the call to objectHolder.getItem), you now need an explicit cast to the String type. Because of type erasure, it is possible to assign a generic class reference to a reference of its nongeneric (legacy) version. Therefore, the following code compiles without error: Vector oldVector; Vector<Integer> intVector; oldVector = intVector; // valid However, though not an error, assigning a reference to a nongeneric class to a reference to a generic class will cause an unchecked compiler warning. This happens when an erasure changes the argument types of a method or a field assignment to a raw type if the erasure changes the method/field type. As an example, the following program causes the warnings shown after it. You must pass -Xlint:unchecked on the command line to javac to see the specific warnings: import java.util.*; public class UncheckedExample { public void processIntVector(Vector<Integer> v) { // perform some processing on the vector } public static void main(String args[]) { Vector<Integer> intVector = new Vector<Integer>(); Vector oldVector = new Vector(); UncheckedExample ue = new UncheckedExample(); // This is permitted10
  31. 31. Chapter 1: Key Java Language Features and Libraries oldVector = intVector; // This causes an unchecked warning intVector = oldVector; // This is permitted ue.processIntVector(intVector); // This causes an unchecked warning ue.processIntVector(oldVector); } } Attempting to compile the previous code leads to the following compiler warnings: warning: unchecked assignment: java.util.Vector to java.util.Vector<java.lang.Integer> intVector = oldVector; // This causes an unchecked warning warning: unchecked method invocation: processIntVector(java.util.Vector<java.lang.Integer>) in UncheckedExample is applied to (java.util.Vector) ue.processIntVector(oldVector); // This causes an unchecked warning 2 warningsWildcards and Bounded Type Variables Because you can’t use CustomHolder<Object> as if it were a super-type of CustomHolder<String>, you can’t write a method that would process both CustomHolder<Object> and CustomHolder<String>. There is, however, a special way to accomplish this. As part of the generics syntax, a wildcard is intro- duced, which, when used, basically means “any type parameter.” Revisit the previous example and show how the wildcard, a single question mark, is used. Take the CustomHolder class and add a few new methods and a new main as follows: public static void processHolderObject(CustomHolder2<Object> holder) { Object obj = holder.getItem(); System.out.println(“Item is: “ + obj); } public static void processHolderString(CustomHolder2<String> holder) { Object obj = holder.getItem(); System.out.println(“Item is: “ + obj); } public static void processHolderWildcard (CustomHolder2<?> holder) { Object obj = holder.getItem(); System.out.println(“Item is: “ + obj); 11
  32. 32. Part I: Thinking Like a Java Developer } public static void main(String args[]) { CustomHolder2<String> stringHolder = new CustomHolder2<String>(); CustomHolder2<Object> objectHolder = new CustomHolder2<Object>(); String str = new String(“test string”); String str2; stringHolder.putItem(str); objectHolder.putItem(str); //processHolderObject(stringHolder); processHolderObject(objectHolder); processHolderString(stringHolder); //processHolderString(objectHolder); processHolderWildcard(stringHolder); processHolderWildcard(objectHolder); } The two lines that are commented will prevent the program from compiling. If both these lines are uncommented, the compiler issues the following errors: c:>javac processHolderObject(CustomHolder2<java.lang.Object>) in C ustomHolder2<E> cannot be applied to (CustomHolder2<java.lang.String>) processHolderObject(stringHolder); ^ processHolderString(CustomHolder2<java.lang.String>) in C ustomHolder2<E> cannot be applied to (CustomHolder2<java.lang.Object>) processHolderString(objectHolder); ^ 2 errors This reminds you that the type parameter used must match the formal type parameter. However, notice that neither line that invokes processHolderWildcard is commented. This is because using the wild- card allows you to pass in either stringHolder or objectHolder. You can read the method parameter type CustomerHolder2<?> as “a CustomHolder2 of any type” as opposed to “of Object type” or “of String type.” A type parameter can be restricted to certain types by what is called a bound. A bound can be applied to a regular type parameter or a wildcard. Revisit the Shape example from earlier in the chapter, which defines a Shape interface: import java.util.ArrayList; import java.util.Iterator; interface Shape { void draw();12
  33. 33. Chapter 1: Key Java Language Features and Libraries } class Square implements Shape { public void draw() { System.out.println(“Drawing square”); } } class Circle implements Shape { public void draw() { System.out.println(“Drawing circle”); } }Now define a PaintProgram class to demonstrate bounds. If you add a drawShape method that definesa type parameter, this won’t work: public static <S> void drawShape(S shape) { shape.draw(); }So you must add a bound to the type parameter so Java treats the shape formal type parameter as aShape and not an Object. By bounding the type parameter to Shape, you dictate that the object passedin must derive directly or indirectly from Shape. Because of this, Java knows that the object is a Shapeand thus can invoke methods that belong to Shape instead of only Object methods: public static <S extends Shape> void drawShapeBounded(S shape) { shape.draw(); }As alluded to earlier, this may make you wonder if generics really are that useful. If you have to explic-itly state the bounds on a type parameter, you may as well just use the Shape interface to constrain anormal method parameter. One of the places generics really do shine is easing the use of collections, andthis is probably the main justification for adding generics to Java.Look at implementing a drawAllShapes method that takes a parameterized ArrayList. As expected,you need a bound here so Java does not treat the contents of the ArrayList as Objects: public static <T extends Shape> void drawAllShapes(ArrayList<T> shapeList) { T shape; Iterator<T> shapeIterator; shapeIterator = shapeList.iterator(); while(shapeIterator.hasNext()) { shape =; shape.draw(); } } 13
  34. 34. Part I: Thinking Like a Java Developer By constraining the T type parameter, invoking draw is acceptable because Java knows it’s a Shape. If you want to specify multiple interfaces/classes to use as a bound, separate them with the ampersand (&). Also note that extends is used to specify bounds regardless of whether the type parameter is bounded by an interface or a class.Using Generics It is straightforward to create objects of a generic type. Any parameters must match the bounds speci- fied. Although you might expect to create an array of a generic type, this is only possible with the wild- card type parameter. It is also possible to create a method that works on generic types. This section describes these usage scenarios.Class Instances Creating an object of a generic class consists of specifying types for each parameter and supplying any necessary arguments to the constructor. The conditions for any bounds on type variables must be met. Note that only reference types are valid as parameters when creating an instance of a generic class. Trying to use a primitive data type causes the compiler to issue an unexpected type error. This is a simple creation of a HashMap that assigns Floats to Strings: HashMap<String,Float> hm = new HashMap<String,Float>();Arrays Arrays of generic types and arrays of type variables are not allowed. Attempting to create an array of parameterized Vectors, for example, causes a compiler error: import java.util.*; public class GenericArrayExample { public static void main(String args[]) { Vector<Integer> vectorList[] = new Vector<Integer>[10]; } } If you try to compile that code, the compiler issues the following two errors. This code is the simplest approach to creating an array of a generic type and the compiler tells you explicitly that creating a generic type array is forbidden: arrays of generic types are not allowed Vector<Integer> vectorList[] = new Vector<Integer>[10]; ^ arrays of generic types are not allowed Vector<Integer> vectorList[] = new Vector<Integer>[10]; ^ 2 errors You can, however, create an array of any type by using the wildcard as the type parameter.14
  35. 35. Chapter 1: Key Java Language Features and LibrariesGeneric Methods In addition to the generic mechanism for classes, generic methods are introduced. The angle brackets for the parameters appear after all method modifiers but before the return type of the method. Following is an example of a declaration of a generic method: static <Elem> void swap(Elem[] a, int i, int j) { Elem temp = a[i]; a[i] = a[j]; a[j] = temp; } The syntax for the parameters in a generic method is the same as that for generic classes. Type variables can have bounds just like they do in class declarations. Two methods cannot have the same name and argu- ment types. If two methods have the same name and argument types, and have the same number of type variables with the same bounds, then these methods are the same and the compiler will generate an error.Generics and Exceptions Type variables are not permitted in catch clauses, but can be used in throws lists of methods. An exam- ple of using a type variable in the throws clause follows. The Executor interface is designed to execute a section of code that may throw an exception specified as a parameter. In this example, the code that fills in the execute method might throw an IOException. The specific exception, IOException, is specified as a parameter when creating a concrete instance of the Executor interface: import*; interface Executor<E extends Exception> { void execute() throws E; } public class GenericExceptionTest { public static void main(String args[]) { try { Executor<IOException> e = new Executor<IOException>() { public void execute() throws IOException { // code here that may throw an // IOException or a subtype of // IOException } }; e.execute(); } catch(IOException ioe) { System.out.println(“IOException: “ + ioe); ioe.printStackTrace(); } } } 15
  36. 36. Part I: Thinking Like a Java Developer The specific type of exception is specified when an instance of the Executor class is created inside main. The execute method throws an arbitrary exception that it is unaware of until a concrete instance of the Executor interface is created.Enhanced for Loop The for loop has been modified to provide a cleaner way to process an iterator. Using a for loop with an iterator is error prone because of the slight mangling of the usual form of the for loop since the update clause is placed in the body of the loop. Some languages have a foreach keyword that cleans up the syntax for processing iterators. Java opted not to introduce a new keyword, instead deciding to keep it simple and introduce a new use of the colon. Traditionally, a developer will write the following code to use an iterator: for(Iterator iter = intArray.iterator(); iter.hasNext(); ) { Integer intObject = (Integer); // ... more statements to use intObject ... } The problem inherent in this code lies in the missing update clause of the for loop. The code that advances the iterator is moved into the body of the for loop out of necessity, because it also returns the next object. The new and improved syntax that does the same thing as the previous code snippet is as follows: for(Integer intObject : intArray) { // ... same statements as above go here ... } This code is much cleaner and easier to read. It eliminates all the potential from the previous construct to introduce errors into the program. If this is coupled with a generic collection, the type of the object is checked versus the type inside the collection at compile time. Support for this new for loop requires a change only to the compiler. The code generated is no different from the same code written in the traditional way. The compiler might translate the previous code into the following, for example: for(Iterator<Integer> $iter = intArray.iterator(); $iter.hasNext(); ) { Integer intObject = $; // ... statements ... } The use of the dollar sign in the identifier in this example merely means the compiler generates a unique identifier for the expansion of the new for loop syntax into the more traditional form before compiling. The same syntax for using an iterator on a collection works for an array. Using the new for loop syntax on an array is the same as using it on a collection: for(String strObject : stringArray) { // ... statements here using strObject ... }16
  37. 37. Chapter 1: Key Java Language Features and Libraries However, the compiler expands the array version to code slightly longer than the collection version: String[] $strArray = stringArray; for(int $i = 0; $i < $strArray.length; $i++) { String strObject = $strArray[$i]; // ... statements here ... } The compiler this time uses two temporary and unique variables during the expansion. The first is an alias to the array, and the second is the loop counter.Additions to the Java Class Library To fully support the new for loop syntax, the object iterated over must be an array or inherit from a new interface, java.lang.Iterable, directly or indirectly. The existing collection classes were retrofitted for the release of JDK 5. The new Iterable interface looks like this: public interface Iterable { /** * Returns an iterator over the elements in this collection. There are no * guarantees concerning the order in which the elements are returned * (unless this collection is an instance of some class that provides a * guarantee). * * @return an Iterator over the elements in this collection. */ SimpleIterator iterator(); } Additionally, java.util.Iterator will be retrofitted to implement java.lang.ReadOnlyIterator, as shown here: public interface ReadOnlyIterator { /** * Returns true if the iteration has more elements. (In other * words, returns true if next would return an element * rather than throwing an exception.) * * @return true if the iterator has more elements. */ boolean hasNext(); /** * Returns the next element in the iteration. * * @return the next element in the iteration. * @exception NoSuchElementException iteration has no more elements. */ Object next(); } 17
  38. 38. Part I: Thinking Like a Java Developer The introduction of this interface prevents dependency on the java.util interfaces. The change in the for loop syntax is at the language level and it makes sense to ensure that any support needed in the class library is located in the java.lang branch.Variable Arguments C and C++ are a couple of the languages that support variable length argument lists for functions. Java decided to introduce this aspect into the language. Only use variable argument parameter lists in cases that make sense. If you abuse them, it’s easy to create source code that is confusing. The C language uses the ellipsis (three periods) in the function declaration to stand for “an arbitrary number of parameters, zero or more.” Java also uses the ellipsis but combines it with a type and identifier. The type can be anything — any class, any primitive type, even array types. When using it in an array, however, the ellip- sis must come last in the type description, after the square brackets. Because of the nature of variable arguments, each method can only have a single type as a variable argument and it must come last in the parameter list. Following is an example of a method that takes an arbitrary number of primitive integers and returns their sum: public int sum(int... intList) { int i, sum; sum=0; for(i=0; i<intList.length; i++) { sum += intList[i]; } return(sum); } All arguments passed in from the position of the argument marked as variable and beyond are combined into an array. This makes it simple to test how many arguments were passed in. All that is needed is to ref- erence the length property on the array, and the array also provides easy access to each argument. Here’s a full sample program that adds up all the values in an arbitrary number of arrays: public class VarArgsExample { int sumArrays(int[]... intArrays) { int sum, i, j; sum=0; for(i=0; i<intArrays.length; i++) { for(j=0; j<intArrays[i].length; j++) { sum += intArrays[i][j]; } } return(sum);18
  39. 39. Chapter 1: Key Java Language Features and Libraries } public static void main(String args[]) { VarArgsExample va = new VarArgsExample(); int sum=0; sum = va.sumArrays(new int[]{1,2,3}, new int[]{4,5,6}, new int[]{10,16}); System.out.println(“The sum of the numbers is: “ + sum); } } This code follows the established approach to defining and using a variable argument. The ellipsis comes after the square brackets (that is, after the variable argument’s type). Inside the method the argu- ment intArrays is simply an array of arrays.Boxing and Unboxing Conversions One tedious aspect of the Java language in the past is the manual operation of converting primitive types (such as int and char) to their corresponding reference type (for example, Integer for int and Character for char). The solution to getting rid of this constant wrapping and unwrapping is boxing and unboxing conversions.Boxing Conversions A boxing conversion is an implicit operation that takes a primitive type, such as int, and automatically places it inside an instance of its corresponding reference type (in this case, Integer). Unboxing is the reverse operation, taking a reference type, such as Integer, and converting it to its primitive type, int. Without boxing, you might add an int primitive to a collection (which holds Object types) by doing the following: Integer intObject; int intPrimitive; ArrayList arrayList = new ArrayList(); intPrimitive = 11; intObject = new Integer(intPrimitive); arrayList.put(intObject); // cannot add intPrimitive directly Although this code is straightforward, it is more verbose than necessary. With the introduction of boxing conversions, the preceding code can be rewritten as follows: int intPrimitive; ArrayList arrayList = new ArrayList(); intPrimitive = 11; // here intPrimitive is automatically wrapped in an Integer arrayList.put(intPrimitive); 19
  40. 40. Part I: Thinking Like a Java Developer The need to create an Integer object to place an int into the collection is no longer needed. The boxing conversion happens such that the resulting reference type’s value() method (such as intValue() for Integer) equals the original primitive type’s value. Consult the following table for all valid boxing con- versions. If there is any other type, the boxing conversion becomes an identity conversion (converting the type to its own type). Note that due to the introduction of boxing conversions, several forbidden con- versions referring to primitive types are no longer forbidden because they now can be converted to cer- tain reference types. Primitive Type Reference Type boolean Boolean byte Byte char Character short Short int Integer long Long float Float double DoubleUnboxing Conversions Java also introduces unboxing conversions, which convert a reference type (such as Integer or Float) to its primitive type (such as int or float). Consult the following table for a list of all valid unboxing conversions. The conversion happens such that the value method of the reference type equals the resulting primitive value. Reference Type Primitive Type Boolean boolean Byte byte Character char Short short Integer int Long long Float float Double double20
  41. 41. Chapter 1: Key Java Language Features and LibrariesValid Contexts for Boxing and Unboxing Conversions Because the boxing and unboxing operations are conversions, they happen automatically with no spe- cific instruction by the programmer (unlike casting, which is an explicit operation). There are several contexts in which boxing and unboxing conversions can happen.Assignments An assignment conversion happens when the value of an expression is assigned to a variable. When the type of the expression does not match the type of the variable, and there is no risk of data loss, the con- version happens automatically. The precedence of conversions that happen is the identity conversion, a widening primitive conversion, a widening reference conversion, and then the new boxing (or unbox- ing) conversion. If none of these conversions are valid, the compiler issues an error.Method Invocations When a method call is made, and the argument types don’t match precisely with those passed in, several conversions are possible. Collectively, these conversions are known as method invocation conversions. Each parameter that does not match precisely in type to the corresponding parameter in the method sig- nature might be subject to a conversion. The possible conversions are the identity conversion, a widen- ing primitive conversion, a widening reference conversion, and then the new boxing (or unboxing) conversion. The most specific method must be chosen anytime more than one method matches a particular method call. The rules to match the most specific method change slightly with the addition of boxing conver- sions. If all the standard checks for resolving method ambiguity fail, the boxing/unboxing conversion won’t be used to resolve ambiguity. Therefore, by the time checks are performed for boxing conversions, the method invocation is deemed ambiguous and fails. Combining boxing with generics allows you to write the following code: import java.util.*; public class BoxingGenericsExample { public static void main(String args[]) { HashMap<String,Integer> hm = new HashMap<String,Integer>(); hm.put(“speed”, 20); } } The primitive integer 20 is automatically converted to an Integer and then placed into the HashMap under the specified key.Static Imports Importing static data is introduced into the language to simplify using static attributes and methods. After importing static information, the methods/attributes can then be used without the need to qualify the method or attribute with its class name. For example, by importing the static members of the Math class, you can write abs or sqrt instead of Math.abs and Math.sqrt. 21