|
The Java PlatformThis page provides the background on how Java programs are created and executed — companions to pages on Exception Handling, Java IO, JVM GC, & JDBC, J2EE, and comparing threading & Logic. In 2003, Java replaced C++ in the the College Board's Advanced Placement Computer Science exams. Over 100 high schools are using "Java Au Naturel" free from Dr. William C. Jones Jr. at Central Connecticut State University. | Topics this page:
|
|
SummaryAn intermediate JVM makes the Java language device independent because each JVM on the various platforms interpret byte-code the same way. To optimize Java software performance, the Version 4 Release 2 Operating System on IBM's AS/400 computers has JVM integrated beneath their Machine Interface (MI). On AS/400 V4R2's, Java classes can be run through a "transformer" to compile Java class files into AS/400-dependent object code using the AS/400 Developer Kit (JDK 1.1.4). Microsoft added Java language support after the initial release of their .NET (dot NET) initiative. The Microsoft Visual J#.NET Redistributable Package 1.1 is installed as a separate program. Microsoft's does not make use of J# (pronounced J Sharp) code like other Java implementations. Microsoft has its own compilation and run-time architecture for .NET programs.
|
|
Java Version History
ScalaMany say that the next jump in the Java language is the Scala language, created by the developer of Java Generics, Martin Odersky (Professor at EPFL in Lucerne, Switzerland). Scala uses the same compilation model as Java and .NET (separate compilation, dynamic class loading, etc.), but features functional programming concepts critical for tackling concurrency to achieve scalability. ClojureClojure is a dialect of Lisp that is dynamically compiled into JVM CLR. DownloadsApache Harmony and Open JDK are open-source implementations of Java.
Rather than going to the
Current Release (6.0),
go to
This Java Applet page tells you what version of Java is running on your browser
Get the Java2 SDK, Standard Edition and Create Your First Java Application
One of the frustrations with Java is its backward and upward compatibility. Code that works in one version of Java may not work in a newer version. , as shown in this Errata to the Deital book |
JavaSoft/MS J++ Executables
| IDEs have their own compilers and tools,
UMLGraph by Diomidis Spinellis creates UML class diagrams from Java code using AT&T's Graphviz open-source graph drawing software.
|
Java Documentation
|
.Java Class Source Files and Compilation
(type) Java Stand-Alone ApplicationsJava application programs are called stand-alone because they can be invoked from the operating system by using just the Java interpreter.
Name application program source files with suffix APP.java. In the Microsoft world, file extensions trigger a program to be invoked. But in UNIX, the class file to be run is a parameter and Java automatically appends it for you. So remember NOT to type “.class” when executing java classes. If you do, you will get a nasty message such as:
Examples of how applications can use capabilities of the JRE (Java Run-time Environment):
args[0] is the first argument passed into from the command line. Java AppletsApplets are named as a dimunitive of "App" because they don't need a hook to the command console that the modifier “main()” provides. If you try to run an applet from the command console, you will get error Exception in thread "main" java.lang.NoSuchMethodError: mainApplets run within an internet browser (or AppletViewer during debugging). They are called from a web page using the "hooks" provided by being a subclass derived from the JApplet super (base parent/ancestor) class. Name applet source files with suffix APPLET.java, such as “HelloAPPLET.java”.
import javax.swing.JApplet; // superclass subpackage
import java.awt.Graphics; // core package with paint. public class HelloAPPLET extends java.applet.JApplet {
int intVar1 = Integer.parseInt(getParameter("num1")); super.paint ( g ); // g inherits a version of the paint super method g.drawString ( strText, 25, 25 ); // bottom left coordinates The java.applet package contains the Applet class which has abstract methods init, start, paint, stop, and destroy.
There are no commas between arguments. To dynamically size and resize the applet on the screen, add this Javascript. The Name= parameter is the handle used by other applets to communicate with it, using this code:
getAppletContext().getApplet("receiver")
To flush the class cache during debugging, click Shift-Reload in the browser. If an old browser tries to load an applet containing a method for a newer JDK it doesn't support, a NoSuchMethodError exception is thrown. So applets should anticipate this exception and provide an alternative method. Java ServletsJava Servlets are server-side components, so they don't have/need a graphical user interface. They are used to provide secure web-based access to data (via JDBC), support facilities such as search engines, and dynamic generation of web pages back to web browser clients. They are invoked by a HTML PUT request in an HTML FORM, typically to a file with a .jsp extension. The Java Servlet API is a Standard Java Extension API as an add-on package.Servlet code use the service() method to to handle Servlet requests. |
Java Virtual Machine Detection
<PARAM name="Person" value="Jerry"> </APPLET> If you use IE to hit a web page with Java Applet, but you do not have a JVM installed, you will see this screen
|
The Microsoft JVM
The JVM on Microsoft Windows machines is not compatible with the Sun JVM. (The "Java" supported by Microsoft's .NET runtime does not recognize bytecode) When a Sun VM is installed, it de-selects the Microsoft VM.
However, some applications (such as tree-controls and Rational RUP) were written for and thus need the Microsoft VM enabled. To allow RUP to find the Microsoft VM in a way that doesn't prevent use of the Sun VM by other apps: 1. Open Internet Explorer. 2. Select Tools->Internet Options->Advanced tab. 3. Scroll down to the “Java (Sun)” and “Microsoft VM” sections. 4. De-select the checkbox beside the Java option. 5. Select the checkbox beside “JIT compiler for virtual machine enabled (requires restart)”. 6. In Start > Settings > Control Panel > double-click Java Plug-in > Browser tab, uncheck "Microsoft Internet Expolorer" under the text "Java(TM) Plig-in will be used as the default Java Runtime in the following browser(s):", then click the Reset button.
Uninstalling the MS JVMSleasy software such as CoolWebSearch (CWS) infect Windows machines through the ByteVerifier bug in the Microsoft Java Virtual Machine. Microsoft does have a patch for this vulnerability that is included in Windows XP SP1a.Since Microsoft no longer supports its JVM, Windows users should remove the MS Java VM
After you reboot:
Run Regedit to find and delete:
|
Compiling Packages, Classpath, Package Import
IBM provides its Jikes compiler as a faster replacement to Sun's compiler. Sun's compiler uses a DOS command-line interface to compile a Java class file "pgm1" into byte code:
c:\jdk141\bin\java pgm1 -classpath .;c:\... -verbose -deprecated
The Sun 1.4 Java compiler's run parameters are: Usage: javac <:options> <:source files> where possible options include: -g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -O Optimize; may hinder debugging or enlarge class file -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath <:path> Specify where to find user class files -sourcepath <:path> Specify where to find input source files -bootclasspath <:path> Override location of bootstrap class files -extdirs <:dirs> Override location of installed extensions -d <:directory> Specify where to place generated class files -encoding <:encoding> Specify character encoding used by source files -target <:release> Generate class files for specific VM versionAdd the -g parameter to receive line numbers accompanying a stack trace when an exception is thrown. Similar to C-language .h header files, packages must first be declared using the import command such as:
The classes loaded are shown during compile if the -verbose parameter is added. java.lang is not listed because JVM automatically loads it. A package statement in source code is compiled with the -d option. Any features of the package's classes not explicitly marked with an access modifier will be accessible to all the members of the package. Common causes for error messages from various Java Compilers are described at this site. Some Java compilers, such as IBM Jikes (written by common-source contributors in C++ for Windows, Linux, etc.), may issue more descriptive messages. SQL Within Java (SQLJ)Oracle8i SQLJ Developer's Guide and Reference Release 2 (8.1.6)Running Java Program From Within OracleA java program can be executed within Oracle using these steps from Experts-Exchange:1) Create a java source file. For example, file JavaProc.java class JavaProc { public static void main(String[] arg_in) { system.out.println("Your input is : " + arg_in[0]); } } 2) Load the .java file into the database. For example, using the default scott schema's password tiger and the OCI driver, resolving external class references at compile time:
loadjava -oci8 -resolve -u scott/tiger JavaProc.java
3) Publish the Java code to PL/SQL by creating a PL/SQL wrapper (rather than BEGIN ... END):
CREATE OR REPLACE PROCEDURE javaproc(cmd VARCHAR2) AS LANGUAGE JAVA NAME 'JavaProc.main(java.lang.String[]) return java.lang.String'; / 4) Execute the procedure from within Oracle PL/SQL SQL> set serverout on (to enable use of DBMS_OUTPUT) Server Output ON SQL> call dbms_java.set_output(2000); maximum is 1000000 for 1MB Statement processed. SQL> exec JavaProc('Hello'); Statement processed. Your input is : Hello Instead of granting specific privileges, several roles are available for Java operations: JAVAUSERPRIV (read I/O operations) and JAVASYSPRIV (write IO operations), JAVA_ADMIN, JAVAIDPRIV, JAVADEBUGPRIV 5) To remove the procedure or function from within Oracle PL/SQL:
ALTER JAVA -- compile Java source & resolve Java class references.
DROP JAVA -- drop a named Java library unit |
Compressed .jar .ear .rar Files
.jar files contain regular java classes.
The classpath environment variable points to the .jar files. For efficient downloading of java applets to a Java-enabled Web browser, multiple .class files are compressed together into a single .jar file. Only a single HTTP connection in the communications overhead. This is done using the jar.exe Java archive tool. It is similar to the PKZIP program for MS-DOS developed by Phil Katz. Both use the ZLIB compression library (RFC 1950) A listing of files in a jar file include the META-INF/MANIFEST.MF entry for the manifest of files in the file. To ensure non-repudiation by the author, Jar files can be signed with a digital certificate issued to the author from a trusted third party CA. Such trusted jar files may be the only files allowed to be downloaded by a local Java security policy. |
Advanced Placement Computer Science Java Subset API by Owen L. Astrachan Java Methods textbook by Maria Litvin and Gary Litvin
|
Debugging with jde.exe
> ?
To set a breakpoint in class file pgm's main method:
> stop in pgm.main
To execute until the breakpoint:
> run
For a listing of the source at the => position:
[] list
cont Source-code analysis tools find the most obvious flaws in code. Packages include:
According to an April 2004 report from the National Cybersecurity Partnership's Working Group on the Software Lifecycle, which cited an analysis of development methods by the Software Engineering Institute at Carnegie Mellon University: “Commercial code typically has anywhere from one to seven bugs per 1,000 lines of code.” Coverity's analysis of MySQL found an average of one bug in every 4,000 lines of code. Coverity audit of the Linux kernel found 985 flaws in 5.7 million lines of code (less than one flaw in every 10,000 lines of code).
| On-Line tutorialsData TypesEnterprise Programming Programming Languages DOS vs. Linux OS Commands The On To Java by Patrick Henry Winston & Sundar Narasimhan invokes sophisticated applets to illustrate their examples. How To Think Like A Computer Scientist - Java Version by Allen B. Downey Java Lab Activites by Roger Frank, Computer Science teacher at Ponderosa High School in Parker, Colorado AP Computer Science Teaching Resources by Alyce Brady, Kalamazoo College
|
Java IDEs (Integrated Development Environments)
For those who just want a better editor than Notepad (which appends “.txt” to the end of file names):
|
How IDEs save time:
|
Utilities For Java Source Code
|
|
Constructor Instantiating Classes; Accessing Methods
JPanel northPanel = new JPanel();
Default constructors are inserted only if one has not been provided. Instantiation makes available public features (methods, member variables, and properties). In the OO world, it is not data but methods that are the focus of work. So data is obtained by invoking methods (using a dot operator between class and method names):
Int curLnF = UIManager.getSystemLookAndFeelClassName();
Names of Classes have a capital initial character, just like primitive data types such as Int, String, etc. because in the OO world a class is a type of data and used the same way.
Int style; // Integer
String class_name = "JavaGUI1"; // String object label = new JLabel( "This is a Metal look-and-feel", SwingConstants.CENTER ); northPanel.add( label );
public class JavaGUI1 extends JFrame {
|
PackagesEach package provide a scope for access protection by grouping classes for high-level functional cohesion. By providing a name space that helps prevent name clashes, packages help reduce change impact when releasing. Each package has defined classes, interfaces, and exceptions. When class files are created, they must be placed in a directory hierarchy that reflects their package names. Package names should contain all lowercase alphanumeric characters, starting with an internet domain code or two-letter ISO country code such as
com.visigenic. com.inprise.vbroker.CORBA. com.visigenic.vbroker.IOP. com.entrust.toolkit.CertificateSet.class com.fourthpass. To be sure that each component of a package name hierarchy is a legitimate directory name on all platforms, don't name classes containing a space, forward slash, backslash, or other symbols. packages used only during testing not belonging to a production build should be preceded with "test." so that they can be quickly identiifed for exclusion. |
The “x” in javax.swing indicates an 'extension" to the core Java libraries not supported by every implementation.
|
Mo' Class Libraries
|
Due to a bug in Communicator, you must hold down the shift key and then click the link to download class files.) |
Methods
Defining a method with the same name as already defined within a class overrides that class. The overriden method must use identical signature (argument types and parameter order and with identical return type). In other words, it is illegal to overload a library method. However, a class can redefine methods with different signatures (overload) methods. By industry convention, the main (entry point) class is usually coded at the bottom of its class code source file. When a class is specified without a method, the default method is invoked. To reference the original (base) method before subclassing or overriding, Java uses the keyword "super" as the first statement of the constructor:
super.CalculateX( "heading" );
C# uses "base" instead of "super". C++, however, requires explicit specification of the class name because C++ allows multiple inheritance. Anything declared inside a method is not a member of the class, but is local to the method. A level in the class inheritance hierarchy cannot be bypassed. If three classes, A, B, and C, all define a method m(), and they are all part of a hierarchy-so that B extends A and C extends B-then the method m() in class C cannot directly invoke the method m() in class A. Formal computer terminology describes Java as a programming language which passes by value, where methods get copies of their arguments' values and have no access to the original values. When a caller calls a method and execution flows into the method's body, the JVM copies the argument values provided by the caller and gives the copies to the method. If the method alters its arguments, the alteration has no effect on the caller. The alternative is called passing by reference, where methods work with the caller's data and not with copies,
| Harvy and Paul Deitels' Fourth Edition for Java 1.3 The Errata from publisher Prentice Hall Pearson)
|
Interfaces of methods in abstract classes
An interface contains no implementation -- a semicolon apears in place of the method body. An interface can only have static final constants -- no fields to be instantiated. Like an abstract class, all methods of an interface are implicitly abstract even if the abstract modifier is omitted. Classes encapsulate both abstract data types (such as a size, position, date) and their interface, which is a collection (a set) of public methods of a class. An interface allows the use of OO polymorphism by providing common method names for all implementations. Classes are abstract because they are defined with no details about how they are implemented, just their types of behaviors and characteristics. An abstract class can only be subclassed, not instantiated. Java operates from a model-view-controller (MVC) paradigm. Registered observers are updated on changes to observable objects.
| This I think is the best bargain: $15 Java in a Nutshell: A Desktop Quick Reference, 3rd Edition by David Flanagan (O'Reilly: 1999) Marcus Green on the SCJP Objective 2, Declaring classes and variables.
|
Object Identifier Naming & Duration
Identifiers must start with a letter, $ or _ (underline), but not a number.
The state of an object is determined by the values stored in its variables. An identifier's duration in memory is also called its lifetime.
The keyword this. is used to reference local variables named the same as variables defined at the class instance level. This is not valid for static methods. An identifier's scope defines where the identifier can be referenced within a program.
|
Modifier Keywords
Class Access Visibility Modifier Keywords
Java does not support the C++ function to designate a friend of a class which has direct access to all the private and protected members of a member function (method) of another class or a stand-alone function.
|
Object modifiersIn addition to keywords main, void, static defined above:
Inner Classes
Unlike non-static nested classes,
static nested classes can be accessed without having an instance of the outer class.
The synchronized modifier is used to control access to critical code in multi-threaded programs. |
Java Developer Resources
Certification |
My Own Style Guidelines
|
Related:
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |