<<

· Developed at by and .

· Designed as an object-oriented, machine-independent programming language.

· Java is both a compiled and an interpreted language.

Source Code

Java Runtime

Byte Code

UNIX PC Mac

Salient Java Features

· Java source code is compiled into binary instructions (byte-code) for a virtual machine (universal format) whereas C/C++ source code is compiled into native binary code.

· Compiled Java byte-code (J-code) is executed by a Java run-time interpreter.

· Java is portable. The same Java application can run on any platform that provides a Java run-time environment.

· In general, interpreters are slow, but because Java interpreter runs compiled byte-code, Java is a fast interpreted language.

· Fundamental unit of Java code is the class.

· Class is an application component that holds both code and data.

· Java supports only single-inheritance class hierarchy.

· Java is a statically-typed (like C/C++), late-binding (like Smalltalk) language.

· No preprocessor in Java, so no macros, #define's or conditional compilation in Java.

· Java eliminates ad-hoc pointers and adds garbage collection and true arrays to the language.

· Java supports threads.

· Java has a powerful exception-handling mechanism.

· Java provides dynamic memory management.

Java Safety of Implementation

System Resources

Security Manager

Class Loader

Verifier

Java Binary

Byte-code Verifier

Verifies byte-code before they are run and ensures that it obeys basic rules of Java language.

Class Loader

Loads binary classes that contain byte-code.

Security Manager

Restricts access to system resources and is the single point of control to system resources. Instructions to compile and run Java code

Create a file called helloworld.java in a sub-dir called java.

/* My first Java program */ class helloworld { public static void main(String [] args) { System.out.println("Hello World!"); } }

To compile:

javac helloworld.java

(This will create a file called helloworld.class)

To run:

java helloworld

· Website: http://www.javasoft.com/

APPLET

· Applet is a small, subordinate or embeddable application confined within the walls of a (eg. Navigator or Hot Java) or Applet Viewer provided by the JDK.

· An applet can interact only with the user and the host from which it originates.

Hello World Applet

· Using a text editor, create a file named HelloWorld.java in the public_html directory with the Java code shown here: import java.applet.*; import java.awt.*; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 50, 25); } } · Compile the Java source file: javac HelloWorld.java · Create an HTML file called Hello.html that includes the applet. The HTML file should be in the same directory that contains the HelloWorld.class file.

A Simple Program Here is the output of my program:

To run the applet, you need to load the HTML file into an application (eg. Netscape Navigator) that can run Java applets. In the web browser specify the URL: http://www.csis.gvsu.edu/~userid /Hello.html

You can also run the applet in an applet viewing program provided by the JDK. In this case the command is: appletviewer file:/home/userid/java/Hello.html (This assumes you have saved the Hello.html and HelloWorld.class files in the java sub-dir)