Java programming language Java SE 6 self-study course

fundamentals of the java programming language java se 6 self-study course pdf free download
GraceRogers Profile Pic
GraceRogers,Greece,Professional
Published Date:12-07-2017
Your Website URL(Optional)
Comment
Java Programming Language, Java SE 6 Electronic Presentation SL-275-SE6 REV G.2 D61748GC11 Edition 1.1Copyright © 2008, 2010, Oracle and/or its affiliates. All rights reserved. Disclaimer This document contains proprietary information, is provided under a license agreement containing restrictions on use and disclosure, 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 as expressly permitted in your license agreement or allowed by 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. Sun Microsystems, Inc. Disclaimer This training manual may include references to materials, offerings, or products that were previously offered by Sun Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided. Oracle and its affiliates cannot be held responsible for any such references should they appear in the text provided. Restricted Rights Notice If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S. 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. Trademark Notice Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark licensed through X/Open Company, Ltd. This page intentionally left blank Course Contents About This Course ..................................................................................... Preface-xvi Course Goals .............................................................................................................................Preface-xvii Course Overview ......................................................................................................................Preface-xix Course Map ................................................................................................................................. Preface-xx Topics Not Covered ..................................................................................................................Preface-xxi How Prepared Are You? .........................................................................................................Preface-xxii Introductions ...........................................................................................................................Preface-xxiii How to Use the Icons ..............................................................................................................Preface-xxiv Typographical Conventions and Symbols ...........................................................................Preface-xxv Getting Started ......................................................................................................... 1-1 Objectives ................................................................................................................................................. 1-2 Relevance .................................................................................................................................................. 1-3 What Is the Java™ Technology? ........................................................................................................... 1-4 Primary Goals of the Java Technology ................................................................................................ 1-5 The Java Virtual Machine ...................................................................................................................... 1-8 Garbage Collection ............................................................................................................................... 1-11 The Java Runtime Environment .......................................................................................................... 1-12 Operation of the JRE With a Just-In-Time (JIT) Compiler .............................................................. 1-13 JVM™ Tasks ........................................................................................................................................... 1-14 The Class Loader ................................................................................................................................... 1-15 The Bytecode Verifier ........................................................................................................................... 1-16 A Simple Java Application ................................................................................................................... 1-17 TheTestGreeting Application ......................................................................................................... 1-18 TheGreeting Class .............................................................................................................................. 1-19 Compiling and Running theTestGreeting Program .................................................................... 1-20 Java™ Programming Language iv Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Compile-Time Errors ............................................................................................................................ 1-21 Runtime Errors ...................................................................................................................................... 1-22 Java Technology Runtime Environment ........................................................................................... 1-23 Object-Oriented Programming ................................................................................ 2-1 Objectives ................................................................................................................................................. 2-2 Relevance .................................................................................................................................................. 2-3 Software Engineering ............................................................................................................................. 2-4 The Analysis and Design Phase ............................................................................................................ 2-5 Abstraction ............................................................................................................................................... 2-6 Classes as Blueprints for Objects .......................................................................................................... 2-7 Declaring Java Technology Classes ...................................................................................................... 2-8 Declaring Attributes ............................................................................................................................... 2-9 Declaring Methods ................................................................................................................................ 2-10 Accessing Object Members .................................................................................................................. 2-11 Information Hiding ............................................................................................................................... 2-12 Encapsulation ........................................................................................................................................ 2-14 Declaring Constructors ........................................................................................................................ 2-15 The Default Constructor ...................................................................................................................... 2-16 Source File Layout ................................................................................................................................ 2-17 Software Packages ................................................................................................................................ 2-18 Thepackage Statement ........................................................................................................................ 2-19 Theimport Statement .......................................................................................................................... 2-20 Directory Layout and Packages .......................................................................................................... 2-21 Development ......................................................................................................................................... 2-22 Compiling Using the-d Option ......................................................................................................... 2-23 Terminology Recap ............................................................................................................................... 2-24 Using the Java Technology API Documentation .............................................................................. 2-25 Java Technology API Documentation ............................................................................................... 2-26 Java™ Programming Language v Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Identifiers, Keywords, and Types ........................................................................... 3-1 Objectives ................................................................................................................................................. 3-2 Relevance .................................................................................................................................................. 3-4 Comments ................................................................................................................................................ 3-5 Semicolons, Blocks, and White Space .................................................................................................. 3-6 Identifiers ................................................................................................................................................. 3-9 Java Programming Language Keywords .......................................................................................... 3-10 Primitive Types ..................................................................................................................................... 3-11 Logical –boolean ................................................................................................................................. 3-12 Textual –char ....................................................................................................................................... 3-13 Textual –String ................................................................................................................................... 3-14 Integral –byte,short,int, andlong ............................................................................................... 3-15 Floating Point –float anddouble .................................................................................................... 3-17 Variables, Declarations, and Assignments ........................................................................................ 3-19 Java Reference Types ............................................................................................................................ 3-20 Constructing and Initializing Objects ................................................................................................ 3-21 Memory Allocation and Layout .......................................................................................................... 3-22 Explicit Attribute Initialization ........................................................................................................... 3-23 Executing the Constructor ................................................................................................................... 3-24 Assigning a Variable ............................................................................................................................. 3-25 Assigning References ............................................................................................................................ 3-26 Pass-by-Value ........................................................................................................................................ 3-27 Thethis Reference ............................................................................................................................... 3-32 Java Programming Language Coding Conventions ........................................................................ 3-36 Expressions and Flow Control ............................................................................... 4-1 Objectives ................................................................................................................................................. 4-2 Relevance .................................................................................................................................................. 4-4 Variables and Scope ................................................................................................................................ 4-5 Variable Scope Example ......................................................................................................................... 4-6 Java™ Programming Language vi Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Variable Initialization ............................................................................................................................. 4-7 Initialization Before Use Principle ........................................................................................................ 4-8 Operator Precedence .............................................................................................................................. 4-9 Logical Operators .................................................................................................................................. 4-10 Bitwise Logical Operators .................................................................................................................... 4-11 Right-Shift Operators and ....................................................................................................... 4-12 Left-Shift Operator .......................................................................................................................... 4-13 Shift Operator Examples ...................................................................................................................... 4-14 String Concatenation With+ ............................................................................................................... 4-15 Casting .................................................................................................................................................... 4-16 Promotion and Casting of Expressions .............................................................................................. 4-17 Simpleif, else Statements .............................................................................................................. 4-18 Complexif, else Statements .......................................................................................................... 4-19 Switch Statements ................................................................................................................................. 4-21 Looping Statements .............................................................................................................................. 4-24 Special Loop Flow Control .................................................................................................................. 4-27 Thebreak Statement ............................................................................................................................ 4-28 Thecontinue Statement ..................................................................................................................... 4-29 Usingbreak Statements with Labels ................................................................................................. 4-30 Usingcontinue Statements with Labels .......................................................................................... 4-31 Arrays ........................................................................................................................ 5-1 Objectives ................................................................................................................................................. 5-2 Relevance .................................................................................................................................................. 5-3 Declaring Arrays ..................................................................................................................................... 5-4 Creating Arrays ....................................................................................................................................... 5-5 Creating Reference Arrays ..................................................................................................................... 5-7 Initializing Arrays ................................................................................................................................... 5-9 Multidimensional Arrays ..................................................................................................................... 5-10 Array Bounds ........................................................................................................................................ 5-12 Java™ Programming Language vii Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Using the Enhancedfor Loop ............................................................................................................ 5-13 Array Resizing ....................................................................................................................................... 5-14 Copying Arrays ..................................................................................................................................... 5-15 Class Design ............................................................................................................. 6-1 Objectives ................................................................................................................................................. 6-2 Relevance .................................................................................................................................................. 6-3 Subclassing ............................................................................................................................................... 6-4 Single Inheritance .................................................................................................................................... 6-7 Access Control ......................................................................................................................................... 6-9 Overriding Methods ............................................................................................................................. 6-10 Overridden Methods Cannot Be Less Accessible ............................................................................. 6-12 Invoking Overridden Methods ........................................................................................................... 6-13 Polymorphism ....................................................................................................................................... 6-15 Virtual Method Invocation .................................................................................................................. 6-17 Heterogeneous Collections .................................................................................................................. 6-18 Polymorphic Arguments ..................................................................................................................... 6-19 Theinstanceof Operator ................................................................................................................... 6-20 Casting Objects ...................................................................................................................................... 6-21 Overloading Methods .......................................................................................................................... 6-23 Methods Using Variable Arguments ................................................................................................. 6-24 Overloading Constructors ................................................................................................................... 6-25 Constructors Are Not Inherited .......................................................................................................... 6-27 Invoking Parent Class Constructors ................................................................................................... 6-28 Constructing and Initializing Objects: A Slight Reprise .................................................................. 6-30 Constructor and Initialization Examples ........................................................................................... 6-31 TheObject Class .................................................................................................................................. 6-34 Theequals Method .............................................................................................................................. 6-35 Anequals Example ............................................................................................................................. 6-36 ThetoString Method ......................................................................................................................... 6-40 Java™ Programming Language viii Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Wrapper Classes .................................................................................................................................... 6-41 Autoboxing of Primitive Types ........................................................................................................... 6-43 Advanced Class Features ....................................................................................... 7-1 Objectives ................................................................................................................................................. 7-2 Relevance .................................................................................................................................................. 7-3 Thestatic Keyword ............................................................................................................................. 7-4 Class Attributes ....................................................................................................................................... 7-5 Class Methods ......................................................................................................................................... 7-7 Static Initializers .................................................................................................................................... 7-10 Thefinal Keyword ............................................................................................................................. 7-12 Final Variables ....................................................................................................................................... 7-13 Blank Final Variables ............................................................................................................................ 7-14 Old-Style Enumerated Type Idiom .................................................................................................... 7-15 The New Enumerated Type ................................................................................................................ 7-19 Advanced Enumerated Types ............................................................................................................. 7-23 Static Imports ......................................................................................................................................... 7-25 Abstract Classes ..................................................................................................................................... 7-27 The Solution ........................................................................................................................................... 7-31 Interfaces ................................................................................................................................................ 7-34 The Flyer Example ................................................................................................................................ 7-35 Multiple Interface Example ................................................................................................................. 7-42 Uses of Interfaces .................................................................................................................................. 7-44 Exceptions and Assertions ..................................................................................... 8-1 Objectives ................................................................................................................................................. 8-2 Relevance .................................................................................................................................................. 8-3 Exceptions and Assertions ..................................................................................................................... 8-4 Exceptions ................................................................................................................................................ 8-5 Exception Example ................................................................................................................................. 8-6 Java™ Programming Language ix Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Thetry-catch Statement ..................................................................................................................... 8-7 Call Stack Mechanism .......................................................................................................................... 8-10 Thefinally Clause ............................................................................................................................. 8-11 Exception Categories ............................................................................................................................ 8-12 Common Exceptions ............................................................................................................................. 8-13 The Handle or Declare Rule ................................................................................................................ 8-14 Method Overriding and Exceptions ................................................................................................... 8-15 Creating Your Own Exceptions .......................................................................................................... 8-17 Handling a User-Defined Exception .................................................................................................. 8-18 Assertions ............................................................................................................................................... 8-20 Recommended Uses of Assertions ..................................................................................................... 8-21 Internal Invariants ................................................................................................................................. 8-22 Control Flow Invariants ....................................................................................................................... 8-23 Postconditions and Class Invariants .................................................................................................. 8-24 Controlling Runtime Evaluation of Assertions ................................................................................ 8-25 Collections and Generics Framework .................................................................... 9-1 Objectives ................................................................................................................................................. 9-2 The Collections API ................................................................................................................................ 9-3 AList Example ...................................................................................................................................... 9-7 TheMap Interface ..................................................................................................................................... 9-8 TheMap Interface API ............................................................................................................................. 9-9 AMap Example ...................................................................................................................................... 9-10 Legacy Collection Classes .................................................................................................................... 9-12 Ordering Collections ............................................................................................................................ 9-13 TheComparable Interface ................................................................................................................... 9-14 Example of theComparable Interface ............................................................................................... 9-16 TheComparator Interface ................................................................................................................... 9-20 Example of theComparator Interface ............................................................................................... 9-21 Generics .................................................................................................................................................. 9-25 Java™ Programming Language x Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services GenericSet Example ............................................................................................................................ 9-27 Generic Map Example .......................................................................................................................... 9-28 Generics: Examining Type Parameters .............................................................................................. 9-29 Wild Card Type Parameters ................................................................................................................ 9-31 The Type-Safety Guarantee ................................................................................................................. 9-32 The Invariance Challenge .................................................................................................................... 9-33 The Covariance Response .................................................................................................................... 9-34 Generics: Refactoring Existing Non-Generic Code .......................................................................... 9-35 Iterators ................................................................................................................................................... 9-36 Generic Iterator Interfaces ................................................................................................................... 9-37 The Enhancedfor Loop ...................................................................................................................... 9-38 I/O Fundamentals ................................................................................................... 10-1 Objectives ............................................................................................................................................... 10-2 Command-Line Arguments ................................................................................................................ 10-3 System Properties .................................................................................................................................. 10-5 TheProperties Class ......................................................................................................................... 10-6 I/O Stream Fundamentals ................................................................................................................... 10-9 Fundamental Stream Classes ............................................................................................................ 10-10 Data Within Streams ........................................................................................................................... 10-11 TheInputStream Methods ............................................................................................................... 10-12 TheOutputStream Methods ............................................................................................................. 10-13 TheReader Methods .......................................................................................................................... 10-14 TheWriter Methods .......................................................................................................................... 10-15 Node Streams ....................................................................................................................................... 10-16 A Simple Example ............................................................................................................................... 10-17 Buffered Streams ................................................................................................................................. 10-19 I/O Stream Chaining .......................................................................................................................... 10-21 Processing Streams ............................................................................................................................. 10-22 TheInputStream Class Hierarchy .................................................................................................. 10-24 Java™ Programming Language xi Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services TheOutputStream Class Hierarchy ................................................................................................ 10-25 TheObjectInputStream and TheObjectOutputStream Classes ............................................ 10-26 TheSerializeDate Class ................................................................................................................. 10-30 TheDeSerializeDate Class ............................................................................................................ 10-32 TheReader Class Hierarchy ............................................................................................................. 10-34 TheWriter Class Hierarchy ............................................................................................................. 10-35 Console I/O and File I/O ......................................................................................... 11-1 Objectives ............................................................................................................................................... 11-2 Console I/O ........................................................................................................................................... 11-3 Writing to Standard Output ................................................................................................................ 11-4 Reading From Standard Input ............................................................................................................ 11-5 Simple Formatted Output .................................................................................................................... 11-7 Simple Formatted Input ....................................................................................................................... 11-8 Files and File I/O .................................................................................................................................. 11-9 Creating a NewFile Object ............................................................................................................. 11-10 TheFile Tests and Utilities .............................................................................................................. 11-11 File Stream I/O .................................................................................................................................... 11-13 File Input Example .............................................................................................................................. 11-14 Printing a File ....................................................................................................................................... 11-15 File Output Example ........................................................................................................................... 11-16 Building Java GUIs Using the Swing API ............................................................ 12-1 Objectives ............................................................................................................................................... 12-2 What Are the Java Foundation Classes (JFC)? .................................................................................. 12-3 What Is Swing? ...................................................................................................................................... 12-4 Swing Architecture ............................................................................................................................... 12-5 Swing Packages ..................................................................................................................................... 12-6 Examining the Composition of a Java Technology GUI ................................................................. 12-7 Swing Containers .................................................................................................................................. 12-9 Java™ Programming Language xii Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Top-Level Containers ......................................................................................................................... 12-10 Swing Components ............................................................................................................................. 12-11 Swing Component Hierarchy ........................................................................................................... 12-12 Text Components ................................................................................................................................ 12-13 Swing Component Properties ........................................................................................................... 12-14 Common Component Properties ...................................................................................................... 12-15 Component-Specific Properties ........................................................................................................ 12-16 Layout Managers ................................................................................................................................ 12-17 TheBorderLayout Manager ........................................................................................................... 12-18 BorderLayout Example ..................................................................................................................... 12-19 TheFlowLayout Manager ................................................................................................................ 12-21 FlowLayout Example ......................................................................................................................... 12-22 TheBoxLayout Manager ................................................................................................................... 12-24 TheCardLayout Manager ................................................................................................................. 12-25 GridLayout Example ......................................................................................................................... 12-27 TheGridBagLayout Manager .......................................................................................................... 12-29 GUI Construction ................................................................................................................................ 12-30 Programmatic Construction .............................................................................................................. 12-31 Key Methods ........................................................................................................................................ 12-34 Handling GUI-Generated Events ........................................................................... 13-1 Objectives ............................................................................................................................................... 13-2 What Is an Event? ................................................................................................................................. 13-3 Delegation Model .................................................................................................................................. 13-4 A Listener Example ............................................................................................................................... 13-6 Event Categories ................................................................................................................................... 13-8 Method Categories and Interfaces ...................................................................................................... 13-9 Complex Example ............................................................................................................................... 13-13 Multiple Listeners ............................................................................................................................... 13-17 Event Adapters .................................................................................................................................... 13-18 Java™ Programming Language xiii Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Event Handling Using Inner Classes ............................................................................................... 13-19 Event Handling Using Anonymous Classes ................................................................................... 13-21 GUI-Based Applications ........................................................................................ 14-1 Objectives ............................................................................................................................................... 14-2 Relevance ................................................................................................................................................ 14-3 How to Create a Menu ......................................................................................................................... 14-4 Creating aJMenuBar ............................................................................................................................. 14-5 Creating aJMenu ................................................................................................................................... 14-6 Creating aJMenuItem .......................................................................................................................... 14-8 Creating aJCheckBoxMenuItem ....................................................................................................... 14-10 Controlling Visual Aspects ................................................................................................................ 14-12 Threads ................................................................................................................... 15-1 Objectives ............................................................................................................................................... 15-2 Relevance ................................................................................................................................................ 15-3 Threads ................................................................................................................................................... 15-4 Creating the Thread .............................................................................................................................. 15-5 Starting the Thread ............................................................................................................................... 15-7 Thread Scheduling ................................................................................................................................ 15-8 Thread Scheduling Example ............................................................................................................... 15-9 Terminating a Thread ......................................................................................................................... 15-10 Basic Control of Threads .................................................................................................................... 15-12 Thejoin Method ................................................................................................................................ 15-13 Other Ways to Create Threads .......................................................................................................... 15-14 Selecting a Way to Create Threads ................................................................................................... 15-15 Using thesynchronized Keyword ................................................................................................. 15-16 The Object Lock Flag .......................................................................................................................... 15-17 Releasing the Lock Flag ...................................................................................................................... 15-20 Usingsynchronized – Putting It Together .................................................................................... 15-21 Java™ Programming Language xiv Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Thread State Diagram With Synchronization ................................................................................. 15-23 Deadlock ............................................................................................................................................... 15-25 Thread Interaction –wait andnotify ............................................................................................ 15-26 Thread Interaction ............................................................................................................................... 15-27 Thread State Diagram Withwait andnotify .............................................................................. 15-28 Monitor Model for Synchronization ................................................................................................ 15-30 TheProducer Class ............................................................................................................................ 15-31 TheConsumer Class ............................................................................................................................ 15-33 TheSyncStack Class .......................................................................................................................... 15-35 Thepop Method .................................................................................................................................. 15-36 Thepush Method ................................................................................................................................ 15-37 TheSyncTest Class ............................................................................................................................ 15-38 TheSyncTest Class ............................................................................................................................ 15-39 Networking .............................................................................................................. 16-1 Objectives ............................................................................................................................................... 16-2 Relevance ................................................................................................................................................ 16-3 Networking ............................................................................................................................................ 16-4 Networking With Java Technology .................................................................................................... 16-6 Java Networking Model ....................................................................................................................... 16-7 Minimal TCP/IP Server ....................................................................................................................... 16-8 Minimal TCP/IP Client ...................................................................................................................... 16-11 Java™ Programming Language xv Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Preface About This Course Java™ Programming LanguageSun Educational Services Course Goals This course provides you with knowledge and skills to: • Create Java™ technology applications that leverage the object-oriented features of the Java language, such as encapsulation, inheritance, and polymorphism • Execute a Java technology application from the command-line • Use Java technology data types and expressions • Use Java technology flow control constructs • Use arrays and other data collections • Implement error-handling techniques using exception handling Java™ Programming Language Preface, slide xvii of xxvii Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Course Goals • Create an event-driven graphical user interface (GUI) by using Java technology GUI components: panels, buttons, labels, text fields, and text areas • Implement input/output (I/O) functionality to read from and write to data and text files • Create multithreaded programs • Create a simple Transmission Control Protocol/ Internet Protocol (TCP/IP) client that communicates through sockets Java™ Programming Language Preface, slide xviii of xxvii Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Course Overview This course describes the following areas: • The syntax of the Java programming language • Object-oriented concepts as they apply to the Java programming language • GUI programming • Multithreading • Networking Java™ Programming Language Preface, slide xix of xxvii Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2Sun Educational Services Course Map The Java Programming Language Basics Identifiers, Object-Oriented Keywords, and Getting Started Programming Types Expressions Arrays and Flow Control More Object-Oriented Programming Advanced Class Design Class Features Exceptions, Collections, and I/O Exceptions Collections and I/O Generics Framework and Assertions Fundamentals Developing Graphical User Interfaces Console I/O and GUI Event GUI-Based File I/O Handling Applications Advanced Java Programming Threads Networking Java™ Programming Language About This Course, xx of xxvii Copyright Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G.2

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.