Скачать презентацию Java for RPG Programmers Phil Coulthard coulthar ca ibm Скачать презентацию Java for RPG Programmers Phil Coulthard coulthar ca ibm

61358991de4c2b1129a2e517db28d746.ppt

  • Количество слайдов: 90

Java for RPG Programmers Phil Coulthard: coulthar@ca. ibm. com George Farr: farr@ca. ibm. com Java for RPG Programmers Phil Coulthard: [email protected] ibm. com George Farr: [email protected] ibm. com IBM | 2003, 2004 © 2004 IBM Corporation tm. Java is a trademark of Sun Microsystems Inc

R P G WEBCAST SCHEDULE Today’s event will run one-hour long. Here are the R P G WEBCAST SCHEDULE Today’s event will run one-hour long. Here are the expected times for each segment of the webcast: • : 00 – : 10: Moderator introduces the speaker and discusses the details of the Webcast. • : 10 - : 60: Speaker delivers a Power. Point presentation on the webcast topic. You can submit questions to the speaker at any time during the event. Just click on the “Ask a Question” button in the lower left corner of your presentation screen. © 2003 IBM Corporation

R P G TECHNICAL FAQs Here answers to the most common technical problems users R P G TECHNICAL FAQs Here answers to the most common technical problems users encounter during a webcast: Q: Why can’t I hear the audio part of the webcast? A: Try increasing the volume on your computer. Q: I just entered the webcast and do not see the slide that the speaker is referring to. What should I do? A: The slides are constantly be pushed to your screen. You’ll should refresh (hit F 5) to view the latest slide. If your question is still not answered, please click the “Ask a Question” button in the lower left corner of your presentation screen and submit your problem. A technical support person will contact you. © 2003 IBM Corporation

Java for RPG Programmers Phil Coulthard: coulthar@ca. ibm. com George Farr: farr@ca. ibm. com Java for RPG Programmers Phil Coulthard: [email protected] ibm. com George Farr: [email protected] ibm. com IBM | 2003, 2004 © 2004 IBM Corporation tm. Java is a trademark of Sun Microsystems Inc

R P G Disclaimer Acknowledgement: l. This presentation is a collaborative effort of the R P G Disclaimer Acknowledgement: l. This presentation is a collaborative effort of the IBM Toronto AS/400 Applicaiton Development presentation team, including work done by: ƒ Phil Coulthard, George Farr l. This presentation is based on the books (www. mcpressonline/ibmpress) ƒ Java for RPG Programmers, ISBN 1 -931182 -06 -X ƒ Java for S/390 and AS/400 COBOL Programmers, 1 -58347011 -5 l. It also contains information from the related Student Workbook (www. mcpressonline/ibmpress) ƒ Java for RPG and COBOL Programmers on i. Series Student Workbook Disclaimer: l. The information contained in this document has not been submitted to any formal IBM test and is distributed on an as is basis without any warranty either express or implied. The use of this information or the implementation of any of these techniques is a customer responsibility and depends on the customers' ability to evaluate and integrate them into the customers' operational environment. While each item may have been reviewed by IBM for accuracy in a specific situation, there is no guarantee that the same or similar results will result elsewhere. Customers attempting to adapt these techniques to their own environment do so at their own risk. Reproduction: l. The base presentation is the property of IBM Corporation. Permission must be obtained PRIOR to making copies of this material for any reason. © 2003 IBM Corporation

R P G What is Java? Ÿ An OO programming language ƒ Created by R P G What is Java? Ÿ An OO programming language ƒ Created by Sun Microsystems Inc. in 1995 ƒ Adopted by Netscape in 1996 ƒ Heavy investment by IBM ever since Ÿ Initially for: ƒ Applets that run in Web Browsers ƒ Applications that are client-GUI or server non-GUI Ÿ Now also for: ƒ Servlets that run in a Web Server ƒ Enterprise Java. Beans that run on a server ƒ And much more. . . © 2003 IBM Corporation

R P Java Landscape G Ÿ How is Java packaged? ƒ For developers l R P Java Landscape G Ÿ How is Java packaged? ƒ For developers l In a "Java Development Kit" (JDK) ƒ Compiler, runtime, command line tools ƒ For runtime l In a "Java Virtual Machine" (JVM) ƒ Interpreter Ÿ How do you get Java? ƒ For developers JDK from Sun (www. java. sun. com) or IBM (www. ibm. com/java) l. JDK also built-in to Java tools like Visual. Age for Java, WSSD l ƒ For runtime l JVM built-in to many Operating Systems, Web Browsers, App'n Servers, PDAs, Cell Phones, etc © 2003 IBM Corporation

R P G JDK Contents Command line tools JVM / interpreter ( R P G JDK Contents Command line tools JVM / interpreter ("java") Bytecode compiler ("javac") Doc'n tool ("javadoc") Documentation Compression tool ("jar") . . . Packages language databas e access math graphics file system . . . networking utilities © 2003 IBM Corporation

R P Java Classes G Ÿ Classes ƒ Compilation unit l no matter what R P Java Classes G Ÿ Classes ƒ Compilation unit l no matter what you are using Java for! ƒ All fields and executable code are inside classes ƒ Source files are compiled into class files Ÿ Bytecode ƒ What are inside class files ƒ Assembler language for Java l what the JVM "interprets" © 2003 IBM Corporation

R P Java Beans G Ÿ Java. Beanstm ƒ Classes designed for fine-grained re-use R P Java Beans G Ÿ Java. Beanstm ƒ Classes designed for fine-grained re-use Java's components, like Microsoft VB's VBX l. Not to be confused with Enterprise Java. Beans!! l ƒ Beans contain properties (fields), lmethods (paragraphs), levents (eg, button-pressed) l ƒ Tools can discover contents dynamically ƒ And present list to use to select from or change Ÿ JARtm Files (Java ARchive) ƒ Java way to group/compress class files l for easy distribution (uses ZIP technology) © 2003 IBM Corporation

R P G Using Java Ÿ Applications ƒ Java command line programs (you call) R P G Using Java Ÿ Applications ƒ Java command line programs (you call) Ÿ Appletstm ƒ Java Web Browser programs (Web Browser calls) Ÿ Enterprise Java. Beanstm ƒ Enterprise-scale re-usable components (Application Server calls) ƒ Large scale (e. g. , payroll) versus Java. Beans (e. g. , tax) Ÿ Java Servletstm ƒ Java Web Server programs (Web Server calls) Ÿ Java. Server Pagestm ƒ HTML plus embedded Java (Servlets call) © 2003 IBM Corporation

R P Java Tools G Ÿ Java Tools are Optional l minimal requirement: JDK R P Java Tools G Ÿ Java Tools are Optional l minimal requirement: JDK + editor Productive l e. g. , wizards and debuggers Numerous l From IBM, Symantec, Sun, Inprise, . . . Ÿ IBM Java Tool story "Next generation" tools are Web. Sphere Studio Site Developer (Java, Web, XML tooling) l Web. Sphere Studio Application Developer (+ EJB tooling) l For i. Series, there is l Web. Sphere Development Studio Client © 2003 IBM Corporation

R P Java Life. Cycle G Ÿ Use javac to compile ƒ Use java R P Java Life. Cycle G Ÿ Use javac to compile ƒ Use java to run from command line l If it is application or to unit-test this individual class compile javac java run java Your app! class ƒ For applets l Use HTML/JSP file with tag pointing to the applet ƒ For servlets l Use HTML/JSP file with
tag pointing to the servlet ƒ For EJBs l Include in Web Application that is deployed to a J 2 EE Container © 2003 IBM Corporation

R P Compiling Java G /* Prototypical Hello World application */ public class Hello R P Compiling Java G /* Prototypical Hello World application */ public class Hello java { public static void main(String args[]) "main" { method System. lout. println("Hello World!"); called by } JVM } E: mycode>javac Hello. java: 6: No variable lout defined in class System. lout. println("Hello World!"); ^ 1 error System. out. println("Hello World!"); E: mycode>javac Hello. java © 2003 IBM Corporation

R P G Running Java Apps E: mycode>java Hello World! Hello class © 2003 R P G Running Java Apps E: mycode>java Hello World! Hello class © 2003 IBM Corporation

R P Non-Java Resources G ƒ How to access non-Java resources? Data. Base Programs R P Non-Java Resources G ƒ How to access non-Java resources? Data. Base Programs Transactions File System Commands ? Web Server + App Server Servlet ? ? Java EJB Application Java server Application network Web page Web Browser Other. . . Applet Java client Application © 2003 IBM Corporation

R P G i. Series Toolbox For Java Ÿ 100% Java classes for: free! R P G i. Series Toolbox For Java Ÿ 100% Java classes for: free! JDBC access to DB 2/400 ƒ DDM record level access to DB 2/400 ƒ Data Queue access Shipped with ƒ Print access OS/400, ƒ Program Call, Command Call WDSc ƒ File system access ƒ Client to server connections, remote login ƒ Much more. . . ƒ Ÿ Runs anywhere ƒ OS/400, Windows, Linux, Unix, . . . © 2003 IBM Corporation

R P G Agenda Java versus RPG © 2003 IBM Corporation R P G Agenda Java versus RPG © 2003 IBM Corporation

R P MODULE vs CLASS G CRTRPGMOD RPG Module Fields Procedures javac Java Class R P MODULE vs CLASS G CRTRPGMOD RPG Module Fields Procedures javac Java Class Variables Methods RPG IV Java © 2003 IBM Corporation

Java Class Syntax class keyword: class name what we're public class Customer defining { Java Class Syntax class keyword: class name what we're public class Customer defining { Global private int cust. Id; private char cust. Code; variables public modifier: field name anyone can use R P G Java public static void main(String args[]) private modifier: only code in this class can access braces { } delimit start and end of class } { . . . } "main" method (described later) public int set. My. Variable(int new. Value) { } . . . Methods (described next) © 2003 IBM Corporation

R P G public modifier: anyone can call Java Methods method name public void R P G public modifier: anyone can call Java Methods method name public void set. Id(int id. Parm) { parameter type + name void keyword: nothing returned Code goes here } // end of the method private modifier: only code in this class can call braces { } delimit start and end of methods Comments integer value returned private int read() { Code goes here } // end of the method Java © 2003 IBM Corporation

R P G Java Method Example int max(int parm 1, int parm 2) { R P G Java Method Example int max(int parm 1, int parm 2) { int retval; no public modifier specified so only classes in this package can call this method Local Variables Parm type/name pairs if (parm 1 > parm 2) retval = parm 1; else retval = parm 2; return retval; } Java Re tur n Va lue int is the integer data type in Java © 2003 IBM Corporation

R P G Naming Conventions Ÿ Class names should ƒ be all lowercase except R P G Naming Conventions Ÿ Class names should ƒ be all lowercase except Ÿ letter of each word first Ÿ Order. Entry eg: Ÿ Method / field names should ƒ be all lowercase except Ÿ letter of each word other than first Ÿ process. Order eg: Ÿ Constants should ƒ be all uppercase Ÿ SUNDAY eg © 2003 IBM Corporation

R P Packages G contains procedures RPG IV Order Inventory module class *SRVPGM Back. R P Packages G contains procedures RPG IV Order Inventory module class *SRVPGM Back. Order class Inventory module Back. Order module orderentry Order orderentry contains methods class package Java © 2003 IBM Corporation

R P G Java Defining Packages orderentry R P G Java Defining Packages orderentry "package" must be first statement in source file Order class Back. Order class Inventory Order. java package orderentry ; public class Order {. . . } class "package" is like compiler directive Back. Order. java package orderentry ; public class Back. Order {. . . } © 2003 IBM Corporation

R P Using Packages G orderentry R P Using Packages G orderentry "import" enables access to classes in package Order class Back. Order class Inventory can import one class or all (*) class Order. java "import" is like ADDLIBLE. It is NOT like /COPY! import orderentry. * ; public class Order {. . . } Java © 2003 IBM Corporation

R P G Naming Packages Ÿ Package names are ƒ usually all lowercase ƒ R P G Naming Packages Ÿ Package names are ƒ usually all lowercase ƒ usually multi-part, dot separated Ÿ Java-supplied packages ƒ all named java. xxx ƒ for example: java. awt or java. awt. event Ÿ Your packages java. lang always imported for you ƒ will start with com. xxx, where xxx. com is your company's domain name ƒ e. g. , IBM's start with com. ibm. xxx © 2003 IBM Corporation

R P Comparing Anatomies G RPG JAVA COMMENTS *PGM Application Program object == Application R P Comparing Anatomies G RPG JAVA COMMENTS *PGM Application Program object == Application *SRVPGM Package Module Class Fields Procedures Variables Methods Compilation unit Global variables Functions Variables Local Variable Code Executable code © 2003 IBM Corporation

R P G Java Syntax Ÿ Statements are free-format ƒ extra blanks and lines R P G Java Syntax Ÿ Statements are free-format ƒ extra blanks and lines are ignored ƒ statements end with semi-colon Ÿ Blocks use braces {} ; C-like syntax ƒ start and end of classes Java ƒ start and end of methods ƒ start and end of conditional / loop blocks Ÿ names are case-sensitive All ƒ abc NOT= ABC ƒ even source file names are case-sensitive © 2003 IBM Corporation

R P Java Comments G l. Multi-line comment: /* this is a multi line R P Java Comments G l. Multi-line comment: /* this is a multi line comment */ /*---------------* * Please read these comments * * as they are very important! * *---------------*/ l. Single line comment: // This whole line is a comment int my. Variable = 10; // Only this part is a comment l. Java. Doc comment: note double asterisk /** This is the scan package * this is the second line. @author George & Phil * @version Feb 26, 2000 */ * © 2003 IBM Corporation

R P Data Types Overview G RPG IV Numeric Types Other Types graphic character R P Data Types Overview G RPG IV Numeric Types Other Types graphic character indicator binary integer zoned unsigned unicode float packed d/t/z or Primitive Types Java Numeric Integer byte short boolean Float int char long float Reference Types covered later double © 2003 IBM Corporation

Java Primitive Types Type In Use Description R P G Type Integer In Use Java Primitive Types Type In Use Description R P G Type Integer In Use int i; Description Java 4 byte signed: about +- 2 billion long l; 8 byte signed: about +- huge # byte b; 1 byte signed: -128 to + 127 Short s; 2 byte signed: -32768 to 32767 Character char c; 2 byte unicode. 1 char only! Boolean boolean flag; Float Single float f; 32 bit Float Double d; 64 bit Long Byte true or false © 2003 IBM Corporation

R P G RPG Data Types. . . Java Comments numeric (no decimals) short R P G RPG Data Types. . . Java Comments numeric (no decimals) short or int depends on length numeric (with decimals) float (length 4) float (length 8) character (length one) character (length n) graphic unicode indicator float or double, or depends on length. Big. Decimal class Big. Decimal is a Java supplied IEEE standard float Both are class double Both are IEEE standard char single character only date, timestamp Gregorian. Calend A class, not a primitive type ar class String class boolean A class, not a primitive type '1' = true, '0' = false © 2003 IBM Corporation

R P G What about Packed? l. No packed decimal data type in Java R P G What about Packed? l. No packed decimal data type in Java ƒ Could use float / double, but precision is a problem for "fixed decimal" numbers l. Answer: Big. Decimal class Java ƒ Part of java. math package ƒ A class, not a built-in "primitive" data type ƒ Software simulation of fixed decimal numbers ƒ Unlimited precision (total number of digits) ƒ Program control over scale (number of decimal digits) ƒ Methods include: add, subtract, divide, multiply, set. Scale l. See also: Big. Integer class © 2003 IBM Corporation

R l l P G Declaring Fields in RPG +*. . 1. . . R l l P G Declaring Fields in RPG +*. . 1. . . +. . . 2. . . +. . . 3. . . +. . . 4. . . +. . . 5. . . +. . . 6. . . +. . . ****** Beginning of data ***************** FQSYSPRT O F 80 PRINTER OFLIND(*INOV) D FIRST S 7 A INZ('George ') D AGE S 2 B 0 INZ(25) D*------------------------------C *LIKE DEFINE FIRST LAST -3 C EVAL LAST='FARR' C MOVE ' AGE WAS--->'AGETEXT 12 C EXCEPT RESULT C MOVE *ON *INLR C*------------------------------OQSYSPRT E RESULT O FIRST 5 O LAST 10 O AGETEXT 22 O AGE 26 ü RPG IV On the C specification Using the Define operation code On the new Definition specification © 2003 IBM Corporation

R P G Java Declaring Variables , type name = modifiers static final public R P G Java Declaring Variables , type name = modifiers static final public private protected ; byte short int long char float double boolean expression literal, variable, computation class-name © 2003 IBM Corporation

R P G Declaring RPG IV D*. . 1. . +. . 2. . R P G Declaring RPG IV D*. . 1. . +. . 2. . +. . 3. . +. . 4. . DEmp. Rcd DS D number 5 I 0 D type 1 A D name 20 A D address 50 A D hired D D salary 9 P 2 DS = "Data Structure" S = "Standalone" Java public class Employee. Record { private int number; private char type; private String name; private String address; private Date hired; private Big. Decimal salary; } access modifiers data types actual variable names © 2003 IBM Corporation

R P G Where's the Length? Ÿ You do not specify #digits! ƒ Data R P G Where's the Length? Ÿ You do not specify #digits! ƒ Data Type determines # of bytes Java Ÿ which determines how much var can hold Ÿ short holds -32768 to 32767 eg: Ÿ Usually you will use: ƒ integer ("int") when no decimals (unless int numbers > 2 billion) ƒ Big. Decimal class when decimals needed ƒ String class when dealing with characters © 2003 IBM Corporation

R P G Declaring and Init'g public class Employee. Record { private int number R P G Declaring and Init'g public class Employee. Record { private int number = 0; private char type = 'R'; private String name = "Joe Public"; private String address = "1 Young St"; private Date hired = new Date(); private Big. Decimal salary = new Big. Decimal("30000. 00"); } Java Note: new operator described later RPG IV D*. . 1. . +. . 2. . +. . 3. . +. . 4. . +. . 5 DEmp. Rcd DS D number 5 I 0 INZ(0) D type 1 A INZ('R') D name 20 A INZ('Joe Public') D address 50 A INZ('1 Young St') D hired D INZ(D'1999 -12 -31') D salary 9 P 2 INZ(30000) © 2003 IBM Corporation

R P G R P G "static" and "final" keywords define a constant Declaring Constants Java public class Employee. Record. Defaults { static final int NUMBER = 0; static final char TYPE = 'R'; static final String NAME = "Joe Public"; static final String ADDRESS = "1 Young St"; static final Date HIRED = new Date(); static final Big. Decimal SALARY = new Big. Decimal("30000. 00"); } RPG IV D*. . 1. . +. . 2. . +. . 3. . +. . 4. . +. . 5 D*Emp. Rcd. DFT DS D number. DFT C CONST(0) D type. DFT C CONST('R') D name. DFT C CONST('Joe Public') D address. DFT C CONST('1 Young St') D hired. DFT C CONST(D'1999 -12 -31') D salary. DFT C CONST(30000) © 2003 IBM Corporation

R P Casting in RPG G . . . +. . . 1. . R P Casting in RPG G . . . +. . . 1. . . +. . . 2. . . +. . . 3. . . +. . . 4. . . +. . . 5. . . +. . . ******** Beginning of data ************ FQSYSPRT O F 80 PRINTER OFLIND(*INOV) D DS 1 DS D int 5 5 I 0 INZ(25) D BIN 9 9 B 0 INZ(22) D ZONE 9 9 S 0 INZ(30) D PACK 9 9 P 0 INZ(40) D*-------------------------C MOVE BIN 9 INT 5 C EXCEPT RESULT C MOVE PACK 9 INT 5 C EXCEPT RESULT C MOVE ZONE 9 INT 5 C EXCEPT RESULT C MOVE *ON *INLR OQSYSPRT E RESULT O INT 5 15 ********* End of data ************* casting is always implicit in RPG IV © 2003 IBM Corporation

R P G Casting in Java public class Test. Cast { public static void R P G Casting in Java public class Test. Cast { public static void main(String args[]) { short s. Value = 10; // 2 bytes Java long l. Value = 30; // 8 bytes l. Value = s. Value; // implicit s. Value = (short)l. Value; // explicit } } Ÿ Casting in Java ƒ only implicit if target type larger than source ƒ else must explicitly cast: (target-type)source © 2003 IBM Corporation

R P Assignment G Ÿ RPG IV: ƒ free-format EVAL op-code & equal operator R P Assignment G Ÿ RPG IV: ƒ free-format EVAL op-code & equal operator '=' Ÿ Java: ƒ no op-code, just equal operator "=" RPG III C RPG IV MOVE 0 X C EVAL Java X = 0; Ÿ Java also allows stringing: A = B = C = 25; Java © 2003 IBM Corporation

R P If-Else G C* C C* op-code IF Body ELSE Body ENDIF : R P If-Else G C* C C* op-code IF Body ELSE Body ENDIF : factor 2 expression RPG IV if (condition) { //Body; } else Java { //Body; } Ÿ Similar in both languages Ÿ But in Java ƒ Body can be compound or single statement Ÿ Single statement bodies don't need braces © 2003 IBM Corporation

R C C C C C P IF Example G AGE RPG III C R C C C C C P IF Example G AGE RPG III C C C C C IFLE MOVE ELSE MOVE ENDIF RPG IV 2 0 PRICE 10 5 PRICE 10 PRICE IF EVAL ELSE EVAL ENDIF if (age price else if price else price AGE <= 2 PRICE = 0 AGE <= 10 PRICE = 5 PRICE = 10 <= 2) Java = 0; (age <= 10) = 5; = 10; note single statement in body so braces not required © 2003 IBM Corporation

R P G RPG C C C* C C* C SELECT vs switch Java R P G RPG C C C* C C* C SELECT vs switch Java SELECT WHEN day = MON do something WHEN day = TUE do something WHEN day = WED do something WHEN day = THU do something OTHER do something ENDSL switch (day) { case MON: // do something break; case TUE: // do something break; . . default: // default code } // end switch statement Improved readability over nested IFs Structures are similar in both languages! © 2003 IBM Corporation

R P G Breakless Switch switch (day) { case 1: case 2: case 3: R P G Breakless Switch switch (day) { case 1: case 2: case 3: Control goes to first "case" that matches the expression, then executes until "break" is encountered, or the end brace // Mon-Wed code break; case 4: case 5: Java // Thur-Fri code break; default: } // end switch statement © 2003 IBM Corporation

R P Looping Around G l. RPG and Java, like all other languages, have R P Looping Around G l. RPG and Java, like all other languages, have three main loops. They are. . . RPG C C* C JAVA start DO limit index : ENDDO DO C C* C DO-WHILE DO-UNTIL DOW expression : ENDDO DOU expression : ENDDO for (initialization; condition; increments) { FOR // body } while (expression) { // body WHILE } do { DO-WHILE // body } while (expression); © 2003 IBM Corporation

R P continue, break G A Label: Note: continue and break can specify a R P continue, break G A Label: Note: continue and break can specify a labeled loop to explicitly iterate or leave C C C RPG IV out: for (int i=0; i < 10; i++) { for (int j=0; j < 10; j++) { if (int. Array[i][j] == -1) { // some code continue out; } if (int. Array[i][j] == -2) break; } // end inner for-loop // outside inner loop } // end outer for-loop Java A DOW IF ITER ENDIF LEAVE ENDDO RECORDN = 2938174 CODE='A 1' © 2003 IBM Corporation

R P Operators: Relational G Operation Java Operator RPG Op. Codes RPG IV Operator R P Operators: Relational G Operation Java Operator RPG Op. Codes RPG IV Operator Equal == EQ = Not Equal != NE <> Greater Than > GT > Less Than < LT < GT or Equal >= GE >= Lt or Equal <= LE <= Or || ORxx OR And && ANDxx AND Negation ! NOT © 2003 IBM Corporation

R P Relational Example G Java if ( (age <= 2) || ( (age R P Relational Example G Java if ( (age <= 2) || ( (age >= 65) && (curr. Day == SENIORS_DAY) ) ) price = 0; note double equals: == C C C C C AGE CURDAY RPG IV IFLE 2 ORGE 65 ANDEQSENDAY MOVE 0 ENDIF IF EVAL ENDIF PRICE RPG III (age <= 2) OR ((age >= 65) AND (currday = SENIORS-DAY) ) price = 0 © 2003 IBM Corporation

R P Operators: Math G Operation Add Subtract Multiply Divide Modulus Power Java Operator R P Operators: Math G Operation Add Subtract Multiply Divide Modulus Power Java Operator RPG Op-Codes RPG Operator + * / % ADD, Z-ADD DIV + * / DIV and MVR n/a ** Use exp or pow in Math class SUB, Z-SUB MULT © 2003 IBM Corporation

R P Math Examples G C* A = B+C C B C* A = R P Math Examples G C* A = B+C C B C* A = (B+C)/12 C B C A RPG III ADD C A 50 ADD DIV C 12 A A 50 RPG IV C C EVAL a = b + c a = (b+c)/12 Java a = b + c; a = (b+c)/12; © 2003 IBM Corporation

R P G Contracted Assignment l. What does this mean? Java X += 10; R P G Contracted Assignment l. What does this mean? Java X += 10; l. Answer: short form for. . . X = X + 10; l. All binary operators supported: X *= 10; X /= 2; Y -= 1; Same as using ADD op-code in RPG and not specifying factor 1 value © 2003 IBM Corporation

R P G Increment, Decrement l. What does this mean? same as X++; C R P G Increment, Decrement l. What does this mean? same as X++; C and l. Answer: short form for C++ X = X + 1; l. Also supports decrementing: X--; l. Can be before or after variable: ++X; --X; © 2003 IBM Corporation

R P Reference Data Types G Primitive Types Java Numeric boolean Integer byte short R P Reference Data Types G Primitive Types Java Numeric boolean Integer byte short Float int float char long double Reference Types Classes Java-Supplied Arrays Strings User-Defined © 2003 IBM Corporation

R P G Using Classes l. To use a class, you must do R P G Using Classes l. To use a class, you must do "h two things: o • Declare an object reference variable: w" Ÿ Declare a variable using class as the type: My. Class my. Variable; • Instantiate an object using "new" my. Variable = new My. Class(); My. Class © 2003 IBM Corporation

R P G Objects l. Object reference variables are ƒ merely pointers, or references R P G Objects l. Object reference variables are ƒ merely pointers, or references , to objects ƒ initially point to "null" Ÿ a keyword in Java l. The new operator: ƒ allocates memory for the class ("instantiates") Ÿ Total memory needed by all global variables in class l. Allocated memory known as ƒ object or ƒ instance of class © 2003 IBM Corporation

public class Customer { private int private String private int private Big. Decimal R public class Customer { private int private String private int private Big. Decimal R P G Object Example id; Customer a. Cust = name; new Customer(); address; phone; account. Balance; public void set. Id(int cust. Id) { id = cust. Id; } you can declare & instantiate in one step! how to call the public boolean read. Info() { methods? boolean readok = false; // read customer info from database return readok; } // more methods. . . } © 2003 IBM Corporation

R P Dot Operator G l. To call a method ƒ use dot operator R P Dot Operator G l. To call a method ƒ use dot operator on object reference variable public class Process. Customer { public static void main(string args[]) { } } Customer a. Cust = new Customer(); a. Cust. set. Id(100012); Can also a. Cust. read. Info(); access non. Must use object reference variable, not Class name private variables with dot operator © 2003 IBM Corporation

R P G Why Objects? R P G Why Objects? "w hy " l. Why must we instantiate? ƒ Because you can instantiate more than one! Customer cust 1 = new Customer(); cust 1. set. Id(100011); Customer cust 2 = new Customer(); cust 2. set. Id(100012); . . . l. Each gets their own memory ƒ Each hold unique values for their variables Ÿ Hence we call global variables "instance variables" © 2003 IBM Corporation

R P G Notes on Objects Ÿ Classes are like templates ƒ or R P G Notes on Objects Ÿ Classes are like templates ƒ or "cookie cutters" Ÿ Classes have no memory allocated ƒ Objects have the actual memory Ÿ object == "instance of class" Ÿ object == actual "cookies" © 2003 IBM Corporation

R P Class/Object Examples G Ÿ Possible classes ƒ ƒ ƒ ƒ Customer Employee R P Class/Object Examples G Ÿ Possible classes ƒ ƒ ƒ ƒ Customer Employee State. Tax Customer. Id Employee. Id Payroll Order Push. Button Ÿ e. g. , in a GUI application Ÿ Each instance might want different label text © 2003 IBM Corporation

R P G Equating References Object 1 step 1 Master object 1 = new R P G Equating References Object 1 step 1 Master object 1 = new Master(); Master object 2 = new Master(); Master Object 2 step 2 object 1 = object 2; copies addresses! Nobody points to object 1 now so it is swept up by Garbage Collector © 2003 IBM Corporation

R P Calling Java Methods G ƒ Three ways to call methods: Ÿ Assignment R P Calling Java Methods G ƒ Three ways to call methods: Ÿ Assignment statement Ÿ returned result is saved in a variable Ÿ Expression Ÿ returned result used in expression but not saved Ÿ Expression Ÿ Runs the method and disregards return value RPG EVAL my. Var = my. Proc(p 1 : p 2) IF CALLP Java my. Proc(p 1 : p 2) = 10 my. Proc(p 1 : p 2) my. Var = my. Object. my. Proc(p 1, p 2); if (my. Object. my. Proc(p 1, p 2) == 10) my. Object. my. Proc(p 1, p 2); EVAL my. Var = no. Parms my. Var = my. Object. no. Parms(); IF no. Parms = 10 if (my. Object. no. Parms() == 10) CALLP no. Parms my. Object. no. Parms(); © 2003 IBM Corporation

R P Overloading G Ÿ Method Overloading ƒ Methods in same class with same R P Overloading G Ÿ Method Overloading ƒ Methods in same class with same name ! But: Ÿ Number or type of parameters are different Ÿ method name + nbr and type of parms == "signature" ƒ Official name 'method overloading' public { // } int max(int parm 1, int parm 2) code to return max of two integers float max(float parm 1, float parm 2) code to return max of two floats © 2003 IBM Corporation

R P G Static Variables Ÿ Java variables can be static : ƒ Use R P G Static Variables Ÿ Java variables can be static : ƒ Use static modifer (like RPG's STATIC keyword) Ÿ Cannot specify it on local variables in methods! ƒ Static variables are called class variables Ÿ Versus instance variables ƒ All objects share same value for static vars Ÿ Qualify with the class name to access them class Rental. Car { static int total. Rented = 0; public void rent. Car() { //. . . ++total. Rented; } if (Rental. Car. total. Rented > MAX_CARS) } © 2003 IBM Corporation

R P G Static Methods Ÿ Methods can be static too ƒ called class R P G Static Methods Ÿ Methods can be static too ƒ called class methods ƒ Equivalent to standalone procedure Ÿ Call by qualifying with class name, not obj ref variable ƒ Cannot reference instance variables in the method class My. Helper. Routines { // static method. . . static int max(int p 1, int p 2) { if (p 1 > p 2) return p 1; else return p 2; } } int maxvalue = My. Helpers max(1000, 2000); If your method does not reference or use any instance variables, be static! © 2003 IBM Corporation

R P Constructors G l. Classes can have constructors ƒ Special methods identified by: R P Constructors G l. Classes can have constructors ƒ Special methods identified by: Ÿ Same name as class Ÿ No return value specified (not even void) ƒ Called by JVM when object created with new Ÿ Right after allocating memory for the object ƒ Your opportunity to do initialization Ÿ like *INZSR in RPG constructor public class My. Class { public My. Class() { //. . . } } © 2003 IBM Corporation

R P G Ctor Overloading Ÿ Constructors can be overloaded ƒ Same as with R P G Ctor Overloading Ÿ Constructors can be overloaded ƒ Same as with all methods Ÿ Number or type of parameters must be unique ƒ Compiler, Runtime determine which to call Ÿ By matching number, type of new parameters Constructor with no parms called default constructor My. Class obj 1 = new My. Class(10); My. Class obj 1 = new My. Class(10, 20); private int my. Variable; private int my. Other. Variable = 0; public My. Class(int parm 1) { my. Variable = parm 1; } public My. Class(int parm 1, int parm 2) { my. Variable = parm 1; my. Other. Variable = parm 2; } © 2003 IBM Corporation

R P G Constructor Example class AS 400 { private String user. Id; private R P G Constructor Example class AS 400 { private String user. Id; private String password; Java AS 400() // default constructor AS 400 { this("PHIL", "GREATGUY"); } AS 400(String user. Id, String password) AS 400 { this. user. Id = user. Id; this. password = password; } } // end AS 400 class use "this()" to call another constructor AS 400 host 1 = new AS 400(); // Call default constructor AS 400 host 2 = new AS 400("GEORGE", "OKGUY"); // Two parm ctor © 2003 IBM Corporation

R P Main: RPG and Java G What RPG IV Java How called CALL R P Main: RPG and Java G What RPG IV Java How called CALL command java command What compile unit gets control first *MODULE without class identified on NOMAIN keyword java command What code gets control first C-specs main method l. Java's main method must look like: public static void main(String args[]) {. . . Array of Strings: } parameters from so JVM can call nothing returned so JVM doesn't have to instantiate class command line JVM looks for this name © 2003 IBM Corporation

R P G Writing To Console * Prototype of this program main entry DMAIN R P G Writing To Console * Prototype of this program main entry DMAIN PR EXTPGM('HWORLD') D STRING 1000 A OPTIONS(*VARSIZE) * Definition of this program main entry DMAIN PI D STRING 1000 A OPTIONS(*VARSIZE) * Global variables DOut. String S 52 A * Main logic C EVAL Out. String = 'Input: ' + C %TRIMR(%SUBST(STRING: 1: 45)) C Out. String DSPLY * End of program C MOVE *ON *INLR RPG IV // main class public class Hello. World { // main method public static void main(String[] args) { // print first parameter passed System. out. println("Input: " + args[0]); } } © 2003 IBM Corporation Java

R P G Review public class Time { private int hour, minute, second; Instance R P G Review public class Time { private int hour, minute, second; Instance variables public Time(int hour, int minute, int second) Time { this. hour = hour; this. minute = minute; this. second = second; } public String to. String() { return "Time: " + hour + ", " + minute + ", " + second; } public static void main(String args[]) { Object reference Time toronto. Time = new Time(08, 30, 0); Time variables Time san. Fran. Time = new Time(05, 30, 0); Time System. out. println(toronto. Time); System. out. println(san. Fran. Time); Often, main is used for } testing non-initial classes Java } © 2003 IBM Corporation

R P G Arrays in RPG COMPILE TIME . . . +. . . R P G Arrays in RPG COMPILE TIME . . . +. . . 1. . . +. . . 2. . . +. . . 3. . . +. . . 4. . . +. . . 5. . . +. . . D NAMES 20 A DIM(5) CTDATA D PERRCD(1) D SALARYS 5 P 0 DIM(5) FROMFILE(X) D PERRCD(10) D SSNumber 9 B 0 DIM(30) D* D*-------------------------PRERUN C* : TIME RUN C* RPG CODE C* : TIME C* : O* COMPILE O* END OF SOURCE CODE **CTDATA NAMES TIME George ARRAY Phil Sandra DATA Jennifer Angelica RPG IV © 2003 IBM Corporation

R P G Arrays in Java Ÿ Declaration int this. Array[ ]; long another. R P G Arrays in Java Ÿ Declaration int this. Array[ ]; long another. Array[ ] [ ]; char[ ] or. This. One[ ]; Java Ÿ Declaration and Definition int this. Array [ ] = new int[1000]; long another. Array [ ][ ] = new long[10]; char[ ] or. This. One[ ] = new char [20]; ƒ ƒ Spacing not important # bracket pairs = # dimensions Type is same for all elements Arrays are objects! Require new #elements [ ] versus ( ) © 2003 IBM Corporation

R P G Run Time Init'n class Test. Multi. Array. RT length is array R P G Run Time Init'n class Test. Multi. Array. RT length is array { instance variable public static void main(String args[]) { int rt. Array[][] = new int[3][3]; // Two dim array int value = 1; // Loop through all rows. . . for (int x. Idx=0; x. Idx < rt. Array. length; x. Idx++) { // Loop through all columns. . . for (int y. Idx=0; y. Idx < rt. Array[x. Idx]. length; y. Idx++) arrays are { rt. Array[x. Idx][y. Idx] = value++; // assign and incr't zero-based in System. out. print(rt. Array[x. Idx][y. Idx] + " "); Java! } // end inner for loop System. out. println(); } // end outer for loop ss var] to acce Use [ } // end main method elements } // end Test. Multi. Array. RT class © 2003 IBM Corporation

R P G Compile Time Init'n l. Java allows initializing at declaration time (compile R P G Compile Time Init'n l. Java allows initializing at declaration time (compile time): String employee[] = {"ABC", "DEF", "GHI", "JKL"}; Note: String objects are covered shortly employee[0] employee[1] employee[2] employee[3] = = "ABC" "DEF" "GHI" "JKL" l. Special Java syntax: ƒ Values specified between curly braces Java ƒ Semi-colon needed after last brace ƒ Values for each element separated by commas ƒ No need to use new operator (implied) © 2003 IBM Corporation

R P G String Basics Ÿ Strings are objects in Java ƒ of the R P G String Basics Ÿ Strings are objects in Java ƒ of the class String (in java. lang package) Ÿ Language has special support: ƒ You can concatenate with the "+" operator ƒ You don't have to use the new operator String text 1 = new String("George"); String text 2 = new String("Phil"); String final. Text = new String(text 1); final. Text = final. Text. concat(" and "); final. Text = final. Text. concat(text 2); OR String text 1 = "George"; String text 2 = "Phil"; String final. Text = text 1 + " and " + text 2; © 2003 IBM Corporation

R P G RPG o/c Strings: Java vs RPG Description Java Method(s) built-in Concatenate R P G RPG o/c Strings: Java vs RPG Description Java Method(s) built-in Concatenate two strings CAT (or '+') concat(string) or '+' operator SUBST %SUBST Extract a substring from a string substring(int start, int end) or substring(int start) SCAN %SCAN Scan for a substring index. Of() %TRIM Trim begin, end blanks trim() %LEN Return length of string length() XLATE Translate a string Not Available CHECK Check for characters Not Available CHECKR Check in reverse Not Available %TRIML Trim leading blanks Not Available %TRIMR Trim trailing blanks Not Available %CHAR V 4 R 2. Converts to string value. Of(datatype value) in String class %REPLACE (V 4 R 2) Allows replacement of substring Not Available © 2003 IBM Corporation

R P G Some String Methods METHOD DESCRIPTION compare. To Compares two Strings lexicographically R P G Some String Methods METHOD DESCRIPTION compare. To Compares two Strings lexicographically ends. With, starts. With Test if String ends or starts with the specified string equals, equals. Ignore. Case Compares this String to another, ignoring case get. Bytes Convert this String into a byte array get. Chars region. Matches Copies characters from this substring into the destination character array Tests if two String regions are equal to. Char. Array Converts this String to a new character array to. Lower. Case Converts all characters in String to lower case to. Upper. Case value. Of Converts all characters in String to upper case Converts primitive data type value to a String (this is a static method) © 2003 IBM Corporation

R P G Class Java Date / Time Package Description Date java. util Simple R P G Class Java Date / Time Package Description Date java. util Simple date/time capture. No manipulation methods Gregorian. Calendar java. util Rich date/time functionality, including comparing, adding, subtracting, extracting Simple. Date. Format java. text For creating "formatting objects" that will format any given Date object to the specified format pattern Time. Zone java. util For creating timezone objects representing any timezone. Apply to Gregorian. Calendar or Simple. Date. Format objects to get equivalent date/time in that timezone Java © 2003 IBM Corporation

R P G OO: Inheritance // class Employee public class Employee { string name; R P G OO: Inheritance // class Employee public class Employee { string name; int salary; ƒ public Employee(string id, int sal) { name = id; salary = sal; } ƒ public void print. Employee() { System. out. print("My name is"+name); } } // class Sub. Employee public class Sub. Employee extends Employee { Child class extends parent class ƒ inherits methods, variables Child can also: ƒAdd new methods, variables ƒOverride methods Java } © 2003 IBM Corporation

R P G OO: Inheritance No limit to inheritance tree ƒ All child classes R P G OO: Inheritance No limit to inheritance tree ƒ All child classes inherit methods of all parents Class Animal Ÿ eat() Ÿ sleep() Ÿ talk() extends Just remember ƒ You can only extend one class ƒ Only one immediate parent Ÿ But grandparents allowed Class Cat Ÿ talk() { println("meow"); } Ÿ purr() Tom. Cat gruff = new Tom. Cat(); gruff. eat(); gruff. talk(); gruff. purr(); extends Java Class Tom. Cat Ÿ talk() { println(“grr"); } Ÿ hiss() © 2003 IBM Corporation

R P G Thanks for listening!! © 2003 IBM Corporation R P G Thanks for listening!! © 2003 IBM Corporation

R P G Agenda More Information © 2003 IBM Corporation R P G Agenda More Information © 2003 IBM Corporation

R P G Websites Website URL Description www. ibm. com/software/awdtools/wds 400 IBM Web. Sphere R P G Websites Website URL Description www. ibm. com/software/awdtools/wds 400 IBM Web. Sphere Development Studio for i. Series www. ibm. com/websphere/developer Web. Sphere Developer Domain www. ibm. com/java www. ibm. com/iseries/java IBM Java www. ibm. com/websphere www. ibm. com/iseries/websphere IBM Web. Sphere IBM i. Series Web. Sphere www. java. sun. com/products Sun Java www. ibm. com/iseries/infocenter. html www. ibm. com/rochester/as 400 bks IBM i. Series online books and help www. ibm. com/redbooks IBM Redbooks © 2003 IBM Corporation

R P G Books Book, URL By, ISBN -Java for RPG Programmers -Java for R P G Books Book, URL By, ISBN -Java for RPG Programmers -Java for COBOL Programmers -Student Work. Book for Java for RPG and COBOL Programmers www. mcpressonline. com/ibmpress Phil Coulthard, George Farr. IBM Press. JAVA and the AS/400 www. 29 th. Street. Press. com Daniel Darnell. ISBN 1 -58304 -033 -1 Java Application Strategies for i. Series and AS/400 2 nd Edition www. mc-store. com/mc-store/ Don Denoncourt. ISBN 1 -58347 -025 -5 Core Java series www. amazon. com Now a series of 3 books. Horstmann, Cornell Teach Yourself Java in 21 Days www. amazon. com Rogers Cadenhead, Laura Lamay. ISBN 1575213907 © 2003 IBM Corporation

R P G Trademarks & Disclaimers 8 IBM Corporation 1994 -2003. All rights reserved. R P G Trademarks & Disclaimers 8 IBM Corporation 1994 -2003. All rights reserved. References in this document to IBM products or services do not imply that IBM intends to make them available in every country. The following terms are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both: AS/400 e e (logo) business IBM(logo) i. Series OS/400 Lotus, Freelance Graphics, and Word Pro are registered trademarks of Lotus Development Corporation and/or IBM Corporation. Domino is a trademark of Lotus Development Corporation and/or IBM Corporation. C-bus is a trademark of Corollary, Inc. in the United States, other countries, or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Microsoft, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. Action. Media, LANDesk, MMX, Pentium and Pro. Share trademarks of Intel Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. SET and the SET Logo are trademarks owned by SET Secure Electronic Transaction LLC. Other company, product and service names may be trademarks or service marks of others. Information is provided "AS IS" without warranty of any kind. All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics may vary by customer. Information in this presentation concerning non-IBM products was obtained from a supplier of these products, published announcement material, or other publicly available sources and does not constitute an endorsement of such products by IBM. Sources for non-IBM list prices and performance numbers are taken from publicly available information, including vendor announcements and vendor worldwide homepages. IBM has not tested these products and cannot confirm the accuracy of performance, capability, or any other claims related to non-IBM products. Questions on the capability of non-IBM products should be addressed to the supplier of those products. All statements regarding IBM future direction and intent are subject to change or withdrawal without notice, and represent goals and objectives only. Contact your local IBM office or IBM authorized reseller for the full text of the specific Statement of Direction. Some information in this presentation addresses anticipated future capabilities. Such information is not intended as a definitive statement of a commitment to specific levels of performance, function or delivery schedules with respect to any future products. Such commitments are only made in IBM product announcements. The information is presented here to communicate IBM's current investment and development activities as a good faith effort to help with our customers' future planning. Performance is based on measurements and projections using standard IBM benchmarks in a controlled environment. The actual throughput or performance that any user will experience will vary depending upon considerations such as the amount of multiprogramming in the user's job stream, the I/O configuration, the storage configuration, and the workload processed. Therefore, no assurance can be given that an individual user will achieve throughput or performance improvements equivalent to the ratios stated here. Photographs shown are of engineering prototypes. Changes may be incorporated in production models. © 2003 IBM Corporation

R P G Questions? Submit your questions now by clicking on the “Ask A R P G Questions? Submit your questions now by clicking on the “Ask A Question” button in the lower left corner of your presentation screen. Phil will answer your questions shortly after the broadcast. © 2003 IBM Corporation