1. Trang chủ
  2. » Ngoại Ngữ

Oracle Fusion Middleware 11g Java Programming Student Guide Volume II

348 413 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 348
Dung lượng 2,51 MB

Nội dung

Volume II • Student Guide D53983GC11 Edition 1.1 Oracle University and Global Business Solutions Peru SAC use only Oracle Fusion Middleware 11g: Java Programming May 2009 D60391 THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Author Copyright © 2009, Oracle All rights reserved Kate Heap Disclaimer Ken Cooper Clay Fuller Taj Islam Peter Laseau Yvonne Price 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 Editors Restricted Rights Notice Daniel Milne 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: Joyce Raftery Graphic Designer Satish Bettegowda 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 Publishers Trademark Notice Pavithran Adka Oracle is a registered trademark of Oracle Corporation and/or its affiliates Other names may be trademarks of their respective owners Nita Brozowski Oracle University and Global Business Solutions Peru SAC use only Technical Contributors and Reviewers THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED I Introduction Objectives I-2 Course Overview I-3 Introducing the Java and Oracle Platforms Objectives 1-2 What Is Java? 1-3 Key Benefits of Java 1-5 Object-Oriented Approach 1-7 Design Patterns 1-8 The MVC Design Pattern 1-9 Platform Independence 1-10 Using Java with Enterprise Internet Computing 1-11 Using the Java Virtual Machine 1-13 How Does the JVM Work? 1-15 Benefits of JIT Compilers 1-17 Implementing Security in the Java Environment 1-19 Deployment of Java Applications 1-21 Using Java with Oracle 11g 1-22 Java Software Development Kit 1-23 Using the Appropriate Development Kit 1-24 Java SE 1-25 Integrated Development Environment 1-26 Summary 1-27 Basic Java Syntax and Coding Conventions Objectives 2-2 Toolkit Components 2-4 Java Packages 2-5 Documenting Using Java SE 2-6 Contents of a Java Source File 2-8 Naming Conventions 2-9 More About Naming Conventions 2-11 Defining a Class 2-13 Rental Class: Example 2-14 Creating Code Blocks 2-16 Oracle University and Global Business Solutions Peru SAC use only Contents iii THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Classpath Use Examples 2-25 Summary 2-26 Practice Overview: Basic Java Syntax and Coding Conventions 2-27 UML Diagram for OrderEntry 2-28 Exploring Primitive Data Types and Operators Objectives 3-2 Keywords and Reserved Words 3-3 Variable Types 3-4 Primitive Data Types 3-5 Variables 3-7 Declaring Variables 3-8 Local Variables 3-9 Defining Variable Names 3-10 Numeric Literals 3-11 Nonnumeric Literals 3-13 Operators 3-15 Categories of Operators 3-16 Using the Assignment Operator 3-17 Arithmetic Operators 3-18 More on Arithmetic Operators 3-19 Guided Practice: Declaring Variables 3-20 Examining Conversions and Casts 3-22 Incrementing and Decrementing Values 3-24 Relational and Equality Operators 3-25 Conditional Operator (?:) 3-26 Logical Operators 3-27 Compound Assignment Operators 3-28 Operator Precedence 3-29 More on Operator Precedence 3-30 Concatenating Strings 3-31 Summary 3-32 Practice Overview: Exploring Primitive Data Types and Operators 3-33 Oracle University and Global Business Solutions Peru SAC use only Defining Java Methods 2-17 Example of a Method 2-18 Declaring Variables 2-19 Examples of Variables in the Context of a Method 2-20 Rules for Creating Statements 2-21 Compiling and Running a Java Application 2-22 Debugging a Java Program 2-23 CLASSPATH Variable 2-24 iv THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Controlling Program Flow Objectives 4-2 Basic Flow Control Types 4-3 Using Flow Control in Java 4-4 if Statement 4-5 Nested if Statements 4-6 Guided Practice: Spot the Mistakes 4-7 switch Statement 4-8 More About the switch Statement 4-9 Looping in Java 4-10 while Loop 4-12 do…while Loop 4-13 for Loop 4-14 More About the for Loop 4-15 Guided Practice: Spot the Mistakes 4-16 break Statement 4-17 continue Statement 4-18 Summary 4-19 Practice Overview: Controlling Program Flow 4-20 Building Applications with Oracle JDeveloper (11g) Objectives 5-2 Oracle JDeveloper (11g) 5-3 Oracle JDeveloper (11g) Environment 5-4 Application Navigator 5-6 Projects 5-8 Creating JDeveloper Items 5-9 Creating an Application 5-10 Project Properties: Specifying Project Details 5-12 Project Properties: Selecting Additional Libraries 5-13 Adding a New Java SE 5-14 Directory Structure 5-15 Exploring the Skeleton Java Application 5-16 Finding Methods and Fields 5-17 Supporting Code Development with Profiler and Code Coach 5-18 Code Editor Features 5-19 Refactoring 5-21 Using Javadoc 5-24 JDeveloper Help System 5-25 Oracle University and Global Business Solutions Peru SAC use only v THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Creating Classes and Objects Objectives 6-2 Object-Oriented Programming 6-3 Classes and Objects 6-5 Classes Versus Objects 6-6 Objects Are Modeled as Abstractions 6-7 Encapsulation 6-8 Inheritance 6-9 Polymorphism 6-10 Guided Practice: Spot the Operations and Attributes 6-11 Java Classes 6-12 Comparing Classes and Objects 6-13 Creating Objects 6-14 new Operator 6-15 Primitive Variables and Object Variables 6-16 null Reference 6-17 Assigning References 6-18 Declaring Instance Variables 6-19 Accessing public Instance Variables 6-20 Defining Methods 6-21 Calling a Method 6-22 Specifying Method Arguments: Examples 6-23 Returning a Value from a Method 6-25 Calling Instance Methods 6-26 Encapsulation in Java 6-27 Passing Primitives to Methods 6-28 Passing Object References to Methods 6-29 Java Packages 6-30 Grouping Classes in a Package 6-31 Setting the CLASSPATH with Packages 6-32 Access Modifiers 6-34 Practice Overview: Creating Classes and Objects 6-36 Oracle University and Global Business Solutions Peru SAC use only Obtaining Help on a Topic 5-26 Oracle JDeveloper Debugger 5-27 Breakpoints 5-29 Debugger Windows 5-31 Stepping Through a Program 5-32 Watching Data and Variables 5-33 Summary 5-34 Practice Overview: Building Java with Oracle JDeveloper 11g 5-35 vi THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Object Life Cycle and Inner Classes Objectives 7-2 Overloading Methods 7-3 Using the this Reference 7-4 Initializing Instance Variables 7-5 Class Variables 7-6 Initializing Class Variables 7-7 Class Methods 7-8 Guided Practice: Class Methods or Instance Methods 7-9 Examples of Static Methods in Java 7-10 Constructors 7-11 Defining and Overloading Constructors 7-12 Sharing Code Between Constructors 7-13 final Variables, Methods, and Classes 7-14 Reclaiming Memory 7-15 finalize()Method 7-16 Inner Classes 7-18 Anonymous Inner Classes 7-20 Calendar Class 7-22 Performing Calculations with the Calendar Class 7-23 Summary 7-26 Practice Overview: Object Life Cycle Classes 7-27 Using Strings Objectives 8-2 Strings in Java 8-3 Creating Strings 8-4 Concatenating Strings 8-5 Performing Operations on Strings 8-6 Performing More Operations on Strings 8-7 Comparing String Objects 8-8 Producing Strings from Other Objects 8-10 Producing Strings from Primitives 8-11 Producing Primitives from Strings 8-12 Oracle University and Global Business Solutions Peru SAC use only JavaBeans 6-37 More About JavaBeans 6-38 Managing Bean Properties 6-39 Exposing Properties and Methods 6-40 Building and Using a JavaBean in JDeveloper 6-41 Summary 6-42 vii THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Using DecimalFormat for Localization 8-21 Guided Practice 8-23 A Regular Expression 8-25 Matching Strings 8-26 Replacing and Splitting Strings 8-27 Pattern Matching 8-28 Regular Expression Syntax 8-29 Steps Involved in Matching 8-31 Guided Practice 8-33 Summary 8-35 Practice Overview: Using Strings and the StringBuffer, Wrapper, and Text- Formatting Classes 8-36 Using Streams for I/O Objectives 9-2 Streams 9-3 Sets of I/O Classes 9-4 How to Do I/O 9-5 Why Java I/O Is Hard 9-6 Byte I/O Streams 9-7 InputStream 9-9 OutputStream 9-10 Using Byte Streams 9-11 Character I/O Streams 9-13 Using Character Streams 9-15 The InputStreamReader Class 9-17 The OutputStreamWriter Class 9-18 The Basics: Standard Output 9-19 PrintStream and PrintWriter 9-20 Formatted Output 9-22 Format Specifiers 9-23 Guided Practice 9-25 The Basics: Standard Input 9-26 Scanner API 9-28 Remote I/O 9-29 Oracle University and Global Business Solutions Peru SAC use only Wrapper Class Conversion Methods 8-13 Changing the Contents of a String 8-14 Formatting Classes 8-16 Formatting Dates 8-17 DecimalFormat Subclass 8-19 viii THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED 10 Inheritance and Polymorphism Objectives 10-2 Key Object-Oriented Components 10-3 Example of Inheritance 10-5 Specifying Inheritance in Java 10-6 Defining Inheritance with Oracle JDeveloper 10-8 Subclass and Superclass Variables 10-9 Default Initialization 10-10 Super() Reference 10-11 Super() Reference: Example 10-12 Using Superclass Constructors 10-13 Specifying Additional Methods 10-15 Overriding Superclass Methods 10-17 Invoking Superclass Methods 10-19 Example of Polymorphism in Java 10-20 Treating a Subclass as Its Superclass 10-21 Browsing Superclass References with Oracle JDeveloper 10-22 Hierarchy Browser 10-23 Acme Video and Polymorphism 10-24 Using Polymorphism for Acme Video 10-25 instanceof Operator 10-27 Limiting Methods and Classes with final 10-29 Ensuring Genuine Inheritance 10-31 Summary 10-32 Practice 10 Overview: Inheritance and Polymorphism 10-33 Oracle University and Global Business Solutions Peru SAC use only Data Streams 9-31 Object Streams 9-32 Object Serialization 9-33 Serialization Streams, Interfaces, and Modifiers 9-36 IOException Class 9-37 Summary 9-39 Practice Overview: Using Streams for I/O 9-40 11 Arrays and Collections Objectives 11-2 Arrays 11-3 Creating an Array of Primitives 11-4 Declaring an Array 11-5 Creating an Array Object 11-6 Initializing Array Elements 11-8 ix THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Iterator Interface 11-20 Sets 11-22 HashSet 11-23 LinkedHashSet 11-25 TreeSet 11-27 Lists 11-29 ArrayList 11-30 Modifying an ArrayList 11-31 Accessing an ArrayList 11-32 LinkedList 11-33 Maps 11-35 Types of Maps 11-36 Example of Using Maps 11-37 Summary 11-39 Practice 11 Overview: Using Arrays and Collections 11-40 12 Using Generic Types Objectives 12-2 Generics 12-3 Declaring Generic Classes 12-5 Using Generic Classes 12-6 Generic Methods 12-7 Wildcards 12-9 Raw Types 12-11 Type Erasure 12-12 Summary 12-13 Oracle University and Global Business Solutions Peru SAC use only Creating an Array of Object References 11-9 Initializing the Objects in an Array 11-10 Using an Array of Object References 11-11 Going Through the Array Elements 11-12 Arrays and Exceptions 11-13 Multidimensional Arrays 11-14 Passing Command-Line Parameters to main() 11-15 Java Collections Framework 11-16 Framework Interface Hierarchy 11-17 Collections Framework Components 11-18 The Collection Interface and the AbstractCollection Class 11-19 x THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Console Output Java applications and applets can output simple messages to the console as follows: System.out.println("This is displayed on the console"); boolean Boolean type, can be true or false byte 1-byte signed integer char Unicode character (i.e 16 bits) short 2-byte signed integer int 4-byte signed integer long 8-byte signed integer float Single-precision fraction, significant figures double Double-precision fraction, 15 significant figures Operators + - * / % ++ -+= -= *= /= %= etc && Logical AND For example, if (i > 50 && i < 70) The second test is only carried out if necessary use & if the second test should always be done Logical OR For example, if (i < || i > 100) The second test is only carried out if necessary use | if the second test should always be done Logical NOT For example, if (!endOfFile) || ! == != > >= < >>> instanceof Arithmetic operators (% means remainder) Increment or decrement by result = ++i; means increment by first result = i++; means the assignment first For example, i += is equivalent to i = i + Oracle University and Global Business Solutions Peru SAC use only Data Types Relational operators Bitwise operators (AND, OR, XOR, NOT) Bitwise shift operators (shift left, shift right with sign extension, shift right with fill) Test whether an object is an instance of a class For example, if (anObj instanceof BankAccount) System.out.println(“$$$”); THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Control Flow: if … else String dayname; … if (dayname.equals("Sat") || dayname.equals("Sun")){ System.out.println("Hooray for the weekend"); } else if (dayname.equals("Mon")) { System.out.println("I don’t like Mondays"); } else { System.out.println("Not long for the weekend!"); } Control Flow: switch switch is used to check an integer (or character) against a fixed list of alternative values: int daynum; … switch (daynum) { case 0: case 6: System.out.println("Hooray for the weekend"); break; case 1: System.out.println("I don’t like Mondays"); break; Oracle University and Global Business Solutions Peru SAC use only if statements are formed as follows (the else clause is optional) The braces {} are necessary if the if-body exceeds one line; even if the if-body is just one line, the braces {} are worth having to aid readability: default: System.out.println("Not long for the weekend!"); break; } THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Control Flow: Loops int i = 0; while (i < 100) { System.out.println("Next square is: " + i++; } i*i); for (int i = 0; i < 100; i++) { System.out.println("Next square is: " + } i*i); int positiveValue; { positiveValue = getNumFromUser(); } while (positiveValue < 0); Oracle University and Global Business Solutions Peru SAC use only Java contains three loop mechanisms: THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Defining Classes // This file is Student.java The class is declared // public, so that it can be used anywhere in the program public class Student { private String name; private int numCourses = 0; // Constructor to initialize all the data members public Student(String n, int c) { name = n; numCourses = c; } // No-arg constructor, to initialize with defaults public Student() { this("Anon", 0); // Call other constructor } // finalize() is called when obj is garbage collected public void finalize() { System.out.println("Goodbye to this object"); } // Other methods public void attendCourse() { numCourses++; } public void cancelPlaceOnCourse() { numCourses ; } Oracle University and Global Business Solutions Peru SAC use only When you define a class, you define the data attributes (usually private) and the methods (usually public) for a new data type The class definition is placed in a java file as follows: public boolean isEligibleForChampagne() { return (numCourses >= 3); } } THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Using Classes To create an object and send messages to the object: public class MyTestClass { // Step - Declare object references // These refer to null initially in this example Student me, you; // Step - Create new Student objects me = new Student("Andy", 0); you = new Student(); // Step - Use the Student objects me.attendCourse(); you.attendCourse(); if (me.isEligibleForChampagne()) System.out.println("Thanks very much"); } } Oracle University and Global Business Solutions Peru SAC use only public static void main(String[] args) { THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Arrays An array behaves like an object Arrays are created and manipulated as follows: // Step - Create the array "object" itself squares = new int[5]; // Creates array with slots // Step - Initialize slots in the array for (int i=0; i < squares.length; i++) { squares[i] = i * i; System.out.println(squares[i]); } Note that array elements start at [0], and that arrays have a length property that gives you the size of the array If you inadvertently exceed an array’s bounds, an exception is thrown at run time and the program aborts Note: Arrays can also be set up by using the following abbreviated syntax: String[] cities = { "San Francisco", "Dallas", "Minneapolis", "New York", "Washington, D.C." }; Oracle University and Global Business Solutions Peru SAC use only // Step - Declare a reference to an array int[] squares; // Could write int squares[]; THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Inheritance and Polymorphism public class Account { private double balance = 0.0; public Account(double initBal) { balance = initBal; } public void deposit(double amt) { balance += amt; } public void withdraw(double amt) { balance -= amt; } public void display() { System.out.println("Balance is: " + balance); } } public class CheckAccount extends Account { private int maxChecks = 0; private int numChecksWritten = 0; public CheckAccount(double initBal, int maxChk) { super(initBal); // Call superclass ctor maxChecks = maxChk; // Initialize our data } public void withdraw(double amt) { super.withdraw(amt); // Call superclass numChecksWritten++; // Increment chk num } public void display() { super.display(); // Call superclass System.out.println(numChecksWritten); } Oracle University and Global Business Solutions Peru SAC use only A class can inherit all of the data and methods from another class Methods in the superclass can be overridden by the subclass Any members of the superclass that you want to access in the subclass must be declared protected The protected access specifier allows subclasses, plus any classes in the same package, to access that item } THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Abstract Classes // Abstract superclass public abstract class Mammal { … } // Concrete subclasses public class Cat extends Mammal { … } public class Dog extends Mammal { … } public class Mouse extends Mammal { … } Oracle University and Global Business Solutions Peru SAC use only An abstract class is one that can never be instantiated; in other words, you cannot create an object of such a class Abstract classes are specified as follows: THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - Abstract Methods // Abstract superclass public abstract class Mammal { // Declare some public abstract public abstract public abstract abstract methods void eat(); void move(); void reproduce(); // Define some data members if you like private double weight; private int age; // Define some concrete methods too if you like public double getWeight{} { return weight; } public int getAge() { return age; } } Oracle University and Global Business Solutions Peru SAC use only An abstract method is one that does not have a body in the superclass Each concrete subclass is obliged to override the abstract method and provide an implementation; otherwise, the subclass is itself deemed abstract because it does not implement all its methods THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - 10 Interfaces public interface Runnable { public void run(); } A class can implement an interface as follows The class is obliged to provide an implementation for every method specified in the interface; otherwise, the class must be declared abstract because it does not implement all its methods public class MyApp extends Applet implements Runnable { public void run() { // This is called when the Applet is kicked off // in a separate thread … } // Plus other applet methods … } Oracle University and Global Business Solutions Peru SAC use only An interface is similar to an abstract class with 100% abstract methods and no instance variables An interface is defined as follows: THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - 11 Static Variables public class Account { private String accnum; private double balance = 0.0; // Instance var // Instance var private static double intRate = 5.0; // Class var … } Static Methods A static method in a class is one that can access only static items; it cannot access any non-static data or methods static methods are defined in the class as follows: public class Account { public static void setIntRate(double newRate) { intRate = newRate; } public static double getIntRate() { return intRate; } … } To invoke a static method, use the name of the class as follows: Oracle University and Global Business Solutions Peru SAC use only A static variable is like a global variable for a class In other words, you get only one instance of the variable for the whole class, regardless of how many objects exist static variables are declared in the class as follows: public class MyTestClass { public static void main(String[] args) { System.out.println("Interest rate is" + Account.getIntRate()); } } THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - 12 Packages Related classes can be placed in a common package as follows: public class Car { … } // Engine.java package mycarpkg; public class Engine { … } // Transmission.java package mycarpkg; public class Transmission { … } Importing Packages Anyone needing to use the classes in this package can import all or some of the classes in the package as follows: import mycarpkg.*; // import all classes in package Oracle University and Global Business Solutions Peru SAC use only // Car.java package mycarpkg; or import mycarpkg.Car; // just import individual classes THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - 13 The final Keyword The final keyword can be used in three situations: Here are some examples: // final classes public final class Color { … } // final methods public class MySecurityClass { public final void validatePassword(String password) { … } } // final variables public class MyTrigClass { public static final double PI = 3.1415; … } Oracle University and Global Business Solutions Peru SAC use only final classes (for example, the class cannot be inherited from) final methods (for example, the method cannot be overridden in a subclass) final variables (for example, the variable is constant and cannot be changed) THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - 14 Exception handling is achieved through five keywords in Java: try The block of code where statements that can cause an exception are placed catch The block of code where error processing is placed finally An optional block of code after a try block, for unconditional execution throw The keyword that is used in the low-level code to generate or throw an exception throws The keyword that specifies the list of exceptions that a method can throw Here are some examples: public class MyClass { public void anyMethod() { try { func1(); func2(); func3(); } catch (IOException e) { System.out.println("IOException:" + e); } catch (MalformedURLException e) { System.out.println("MalformedURLException:" + e); } finally { System.out.println("This is always displayed"); } } public void func1() throws IOException { … } public void func2() throws MalformedURLException { … } public void func3() throws IOException, MalformedURLException { … } Oracle University and Global Business Solutions Peru SAC use only Exception Handling } THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming B - 15 Oracle University and Global Business Solutions Peru SAC use only THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED [...]... SortedSet and SortedMap interfaces Oracle University and Global Business Solutions Peru SAC use only Java Collections Framework THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 16 Collection List Map Set SortedMap SortedSet Copyright © 2009, Oracle All rights reserved Framework... show how to create and initialize the arrays As you will see, an array in Java is an object Oracle University and Global Business Solutions Peru SAC use only Arrays THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 3 Creating an Array of Primitives Null arrayName type[] arrayName;... Unicode 0000 Java uses the Unicode character set Examples of Valid Array Creation Example 1 final int SIZE = 4; int[] powers = new int[SIZE]; Oracle University and Global Business Solutions Peru SAC use only • // SIZE is a constant THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming. .. PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 11 Going Through the Array Elements Use a Loop to explore each element in the array for (int i = 0;i < categories.length; i++){ System.out.println("Category: "+categories[i]); } • Java 5.0 provides this alternative enhanced syntax for (String category: categories) { System.out.println ("Category: " +category); } Copyright © 2009, Oracle All... has two elements, and so on Oracle University and Global Business Solutions Peru SAC use only Java supports arrays of arrays: int[][] a = new int[10][]; for (int i = 0; i < a.length; i++) { a[i] = new int[i + 1]; } THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 14 • • main()has... THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 15 Java Collections Framework is an API architecture for managing a group of objects that can be manipulated independently of their internal implementation It is: • Found in the java. util package • Defined by six core interfaces and some implementation classes: –... Using JDBC to Access the Database Objectives 15-2 Java, Java EE, and Oracle 11g 15-3 Connecting to a Database with Java 15-4 Java Database Connectivity (JDBC) 15-5 Preparing the Environment 15-6 Steps for Using JDBC to Execute SQL Statements 15-7 Step 1: Register the Driver 15-8 Connecting to the Database 15-9 Oracle JDBC Drivers: Thin-Client Driver 15-10 Oracle JDBC Drivers: OCI Client Driver 15-11 Choosing... using the next() method Oracle University and Global Business Solutions Peru SAC use only • THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 19 Iterator Interface • • Implements an object-oriented approach for accessing elements in a collection Replaces the java. util.Enumeration... PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 20 Iterator Interface (continued) Oracle University and Global Business Solutions Peru SAC use only for (Iterator e = al.iterator(); e.hasNext(); ) { String s = (String) e.next(); System.out.println(s.toUpperCase()); } THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle. .. array object is created The following slides explain these three steps in detail Oracle University and Global Business Solutions Peru SAC use only 1 Declare the array THESE eKIT MATERIALS ARE FOR YOUR USE IN THIS CLASSROOM ONLY COPYING eKIT MATERIALS FROM THIS COMPUTER IS STRICTLY PROHIBITED Oracle Fusion Middleware 11g: Java Programming 11 - 4 Declaring an Array Create a variable to reference the array

Ngày đăng: 25/11/2016, 19:17

TỪ KHÓA LIÊN QUAN

w