Preface IIntroduction Objectives I-2 Course Overview I-3 1Introducing the Java and Oracle Platforms Objectives 1-2 What Is Java? 1-3 Key Benefits of Java 1-4 An Object-Oriented Approach 1-6 Platform Independence 1-7 Using Java with Enterprise Internet Computing 1-8 Using the Java Virtual Machine 1-10 How Does JVM Work? 1-12 Benefits of Just-In-Time (JIT) Compilers 1-14 Implementing Security in the Java Environment 1-16 Deployment of Java Applications 1-18 Using Java with Oracle 10g 1-19 Java Software Development Kit 1-20 Using the Appropriate Development Kit 1-21 Integrated Development Environment 1-22 Exploring the JDeveloper Environment 1-23 Oracle 10g Products 1-24 Summary Defining Object-Oriented Principles Objectives 2-2 What Is Modeling? 2-4 What Are Classes and Objects? 2-5 An Object s Attributes Maintain Its State 2-6 Objects Have Behavior 2-8 Objects Are Modeled as Abstractions 2-9 Defining Object Composition 2-11 The Donut Diagram 2-13 Guided Practice: Spot the Operations and Attributes 2-14 Collaborating Objects 2-15 Objects Interact Through Messages 2-16 What Is a Class? 2-17 How Do You Identify a Class? 2-18 Comparing Classes and Objects 2-19 What Is Encapsulation? 2-21 What Is Inheritance? 2-22 Using the Is-a-Kind-of Relationship 2-23 What Is Polymorphism? 2-24 Contents iii
Architecture Rules for Reuse 2-26 Engineering for a Black Box Environment 2-27 Order Entry UML Diagram 2-28 Summary 2-29 Practice 2: Overview 2-30 Order Entry System Partial UML Class Model Basic Java Syntax and Coding Conventions Objectives 3-2 Examining Toolkit Components 3-4 Exploring Packages in J2SE/J2EE 3-5 Documenting Using the J2SE 3-6 Contents of a Java Source 3-7 Establishing Naming Conventions 3-8 More About Naming Conventions 3-10 Defining a Class 3-12 Rental Class: Example 3-13 Creating Code Blocks 3-15 Defining Java Methods 3-16 Examples of a Method 3-17 Declaring Variables 3-18 Examples of Variables in the Context of a Method 3-19 Rules for Creating Statements 3-20 What Are JavaBeans? 3-21 Managing Bean Properties 3-22 Exposing Properties and Methods 3-23 JavaBean Standards at Design Time 3-24 Compiling and Running a Java Application 3-25 The CLASSPATH Variable 3-26 CLASSPATH : Example 3-27 Summary 3-28 Practice 3: Overview Exploring Primitive Data Types and Operators Objectives 4-2 Reserved Keywords 4-4 Variable Types 4-5 Primitive Data Types 4-7 What Are Variables? 4-9 Declaring Variables 4-10 Local Variables 4-11 Defining Variable Names 4-12 What Are Numeric Literals? 4-13 What Are Nonnumeric Literals? 4-15 Guided Practice: Declaring Variables 4-17 What Are Operators? 4-19 iv
Categorizing Operators 4-20 Using the Assignment Operator 4-21 Working with Arithmetic Operators 4-22 More on Arithmetic Operators 4-23 Examining Conversions and Casts 4-24 Incrementing and Decrementing Values 4-26 Relational and Equality Operators 4-27 Using the Conditional Operator ( ?: ) 4-28 Using Logical Operators 4-29 Compound Assignment Operators 4-30 Operator Precedence 4-31 More on Operator Precedence 4-32 Concatenating Strings 4-33 Summary 4-34 Practice 4: Overview Controlling Program Flow Objectives 5-2 Categorizing Basic Flow Control Types 5-4 Using Flow Control in Java 5-6 Using the if Statement 5-7 Nesting if Statements 5-8 Guided Practice: Spot the Mistakes 5-9 Defining the switch Statement 5-10 More About the switch Statement 5-12 Looping in Java 5-13 Using the while Loop 5-14 Using the do…while Loop 5-15 Using the for Loop 5-16 More About the for Loop 5-17 Guided Practice: Spot the Mistakes 5-18 The break Statement 5-19 Summary 5-20 Practice 5: Overview Building Applications with Oracle JDeveloper 10g Objectives 6-2 What Is Oracle JDeveloper 10g? 6-3 Exploring the JDeveloper Environment 6-4 Examining Workspaces 6-5 What Are Projects? 6-7 Creating JDeveloper Items 6-8 Creating an Application Workspace 6-9 Specifying Project Details 6-10 Selecting Additional Libraries 6-11 Adding a New J2SE 6-12 v
Looking at the Directory Structure 6-13 Exploring the Skeleton Java Application 6-14 Finding Methods and Fields 6-15 Supporting Code Development with Profiler and Code Coach 6-16 Customizing JDeveloper 6-17 Using the Help System 6-18 Obtaining Help on a Topic 6-19 Oracle JDeveloper 10g Debugger 6-20 Setting Breakpoints 6-22 Using the Debugger Windows 6-24 Stepping Through a Program 6-25 Watching Data and Variables 6-26 Summary 6-27 Practice 6: Overview Creating Classes and Objects Objectives 7-2 Using Java Classes 7-4 Comparing Classes and Objects 7-5 Creating Objects 7-6 Using the new Operator 7-7 Comparing Primitives and Objects 7-8 Using the null Reference 7-9 Assigning References 7-10 Declaring Instance Variables 7-11 Accessing public Instance Variables 7-12 Defining Methods 7-13 Calling a Method 7-14 Specifying Method Arguments: Examples 7-15 Returning a Value from a Method 7-16 Calling Instance Methods 7-17 Applying Encapsulation in Java 7-18 Passing Primitives into Methods 7-19 Passing Object References into Methods 7-20 What Are Class Variables? 7-21 Initializing Class Variables 7-22 What Are Class Methods? 7-23 Guided Practice: Class Methods or Instance Methods 7-24 Examples in Java 7-25 Creating Classes Using the Class Editor 7-26 What Are Java Packages? 7-27 Grouping Classes in a Package 7-28 Setting the CLASSPATH with Packages 7-29 Access Modifiers 7-30 Summary 7-32 Practice 7: Overview 7-33 vi
8Object Life Cycle and Inner Classes Objectives 8-2 Overloading Methods 8-4 Using the this Reference 8-5 Initializing Instance Variables 8-6 What Are Constructors? 8-7 Defining and Overloading Constructors 8-8 Sharing Code Between Constructors 8-9 final Variables, Methods, and Classes 8-10 Reclaiming Memory 8-11 Using the finalize() Method 8-12 What Are Inner Classes? 8-13 Using Member Inner Class 8-14 Using Local Inner Class 8-15 Defining Anonymous Inner Classes 8-16 Using the Calendar Class 8-17 Summary 8-18 Practice 8: Overview Using Strings, String Buffer, Wrapper, and Text-Formatting Classes Objectives 9-2 What Is a String ? 9-3 Creating a String 9-4 Concatenating Strings 9-5 Performing Operations on Strings 9-6 Performing More Operations on Strings 9-7 Comparing String Objects 9-8 Producing Strings from Other Objects 9-9 Producing Strings from Primitives 9-10 Producing Primitives from Strings 9-11 Wrapper Class Conversion Methods 9-12 Changing the Contents of a String 9-13 Formatting Classes 9-14 Using the SimpleDateFormat Class 9-15 Using the MessageFormat Class 9-16 Using DecimalFormat 9-17 Guided Practice 9-18 Using Regular Expressions 9-20 About System.out.println 9-23 About OutputStream and PrintStream 9-24 What Is Object Serialization? 9-25 Serialization Streams, Interfaces, and Modifiers 9-28 Summary 9-29 Practice 9: Overview 9-30 vii
10Reusing Code with Inheritance and Polymorphism Objectives 10-2 Key Object-Oriented Components 10-3 Example of Inheritance 10-4 Specifying Inheritance in Java 10-5 Defining Inheritance by Using Oracle JDeveloper 10g 10-6 What Does a Subclass Object Look Like? 10-7 Default Initialization 10-8 The super Reference 10-9 The super Reference Example Using Superclass Constructors Specifying Additional Methods Overriding Superclass Methods Invoking Superclass Methods Example of Polymorphism in Java Treating a Subclass as Its Superclass Browsing Superclass References by Using Oracle JDeveloper 10g Acme Video and Polymorphism Using Polymorphism for Acme Video Using the instanceof Operator Limiting Methods and Classes with final Ensuring Genuine Inheritance Summary Practice 10: Overview Using Arrays and Collections Objectives 11-2 What Is an Array? 11-3 Creating an Array of Primitives 11-4 Declaring an Array of Primitives 11-5 Creating an Array Object for an Array of Primitives 11-6 Initializing Array Elements 11-8 Creating an Array of Object References 11-9 Initializing the Objects in the Array Using an Array of Object References Arrays and Exceptions Multidimensional Arrays main() Revisited Working with Variable-Length Structures Modifying a Vector Accessing a Vector Java Collections Framework Collections Framework Components Using ArrayList and Hashtable Using Iterators Summary Practice 11: Overview viii
12Structuring Code Using Abstract Classes and Interfaces Objectives 12-2 Defining Abstract Classes 12-3 Creating Abstract Classes 12-4 What Are Abstract Methods? 12-5 Defining Abstract Methods 12-7 Defining and Using Interfaces 12-8 Examples of Interfaces 12-9 Creating Interfaces Implementing Interfaces Sort: A Real-World Example Overview of the Classes How the Sort Works The Sortable Interface The Sort Class The Movie Class Using the Sort Using instanceof with Interfaces Summary Practice 12: Overview Throwing and Catching Exceptions Objectives 13-2 What Is an Exception? 13-3 How Does Java Handle Exceptions? 13-4 Advantages of Java Exceptions: Separating Error Handling Code 13-5 Advantages of Java Exceptions: Passing Errors Up the Call Stack 13-7 Advantages of Java Exceptions: Exceptions Cannot Be Ignored 13-8 Checked Exceptions, Unchecked Exceptions, and Errors 13-9 What to Do with an Exception Catching and Handling Exceptions Catching a Single Exception Catching Multiple Exceptions Cleaning Up with a finally Block Catching and Handling Exceptions: Guided Practice Allowing an Exception to Pass to the Calling Method Throwing Exceptions Creating Exceptions Catching an Exception and Throwing a Different Exception Summary Practice 13: Overview ix
14User Interface Design: Swing Basics Planning the Application Layout Objectives 14-2 Running Java UI Applications 14-3 AWT, Swing, and JFC 14-4 Swing Features 14-6 Lightweight or Heavyweight Components? 14-8 Planning the UI Layout 14-9 The Containment Hierarchy Top-Level Containers Intermediate Containers Atomic Components Layout Management Overview Border Layout GridBag Layout GridBag Constraints Using Layout Managers Combining Layout Managers Using Frames or Dialogs Using JPanel Containers Adding Borders to Components Using Internal Frames Swing Text Controls Adding Components with Oracle JDeveloper 10g Creating a Frame Adding Components Setting Pluggable Look and Feel Summary Practice 14: Overview Adding User Interface Components and Event Handling Objectives 15-2 Swing Components 15-3 Swing Components in JDeveloper 15-5 Invoking the UI Editor 15-7 How to Add a Component to a Form 15-8 Edit the Properties of a Component 15-9 Code Generated by JDeveloper Creating a Menu Using JDeveloper Menu Editor Practice 15-1: Overview UI for Java Application Java Event Handling Model Event Handling Code Basics Event Handling Process: Registration Event Handling Process: The Event Occurs Event Handling Process: Running the Event Handler x
Using Adapter Classes for Listeners Swing Model View Controller Architecture Basic Text Component Methods Basic JList Component Methods What Events Can a Component Generate? How to Define an Event Handler in JDeveloper Default Event Handling Code Style Generated by JDeveloper Completing the Event Handler Method Summary Practice 15-2: Overview Using JDBC to Access the Database Objectives 16-2 Java, J2EE, and Oracle 10g 16-3 Connecting to a Database with Java 16-4 What Is JDBC? 16-5 Preparing the Environment 16-6 Steps for Using JDBC to Execute SQL Statements 16-8 Step 1: Registering the Driver 16-9 Connecting to the Database Oracle JDBC Drivers: Thin Client Driver Oracle JDBC Drivers: OCI Client Drivers Choosing the Right Driver Step 2: Getting a Database Connection About JDBC URLs JDBC URLs with Oracle Drivers Step 3: Creating a Statement Using the Statement Interface Step 4a: Executing a Query The ResultSet Object Step 4b: Submitting DML Statements Step 4b: Submitting DDL Statements Step 5: Processing the Query Results Step 6: Closing Connections A Basic Query Example Mapping Database Types to Java Types Handling an Unknown SQL Statement Handling Exceptions Managing Transactions The PreparedStatement Object How to Create a PreparedStatement How to Execute a PreparedStatement Maximize Database Access Connection Pooling Summary Practice 16: Overview xi
17Deploying Applications by Using Java Web Start Objectives 17-2 What Is Java Web Start? 17-3 Running a Web Start Application 17-4 Advantages of Web Start 17-5 Examining the JNLP File 17-6 Deploying Applications with JDeveloper 17-7 Creating the Deployment Profile File 17-8 Saving the Deployment Profile 17-9 Selecting Files to Deploy Making an Executable.jar File Creating and Deploying the Archive File Using JDeveloper to Deploy an Application to Java Web Start Step 1: Generate Deployment Profiles and Archive Application Step 2a: Start OC4J Step 2b: Creating a Connection Step 3: Use Web Start Wizard to Create a JNLP File Step 4: Archive and Deploy the Application to the OC4J Server Summary Practice 17: Overview Appendix A: Practice Solutions Appendix B: Java Language Quick-Reference Guide Appendix C: Practice Solutions xii