JE_IBM - Java101 - Basic Syntax and Structure
JE_IBM - Java101 - Basic Syntax and Structure
Java™ 101:
Basic Syntax and Structure
Osman Omer
([email protected])
1
IBM System i
Outline
• Introduction
• Why Java™?
• Object-Oriented Overview
• Java Keywords and Definitions
• Elementary Java Structure
• Java Syntax and Control Flow
• Compiling and executing Java Code
• Tips for Approaching Java Code
• Tools for Java Development
IBM System i
Introduction
• Goals
– Introduce basic Java syntax elements
– Compare control flow constructs with those in RPG
– Develop skills for reading and understanding Java source code
2
IBM System i
IBM System i
OO in 5 Minutes or Less
• Class
– A pattern, template, or model for an object
– Existence is independent of any single VM
• Stored ‘externally’ in files in the filesystem
• Classes can define “inner” classes since Java2
– Invariant (constant) class data shareable
• Object
– Defn: combination of data and methods
– An instance of a class
– Existence depends on a VM to “hold” the object
• Data
– The fields of an object (attributes, characteristics)
• Methods
– The functions of an object (procedures, subroutines)
3
IBM System i
Source code
Java Definitions
bytecode
• Classfile
– A file in the hierarchical file system JVM
– Contains Java "object code" (a.k.a "bytecode")
– Result of compiling Java source code Executable
• Jar File
– Java ARchive; a collection (zipfile) of classfiles and other resources
• Virtual Machine for Java (VM)
– Software that loads and executes bytecode
• Classpath
– Locations searched by the VM for classes and other resources
• Package
– Collection of related classes
– Provides access protection and name space management
IBM System i
Java Keywords
4
IBM System i
• FROM reuse
– Everybody codes "from reuse" in Java
• …every time you use a java.lang.String, for instance
– Typically, “applications” are strictly FROM reuse
• FOR reuse
– Designing classes to be used by others (i.e. API)
• Up-front design work fundamentally important
• Migration paths, versioning, encapsulation
– Definitely “advanced” Java programming
IBM System i
Caveats
• We only have 1¼ hours total – just time for the "high points"
– The complete Java language "specification" (JLS) is available here:
• http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
– This specification includes a complete "grammar" for Java
• Formal specification of keywords and their valid relationships
5
IBM System i
IBM System i
• Remember:
– Java classes define the methods and fields of an object
• Classes:
– May declare fields and methods (explicitly within the braces)
– Will inherit any fields and methods of their superclasses
6
IBM System i
} // End of ExampleClass
IBM System i
} // End of ExampleClass
7
IBM System i
• Example: keep a count of all cars in this VM (extra credit for finding the bug…)
class Car {
static int numCars; // static field to hold count of cars
String myMake; // instance's make
String myModel; // instance's model
int myModelYear; // instance's year
IBM System i
8
IBM System i
IBM System i
9
IBM System i
IBM System i
• Conditional
– Branches and loops
• Exceptional
– try, catch, throw
• Unconditional
– Method calls
10
IBM System i
•if/else
– executes block if expression
evaluates to “true”
IBM System i
11
IBM System i
• switch
– An easy-to-read collection of if
statements
– Use the break keyword to
transfer control to just after the
switch statement
– Argument to switch() must
be scalar value (i.e. integer or
character)
IBM System i
12
IBM System i
Condition F
• do … while End
– do { statements } while ( condition ) ; Start
T Condition
F
End
IBM System i
13
IBM System i
• try/catch
– Errors are propagated up the
stack
– Always list catch blocks from
most specific to most general
– The finally statement is always
executed
– Must re-throw exception using
the throw keyword if not
handled
IBM System i
14
IBM System i
import javax.swing.*;
class MyWhy {
public static void main( String[] args ) {
String question = "Why \"i\"?";
String answer = showDialog("Question!", question);
System.out.println( question + " " + answer );
System.exit(0);
}
IBM System i
15
IBM System i
IBM System i
• Check packaging
– Jar or classes
– Does it include the source?
• API or Application
– If it is an API, evaluate the interface
– If it is an Application, look for main and run it
• Look at documentation
– Is there a javadoc? (Should be for API!)
16
IBM System i
• Modeling
– Unified Modeling Language (UML)
– Rational Rose XDE
• Decompilers
– DJ Java Decompiler
– jShrink
IBM System i
Summary
• Java Resources
– Sun’s Java Website (java.sun.com)
– IBM developerWorks
– IBM Toolbox for Java
17
IBM System i
// file: Mydu.java
import java.io.*;
Usage: Mydu [-a | -s] [-k | -b] [filename …]
import java.util.*;
// constructor (<init>)
public Mydu(String[] args) { cmdArgs = parseArgsRemoveFlags(args); }
IBM System i
18
IBM System i
} catch(IOException ioe) {
throw new RuntimeException("Error in recurse", ioe);
} finally {
return accumulator;
}
} // end of recurse
IBM System i
19
IBM System i
References
IBM System i
Trademarks of International Business Machines Corporation in the United States, other countries, or both can be found on the World Wide Web at
http://www.ibm.com/legal/copytrade.shtml.
Intel, Intel logo, Intel Inside, Intel Inside logo, Intel Centrino, Intel Centrino logo, Celeron, Intel Xeon, Intel SpeedStep, Itanium, and Pentium are trademarks or registered
trademarks of Intel Corporation or its subsidiaries in the United States and other countries.
Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both.
Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft 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.
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both.
Other company, product, or service names may be trademarks or service marks of others.
The 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 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.
Some information 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 may be engineering prototypes. Changes may be incorporated in production models.
20
IBM System i
Backup Slides
IBM System i
2 2 C 16.616% -1.75% A
8 7 C# 3.171% -0.11% A
21
IBM System i
IBM System i
javadoc
java/lang/Object.java
/**
* Class <code>Object</code>
* Every class has <code>Obj
* . . .
javadoc
22