Advance-Java Pps
Advance-Java Pps
Applications
Overview
We will see some advanced techniques
and applications of Java
Specific
Object Object
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
Many Other Collections
Vector
Stack
LinkedList
Dictionary
ArrayList
http://www.javasoft.com/products/jdk/1.2/docs/api/in
dex.html for a complete list
Networking
}
catch (UnknownHostException e) {
System.err.println("Don't know about host: avatar.");
System.exit(1);
}
catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: avatar.");
System.exit(1);
}
Simple Socket Example
Set up a mechanism to read from standard input
out.println(userInput); Write to
Server
System.out.println("echo: " + in.readLine());
} Output whats read back from Server
Close up Shop on Client side
out.close( );
in.close( );
stdIn.close( );
echoSocket.close( );
Basic Steps
Open a socket.
Open an input stream and output stream to the
socket.
Read from and write to the stream according to the
server's protocol.
Close the streams.
Close the socket.
Same Basic Steps
This client program is straightforward and simple
because the Echo server implements a simple
protocol
Ports Client
Server
socket.close();
The newprotocol Server
class ThreadedSocket extends Thread {
// here is where all the real work is done.
private Socket socket;
output.writeObject( protocol );
Reading and Writing Objects
An ObjectOutputStream writes primitive data
types and graphs of Java objects to an
OutputStream.
General Mechanism
This works for the sockets as was just shown
but is actually more general
Persistent storage of objects can be
accomplished by using a file for the stream.
File example
For example to write an object that can be read by
the example in ObjectInputStream
int i = p.readInt();
istream.close();
The Needed Java Framework
Only objects that support the java.io.Serializable
interface can be written to streams.
Foo
Foo
Bar
Bar
Other Ways of Retrieving Class Objects
Class c = java.awt.Button.class;
Class c = Class.forName(commandNameToken)
Getting the Class Name
Every class in the Java programming language has a
name. When you declare a class, the name immediately
follows the class keyword