JAVA!
Java Hello World
As an Application...
class HelloWorldApp {
public static void main (String args[]) {
System.out.println("Hello World!");
}
}
Java Hello World
As an Applet...
import java.awt.Graphics;
import java.applet.Applet;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
Referencing it in 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>
Building a Class in Java
class Motorcycle {
String make;
String color;
boolean engineState;
void startEngine()
if (engineState == true)
System.out.println("The engine is already on.");
else {
engineState = true;
System.out.println("The engine is now on.");
}
}
public static void main(String args[]) {
Motorcycle m = new Motorcycle();
m.make = "Yamaha RZ350";
m.color="Yellow";
m.startEngine();
}
}
Issues in Java class structures and behaviors
Terms: methods, classes, subclasses, superclasses
Single inheritance
"extends"
No "virtual" - Smalltalk-like overriding
Can define a "package"
a way of grouping together related classes and interfaces (a
collection of method names, without actual definitions, that indicate
that a class has a set of behaviors in addition to the behaviors
the class gets from its superclasess.)
Issues in Java data
Real Strings and booleans
"fred" + " " + "jones"
No pointers!
All object references are Smalltalk-like references
Dot-notation goes further
myobject.instance
myobject.instance.its_instance
myobject.getClass().getName()
Class methods and data are "static"
Java Class Libraries
java.lang: Object, String, System, Integer, Character, Float,
etc.
java.util: Date, Vector, Hashtable
java.io
java.net: Socket, URL
java.awt (Abstract Window Toolkit): Window, Menu, Button,
Font, Checkbox
java.applet: Applet, AudioClip
A More Complex Example:
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + "
chars.");
}
}
Using the command line
import java.io.*;
class CountFile {
public static void main(String args[])
throws java.io.IOException, java.io.FileNotFoundException
{
int count = 0;
InputStream is;
String filename;
if (args.length == 1) {
is = new FileInputStream(args[0]);
filename = args[0];
} else {
is = System.in;
filename = "Input";
}
while (is.read() != -1)
count++;
System.out.println(filename + " has " + count + "
chars.");
}
}
Exceptions
public static void main(String args[])
throws java.io.IOException
An exception is an event that occurs
during the execution of program that prevents the continuation
of the normal flow of instructions.
In Java, when an error occurs, the program
throws an exception. You can catch exceptions and try to handle
them within a special code segment known as an exception handler.
Differences between Java and
C++
Not const but "final"
Not ~class but "finalize()"
No pointers
Strings are objects in Java, not
arrays
Java uses garbage collection
No operator overloading
No multiple inheritance
ALL
methods and objects
News Page | CS2390 Sp'98 Home Page | CS2390 CoWeb | STABLE | BOOST
Questions/comments/concerns to guzdial@cc.gatech.edu
Page last updated 5/14/98; 10:16:18 AM