Notes
Slide Show
Outline
1
Introduction to Java
for PL/SQL Programmers
2
Agenda
  • Java Basics
  • More advanced Java concepts
  • Implementing Java
  • Java vs. PL/SQL
  • Why use Java?
  • Deploying in Java


3
Overview
  • For PL/SQL programmers wanting to move to Java, there are some important conceptual differences to understand.
  • Learning Java is more than just learning a new syntax.
  • You need to take an object-oriented approach.
  • This presentation will point out basic differences between Java and PL/SQL along with elements requiring more careful attention.
4
Java Language
  • Fully supports object-orientation
  • Supports multi-tasking
  • Platform independent (code is portable)
  • Enhanced memory management
  • Garbage collection
5
Java Terms
  • Fundamental building block: class
    • Everything is a class
  • Datatypes
    • Primitive (also called simple or built-in)
      • Implemented for performance reasons
      • Each has a sister class
    • User-defined
  • Instance: specific occurrence of an object in a class
  • Variable: instantiation of a primitive datatype
6
Basic Parts of Java (1)
  • Comments
    • /* This is a long comment or                          multiline comment in Java. */
    • // This is a short comment.
    • --Double dashes don’t work.
    • /** automatically generates Javadoc */


7
Basic Parts of Java (2)
  • Access to objects controlled by keywords
    • public: code can be used anywhere
    • private: access limited to other members of the same class
    • protected: access a class member anywhere except from  a non subclass in a different package
  • Executable program blocks
    • Collection of declarations, specifiers, and methods
    • Specifiers modify class visibility and functionality
8
Sample Java Code
  •   public class MyFirstExample {
  •     public static void main ( String args[]){
  •         System.out.println(“Hello World”);
  •       }
  •    }
  • First line declares a new class
    • Specifier public
    • { } represent the start and end of each code block
  • Second line declares a method  - main()
    • JVM looks for method main( ) when application starts
    • static declaration needed because main() method called before any objects are created
    • void is a declaration of return type
    • Remainder of line used to pass parameters to main()method
  • Third line calls a library
9
Java Primitive Datatypes
  • byte
  • short
  • int
  • long
  • char
  • float
  • double
  • boolean
  • Can only hold a single value
  • Cannot be passed by reference or by using explicit pointers
  • Beware of auto casting of variables
    • Integer = float (auto rounds)
10
Types of Variables
  • Boolean
    • 2 values in Java
      • true and false
  • Character
    • Single byte
    • Can be numeric or character
    • Different from a PL/SQL CHAR
  • String
    • Defines an object
    • Used to declare string variables
    • Implemented in Java as an object
  • StringBuffer
    • Sister class to string
    • Used for character sequences that can change size and/or be modified
  • Numeric
    • For whole numbers
      • long, int, short, byte
    • For fractions
      • float, double
11
Object Concepts
  • Object: instance of a class
  • Objects are passed by reference
    • May be as complex as desired
  • To use a class, must declare an instance of it like any other datatype
    • Ex. Integer age = 38;
  • Each public class definition stored in its own source file
    • Has exactly same name as class with .java file extension
  • Compiled version called a class file (.class)
  • Related class files stored in libraries of packages
    • Stored in zip files (.jar)
12
Inheritance and Polymorphism
  • Object-orientation of Java supports inheritance and polymorphism
  • Inheritance
    • Properties/behaviors derived from parent class (superclass) to child (subclass)
  • Polymorphism
    • Can send a message to an object without concern for datatype
    • Includes overriding and overloading
  • Can add additional keywords to class definitions to provide specific capabilities
    • extends – inherits methods and variables
13
Inheritance & Polymorphism Examples
  • // A parent class for a 2-Dimensional object
    public class Box {
      int width  = 2;
      int length = 3;
    }
  • // A child class for a 3-Dimensional object, which inherits a parent class
    public class Cube extends Box {
      int height = 4;
      int width  = 1; //polymorphism - this variable(width) is set to 1.
      int volume = super.width * length * height;
      public Cube() {
        System.out.println("The volume of the cube is: " + volume);
      }

      public static void main(String[] args) {
        new Cube();
      }
    }
14
Variable/Object Declarations
  • In Java – can take place anywhere
  • Still need to set coding standards
    • Should still declare variables and objects as in traditional programming languages
    • Creating objects in “pseudo-declaration” sections makes code more consistent
      • Supports objects created on the fly
      • Code stands out and is easier to maintain
  • Cautions
    • Names may not begin with a number
    • Java is case sensitive
15
Java Arrays
  • Similar to arrays in other languages
  • Collections of objects of similar type
  • May have one or more dimensions (multidimensional)
  • Elements within arrays are accessed by indexes (start at 0)
  • To create an array:
    • Declare a variable
    • Allocate memory
    • Initialize elements
16
Collections
  • Powerful classes provide functionality for grouped objects
    • Dynamic arrays
    • Vectors
    • Linked lists
    • Trees
    • Hash tables
  • Java2 has implemented maps
    to associate key/value pairs.
17
Global Variables
  • May need methods or variables that are independent of class instantiations
  • Use keyword static
    • Member is instantiated when class is referenced for the first time
    • Act as global variables that are accessed by all instances of the class
    • Also used with main() method in application classes
18
Constants
  • Useful at the variable, method, and class levels
  • Use all uppercase when naming
  • Use keyword final (like CONSTANT in PL/SQL)
    • Variables must be initialized in same statement
    • Class cannot override method in a subclass
    • Class cannot be inherited
      • final only applies to method and class declarations
      • Can be overridden in a subclass
19
Control Statements
  • Java uses traditional control statements
    • if-else, switch, loop
    • Also includes three jump statements
      • break, continue, return
      • Control program flow during run time
  • Loop controls
    • Iterative control structures
    • Control repeated execution of specific code segments
    • for while, do while
    • Functionality can be extended using jump statements
    • Loop portions can be modified to handle almost any valid Java expression
20
Exception Handling
  • Can generate an exception using the keyword throw
  • Other Java exception handling statements:
    • try, catch, throws, finally

21
Interfaces
  • Similar to a PL/SQL package specification
  • Use to specify what a class will do without defining how it will be done
  • Must include a declaration statement for each of the class methods to be implemented
    • Does not include the method bodies and instance variables
    • Methods defined are abstract by definition
  • Allow you to compile code that depends on a class while deferring method bodies until interface is implemented
  • Variable declarations in interfaces are final and static by definition
    • Act as constants shared by all classes implementing the interface
22
Interface Example
  • Interface MyArea {
  •   int length = 3;
  •   int width = 2;
  •   double area();
  • }
  • class MyBox implements MyArea{
  •   public MyBox() { }
  •   double area(){ return (length * width); }
  • }
  • class MyTest {
  •   public static void main (String args[]) {
  •     MyArea boxArea = new MyBox();
  •     System.out.println("Test box area is: “ +
  •         boxArea.area() );
  •   }
  • }
23
Garbage Collection
  • Java handles de-allocation of memory for objects no longer referenced by a variable via the garbage collector
  • Explicit activities (ex. Releasing file handle for given object) handled through finalize() method
    • Called just before object is destroyed
    • No guarantee when finalize() method is executed
  • Can cause problems
    • Program not responsive during collection
24
Java vs. PL/SQL
  • Can’t just learn a new syntax
  • More than just {  } instead of BEGIN and END
  • Need an object oriented approach
    • Not always more efficient
    • May need to make design concessions
  • Build upon user-defined datatypes (classes) rather than functions and procedures
25
Java-PL/SQL Comparison
  • Java Example:
  • class MyBooleanOperators {
  • public static void main (String args[]) {
  • boolean myTestCondition = true;
  • if (myTestCondition) System.out.println("My condition was TRUE");
  • myTestCondition = false;
    //Now reset test condition to FALSE
  • if (myTestCondition) System.out.println("This line Never prints");
  • }
  • }
  • PL/SQL Example:


  • BOOLEAN operators may be TRUE, FALSE or NULL.
  • v_test_condition BOOLEAN := TRUE;


26
Java-PL/SQL Comparison
  • Java Examples:
  • The char datatype can only represent a single character or symbol, and may be initialized as follows:
  • // decimal equivalent of letter 'a' char myFirstChar = 97;
  • // using a character
    char mySecondChar = 'a';
  • // octal equivalent of letter 'a' char myThirdChar = '\141';
  • // Unicode (Hex) value for 'a'
  •    char myFourthChar = '\u0061' ;
  • PL/SQL Example:
  • The CHAR datatype is a fixed length string.


  • v_string_char CHAR(48) := 'Datatype CHAR is a fixed length string in PL/SQL';



27
Java-PL/SQL Comparison
  • public class MyRatio extends Object {
  •   public MyRatio() {
      int numerator = 5;
  •     int denominator = 0;
  •     int ratio;
  •     try {          //The beginning of a try block
  •       ratio = numerator / denominator ; //divide by zero
  •     }
  •     catch (Exception e) { //the start of the catch block
  •        e.printStackTrace(); //sends error msg to screen
  •     }
  •     System.out.println("Printed because the error is
           handled correctly");
  •   }
  •   public static void main(String[] args) {
       //This is the main method
  •      new MyRatio(); //This calls the constructor
      }
  •   }
28
Java-PL/SQL Comparison
  • DECLARE
  •   v_numerator INTEGER = 5;
  •   v_denominator INTEGER = 0;
  •   v_ratio INTEGER;
  • BEGIN
  •   BEGIN    -- The start of the (try) block
      -- problem code
  •     v_ratio := v_numerator / v_denominator;
  •   EXCEPTION   --This is like the catch block
  •     WHEN OTHERS
  •     THEN
  •       dbms_output.put_line(
  •       'Exception: ' || SQLCODE ||', '|| SQLTERM);
  •   END;
  •       dbms_output.put_line(
  •       'This is printed because the error is
  •        handled correctly');
  • END;
29
Why use Java?
  • Emerging language with many advantages over other programming languages
  • Offers fully object-oriented environment for development and web deployment
  • Improved flexibility
  • Ability to build sophisticated applications
  • Drawbacks
    • Newness
    • Less available technical expertise
30
The Market
  • The world wants Java, Web and Internet applications.
31
Is Java all you need to deploy on the Web?
  • NO!!!!
  • Java Deployment
    • JSP
    • Applet
    • Applications
  • Each alternative requires different tools and languages
32
Moving to a Java-Based Development Environment
  • Don’t abandon old development environment immediately
    • Java is still bleeding-edge technology
  • Java alone is not enough
    • Still need to know HTML, JavaScript, and other editors (FrontPage, Dreamweaver)
  • Keep it simple
    • Oracle Developer uses PL/SQL for both front and back ends
33
Java Applications
  • Required Tools
  • JDeveloper
  • Required Languages
  • Java
  • XML
  • JDBC
  • SQLJ
34
Java Applets
  • Required Tools
  • JDeveloper
  • Apache or IAS
  • Required Languages
  • Java
  • XML
  • JDBC
  • SQLJ
35
JSPs
  • Required Tools
  • JDeveloper
  • Apache or IAS
  • Required Languages
  • Java
  • HTML
  • JavaScript
  • XML, JDBC, SQLJ
36
Contact Information
  • Dr. Paul Dorsey - pdorsey@dulcian.com
  • Dulcian Website: www.dulcian.com
  • Oracle JDeveloper 3 Handbook
  • by Dr. Paul Dorsey & Peter Koletzke


  • Oracle Developer: Advanced Forms & Reports
  • by Peter Koletzke & Dr. Paul Dorsey


  • Oracle8 Design Using UML Object Modeling
  • by Dr. Paul Dorsey & Joseph Hudicka


  • Oracle Designer Handbook
  • by Peter Koletzke & Dr. Paul Dorsey