requirements - portability, extremely reliable
1994/95 HotJava - first web browser which supported
Java applets - small Java programs that can be
included in HTML file
Supported and Marketed by Sun Microsystems
free for non-commercial use
Java Development Kit (JDK) available for Solaris, Windows (95/NT), Linux, Mac.
Current version is JDK 1.5.
In OO the focus is on the data in the application and methods (procedure) that manipulate data.
You may need to change your programming style (from purely procedural to OO).
It uses similar language constructs as C.
Java does not use - pointers, records (struct in C), GOTO, etc.
Java supports - arrays, strings, automatic garbage collection, etc.

------------------------------------------------------
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
------------------------------------------------------
% javac HelloWorldApp.java
% java HelloWorldApp
----------------------------------
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld2 extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
----------------------------------
This HTML file should contain the following text:
----------------------------------------- <HEAD> <TITLE> A Simple Program </TITLE> </HEAD> <BODY> Here is the output of my program: <APPLET CODE="HelloWorld2.class" WIDTH=150 HEIGHT=25> </APPLET> </BODY> </HTML> ------------------------------------------Load the HTML File into Java-compatible browser (NetScape).
OR
you can use the Applet Viewer provided in the JDK as below,
% appletviewer Hello2.html
whereas in Java - programming is object-oriented.
In procedural programming,
Each class contains data as well as the set of methods (procedures) that manipulate the data.
An instance of a user-defined type (i.e. a class) is called an
object.
Objects have the property of information hiding .
Instead of defining completely (separate) new class, the programmer can designate that the new class is to inherit attributes and behaviours of the existing class (called superclass). The new class is referred to as subclass.
Programmer can add more attributes and behaviours to the subclass, hence, normally subclasses have more features than their superclasses.
Inheritance relationships form tree-like hierarchical structures.
For example,
"Is-a" relationship
For example, UndergraduateStudent can be treated as Student too.
You should use inheritance to model "is-a" relationship.
Important : Don't use inheritance unless all inherited data and methods make sense.
For example, mathematically a circle is-a (an) oval, however you should
not inherit a class circle from a class oval. A class oval can have one
method to set width and another to set height.
For example, a Rectangle Is-NOT-a Line. However, we may use a Line to draw a Rectangle.
The "has-a" relationship is quite different from an "is-a" relationship. "Has-a" relationships are examples of creating new classes by composition of existing classes (as oppose to extending classes).
Think carefully about the functionality (methods) a class should offer.
Always try to keep data private (local).
Consider different ways an object may be created.
Creating an object may require different actions such as initializations.
Always initialize data.
If the object is no longer in use, free up all the associated resources.
For example,
a circle can be described by the x, y position of its centre and by its radius.
We can define some useful methods for circles,
// The class of circles (partially defined)
public class Circle {
public double x, y; public double r;
// Methods that return the circumference and area of the circle
public double circumference( ) {
return 2 * 3.14159 * r ;
}
public double area ( ) {
return 3.14159 * r * r ;
}
}
For example,
Circle c ; c = new Circle();
Circle d = new Circle();
For example,
Circle c = new Circle(); //initialize our circle to have centre (2, 5) and radius 1.0. c.x = 2.0; c.y = 5.0; c.r = 1.0;
// let's create another circle Circle d = new Circle(); //initialize this circle to centre (10, 7) and radius 4.0. d.x = 10.0; d.y = 7.0; d.r = 1.0;
Circle c = new Circle(); double a; c.r = 2.5; a = c.area();
//Note that it is not a = area(c)
This can be achieved in at least 3 different ways ….
First approach ….
// The class of graphical circles
public class GraphicalCircle {
public double x, y; public double r; public Color outline, fill;
public double circumference( ) {
return 2 * 3.14159 * r ;
}
public double area ( ) {
return 3.14159 * r * r ;
}
public void draw(Graphics g) {
g.setColor(fill);
g.fillOval(x-r, y-r, 2*r, 2*r);
g.setColor(outline);
g.drawOval(x-r, y-r, 2*r, 2*r);
}
}
In this approach we are creating the new separate class for GraphicalCircle
and re-writing the code already available in the class Circle.
Hence this approach is NOT elegant, in fact it is the WORST possible solution. Second approach ……
public class GraphicalCircle {
public Circle c; // here's the math circle
// Here are the old methods
public double area() { return c.area(); }
public double circumference(){ return c.circumference(); }
//The new graphics variables and methods go here public Color outline, fill;
public void draw(Graphics g) {
g.setColor(outline);
g.drawOval(c.x-c.r, c.y-c.r, 2*c.r, 2*c.r);
g.setColor(fill);
g.fillOval(c.x-c.r, c.y-c.r, 2*c.r, 2*c.r);
}
}The above approach uses "Has-a" relationship. That means, a GraphicalCircle has a (mathematical) Circle.
It uses methods from the class Circle (area and circumference) to define some of the new methods. This technique is also known as method forwarding.
This approach is also NOT elegant, as it is possible in this example to use "Is-a" relationship instead of "Has-a" relationship (see third approach). Third approach ……
public class GraphicCircle extends Circle {
Color outline, fill;
public void draw(Graphics g) {
g.setColor(outline);
g.drawOval(x-r, y-r, 2*r, 2*r);
g.setColor(fill);
g.fillOval(x-r, y-r, 2*r, 2*r);
}
}The subclass GraphicCircle inherits all the variables and methods of its superclass Circle.
The above approach provides the most elegant solution.
Example,
GraphicCircle gc; ... ... double area = gc.area();We can assign an instance of GraphicCircle to a Circle variable.
Example,
GraphicCircle gc; ... ... Circle c = gc
If we don't define the superclass, by default, the superclass is the class Object.
Object Class :