The document discusses several advanced Java techniques including data structures, networking using sockets, and reflection. It provides examples of using a Hashtable to store key-value pairs, describes how sockets allow for network communication between clients and servers by establishing connections, and explains how reflection allows determining information about classes at runtime such as their fields and methods.
3. Hashtable Example Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3)); key value
4. Hashtable Example Integer n = (Integer)numbers.get("two"); if (n != null) { System.out.println("two = " + n); } Object Specific Object
5.
6.
7.
8.
9.
10.
11.
12. Echo Echo Echo import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; // …
13. Establish the Socket connection try { echoSocket = new Socket(“image ", 7777); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch … Host Port Output Input
14. Need to Catch Exceptions } 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); }
15. Simple Socket Example BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } Set up a mechanism to read from standard input Output what’s read back from Server Write to Server Read from standard input
16. Close up Shop on Client side out.close( ); in.close( ); stdIn.close( ); echoSocket.close( );
17.
18.
19.
20.
21.
22.
23. The newprotocol Client public class Client { Socket socket = new Socket( "127.0.0.1", 9999 ); // ObjectInputStream input = new ObjectInputStream(socket. getInputStream () ); // read using serialization NewProtocol protocol = ( NewProtocol )(input .readObject () ); System.out.println(“Protocol: “ + protocol); socket.close();
24. The newprotocol Server class ThreadedSocket extends Thread { // here is where all the real work is done. private Socket socket; ThreadedSocket( Socket socket ) { this.socket = socket; //… ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream() ); output.writeObject( protocol );
25.
26.
27. The read counterpart FileInputStream istream = new FileInputStream(" foo.bar "); ObjectInputStream p = new ObjectInputStream(istream); int i = p.readInt(); String today = (String)p.readObject(); Date date = (Date)p.readObject(); istream.close();
28.
29.
30.
31.
32.
33.
34.
35.
36.
37. An Example import java.lang.reflect.*; import java.awt.*; class SampleName { public static void main(String[] args) { Button b = new Button(); printName(b); } static void printName(Object o) { Class c = o.getClass(); String s = c.getName(); System.out.println(s); }} Need Reflection Package To Do this