| 1.124 Lecture 11 | 10/17/2000 |
| Java compiler | Java interpreter | |||
| myprog.java | --------------> | myprog.class | ----------------> | Program output |
| javac | java
appletviewer netscape |
It takes time to learn everything about Java and it is important to set your expectations accordingly. There are two main challenges:
The Java language is still evolving. We will be using the Java 2 platform, which is also known as the Java Development Kit (JDK). The latest release is Java 2 version 1.3. The default version on Athena is 1.3.0 for Sun and Linux, and 1.2.2 for SGI. To access the Java locker on Athena, type
add -f java (Sun)
add java (SGI, Linux)
Be prepared to encounter bugs in the implementation of the language
from time to time. This includes inconsistencies across hardware
platforms. Also note that the latest version of Java is not supported
by the Netscape browser.
An applet is a program that can be embedded in an HTML page. The program can be run by loading the page into a Java-enabled browser. The JDK includes a tool, called appletviewer, that can also be used to view applets.
A Java program can be designed to function
javac HelloWorld.java
This will produce a file named HelloWorldApp.class.
We can run the program as application by typing
java HelloWorldApp
The command line interpreter looks for a function named main() in the HelloWorldApp class and then executes it.
Points to note:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello
World!");
}
}
The JApplet class possesses a method named paint(), which it inherits from one of its superclasses. Our HelloWorld class inherits this paint() method when it inherits the JApplet class. The purpose of paint() is to draw the contents of the applet. Unfortunately, the default paint() method that we inherit cannot do anything useful since it has no way of knowing what we want to draw. We must therefore override the default paint() in our HelloWorld class. (Note that while C++ requires the use of the virtual keyword to indicate function overriding, Java does not require us to inform the compiler that overriding will take place.)
The paint() method receives as an argument a Graphics object, which contains information about where and how we can draw. In this example, we choose to draw the text "Hello World!" at coordinates (50,25) by calling the drawString method.
We can compile our program by typing
javac HelloWorld.java
This produces a file named HelloWorld.class. We now embed our applet in an HTML file, Hello.html, and we can run it by typing
appletviewer Hello.html
We can also view Hello.html in a Java-enabled browser.
HelloWorld.java
import javax.swing.JApplet;
import java.awt.Graphics;
public class HelloWorld extends JApplet {
public void paint(Graphics g) {
g.drawString("Hello world!",
50, 25);
}
}
Hello.html
<HTML>
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
Here is a list of primitive data types.
| Primitive data type | Size in bytes/Format |
|---|---|
| byte | 1 |
| char, short | 2 |
| int, float | 4 |
| long, double | 8 |
| boolean | true or false |
Reference data types include arrays and classes. Here is an example of a Line class.
Line.java
class Line {
private int miX1, miX2, miY1, miY2;
public Line() {
miX1 = miX2 = miY1 =
miY2 = 0;
}
public Line(int iX1, int iX2, int iY1, int iY2)
{
miX1 = iX1;
miX2 = iX2;
miY1 = iY1;
miY2 = iY2;
}
}
Line line;
// Declaration of object (does not create object.)
line = new Line();
// Instantiation of object.
Finalization and garbarge collection happen asynchronously in the background. It is also possible to force these tasks to occur using the System.runFinalization() and System.gc() commands.
A finalizer has the form
protected void finalize() throws Throwable {
...
// Clean
up code for this class here.
...
super.finalize();
// Call the superclass's finalizer (if provided.)
}
class SubClassName extends SuperClassName {
...
}
If a superclass name is not specified, the superclass is assumed to
be java.lang.Object. Also, note that each class can
have only one immediate superclass i.e. Java does not support multiple
inheritance.
We can create a package by placing a package statement at the
top of every source file that defines a class belonging to the package.
We may later use the classes in the package by placing an import
statement at the top of the source file that needs to access the classes
in the package.
graphics/Line.java (Path of the file is relative to the CLASSPATH environment variable.)
package graphics; // Class Line belongs to package graphics.
public class Line { // The public class
modifier makes this class accessible outside the package.
...
}
MyTest.java
import graphics.*; // Provides access to all public classes in package graphics.
class MyTest {
public static void main(String[] args) {
Line line;
graphics.Line line2;
// Can be used for conflict resolution if two packages have a Line class.
line = new Line(0,0,3,4);
line2 = new Line();
}
}
If a package name is not specified for a class, then the class belongs to the default package. The default package has no name and it is always imported.
Here are a few of the core Java packages:
class Access {
private privateMethod();
// Access level is "private".
protected protectedMethod();
// Access level is "protected".
public publicMethod();
// Access level is "public".
packageMethod();
// Access level is "package".
}
| Access specifier | Acessible by | |||
|---|---|---|---|---|
| Class definition | Subclass definition | Rest of Package | Rest of World | |
| private | Yes | No | No | No |
| protected | Yes | Yes | Yes | No |
| public | Yes | Yes | Yes | Yes |
| none i.e. package | Yes | No | Yes | No |
class MyPoint {
int x;
int y;
static int x_origin;
static int y_origin;
}
In this example, every object has its own x member, however,
all objects share a single x_origin member.
class Avo {
final double AVOGADRO = 6.023e23;
}
public class Line { // The public class
modifier makes this class accessible outside the package.
...
}
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX,
int newY) {
...
}
abstract void draw();
// This means that the class must be made abstract.
}
class Circle extends GraphicObject {
void draw() {
...
}
}
final class String {
...
}
It is also possible to make individual methods final.