Oracle 10g: Java Programming Volume 1 Student Guide D17249GC12 Edition 1.2 July 2005 D19367
Author Kate Heap Technical Contributors and Reviewers Ken Cooper Brian Fry Jeff Gallus Glenn Maslen Gayathri Rajagopal Publisher Sujatha Nagendra Copyright 2005, Oracle. All rights reserved. This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle. The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free. If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United States Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS The U.S. Government s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract. Oracle, JD Edwards, PeopleSoft, and Retek are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Contents Preface I Introduction Objectives I-2 Course Overview I-3 1 Introducing 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 1-25 2 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 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 2-33 3 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 3-29 4 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 4-35 5 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 5-21 6 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 6-28 7 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
8 Object 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 8-19 9 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
10 Reusing 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 10-10 Using Superclass Constructors 10-11 Specifying Additional Methods 10-13 Overriding Superclass Methods 10-15 Invoking Superclass Methods 10-17 Example of Polymorphism in Java 10-19 Treating a Subclass as Its Superclass 10-20 Browsing Superclass References by Using Oracle JDeveloper 10g 10-21 Acme Video and Polymorphism 10-22 Using Polymorphism for Acme Video 10-23 Using the instanceof Operator 10-25 Limiting Methods and Classes with final 10-26 Ensuring Genuine Inheritance 10-27 Summary 10-28 Practice 10: Overview 10-29 11 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 11-10 Using an Array of Object References 11-11 Arrays and Exceptions 11-12 Multidimensional Arrays 11-13 main() Revisited 11-14 Working with Variable-Length Structures 11-15 Modifying a Vector 11-16 Accessing a Vector 11-17 Java Collections Framework 11-18 Collections Framework Components 11-20 Using ArrayList and Hashtable 11-21 Using Iterators 11-22 Summary 11-23 Practice 11: Overview 11-24 viii
12 Structuring 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 12-10 Implementing Interfaces 12-12 Sort: A Real-World Example 12-13 Overview of the Classes 12-14 How the Sort Works 12-15 The Sortable Interface 12-16 The Sort Class 12-17 The Movie Class 12-18 Using the Sort 12-19 Using instanceof with Interfaces 12-20 Summary 12-21 Practice 12: Overview 12-22 13 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 13-11 Catching and Handling Exceptions 13-12 Catching a Single Exception 13-13 Catching Multiple Exceptions 13-14 Cleaning Up with a finally Block 13-15 Catching and Handling Exceptions: Guided Practice 13-16 Allowing an Exception to Pass to the Calling Method 13-18 Throwing Exceptions 13-19 Creating Exceptions 13-20 Catching an Exception and Throwing a Different Exception 13-21 Summary 13-22 Practice 13: Overview 13-23 ix
14 User 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 14-10 Top-Level Containers 14-12 Intermediate Containers 14-14 Atomic Components 14-15 Layout Management Overview 14-16 Border Layout 14-18 GridBag Layout 14-19 GridBag Constraints 14-20 Using Layout Managers 14-22 Combining Layout Managers 14-24 Using Frames or Dialogs 14-25 Using JPanel Containers 14-27 Adding Borders to Components 14-29 Using Internal Frames 14-30 Swing Text Controls 14-32 Adding Components with Oracle JDeveloper 10g 14-33 Creating a Frame 14-34 Adding Components 14-35 Setting Pluggable Look and Feel 14-37 Summary 14-39 Practice 14: Overview 14-40 15 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 15-10 Creating a Menu 15-12 Using JDeveloper Menu Editor 15-13 Practice 15-1: Overview 15-14 UI for Java Application 15-15 Java Event Handling Model 15-20 Event Handling Code Basics 15-21 Event Handling Process: Registration 15-22 Event Handling Process: The Event Occurs 15-24 Event Handling Process: Running the Event Handler 15-25 x
Using Adapter Classes for Listeners 15-26 Swing Model View Controller Architecture 15-27 Basic Text Component Methods 15-30 Basic JList Component Methods 15-31 What Events Can a Component Generate? 15-32 How to Define an Event Handler in JDeveloper 15-33 Default Event Handling Code Style Generated by JDeveloper 15-34 Completing the Event Handler Method 15-35 Summary 15-36 Practice 15-2: Overview 15-37 16 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 16-10 Oracle JDBC Drivers: Thin Client Driver 16-11 Oracle JDBC Drivers: OCI Client Drivers 16-12 Choosing the Right Driver 16-13 Step 2: Getting a Database Connection 16-14 About JDBC URLs 16-15 JDBC URLs with Oracle Drivers 16-16 Step 3: Creating a Statement 16-17 Using the Statement Interface 16-18 Step 4a: Executing a Query 16-19 The ResultSet Object 16-20 Step 4b: Submitting DML Statements 16-21 Step 4b: Submitting DDL Statements 16-22 Step 5: Processing the Query Results 16-23 Step 6: Closing Connections 16-24 A Basic Query Example 16-25 Mapping Database Types to Java Types 16-26 Handling an Unknown SQL Statement 16-28 Handling Exceptions 16-29 Managing Transactions 16-30 The PreparedStatement Object 16-31 How to Create a PreparedStatement 16-32 How to Execute a PreparedStatement 16-33 Maximize Database Access 16-34 Connection Pooling 16-35 Summary 16-38 Practice 16: Overview 16-39 xi
17 Deploying 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 17-10 Making an Executable.jar File 17-11 Creating and Deploying the Archive File 17-12 Using JDeveloper to Deploy an Application to Java Web Start 17-13 Step 1: Generate Deployment Profiles and Archive Application 17-14 Step 2a: Start OC4J 17-15 Step 2b: Creating a Connection 17-16 Step 3: Use Web Start Wizard to Create a JNLP File 17-17 Step 4: Archive and Deploy the Application to the OC4J Server 17-18 Summary 17-19 Practice 17: Overview 17-20 Appendix A: Practice Solutions Appendix B: Java Language Quick-Reference Guide Appendix C: Practice Solutions xii