Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Advance Java


Published on

This presentation explains about concepts in Advanced Java

Published in: Technology, News & Politics

Advance Java

  1. 1. Advanced Java Applications
  2. 2. Overview <ul><li>We will see some advanced techniques and applications of Java </li></ul><ul><li>We will take a quick look at some data structures built into the language </li></ul><ul><li>Next will show be how Java and Object Orientation can be applied to networking </li></ul><ul><li>Finally we will be to show Reflection in Java </li></ul>
  3. 3. Hashtable Example Hashtable numbers = new Hashtable(); numbers.put(&quot;one&quot;, new Integer(1)); numbers.put(&quot;two&quot;, new Integer(2)); numbers.put(&quot;three&quot;, new Integer(3)); key value
  4. 4. Hashtable Example Integer n = (Integer)numbers.get(&quot;two&quot;); if (n != null) { System.out.println(&quot;two = &quot; + n); } Object Specific Object
  5. 5. Many Other Collections <ul><li>Vector </li></ul><ul><li>Stack </li></ul><ul><li>LinkedList </li></ul><ul><li>Dictionary </li></ul><ul><li>ArrayList </li></ul><ul><li> for a complete list </li></ul>
  6. 6. Networking <ul><li>Using the networking capabilities provided in the Java environment is quite easy </li></ul><ul><li>We will see how to use Sockets </li></ul>
  7. 7. Sockets <ul><li>Lower-level network communication </li></ul><ul><ul><li>- Client – uses some service </li></ul></ul><ul><ul><li>- Server - provides some service </li></ul></ul><ul><li>TCP provides a reliable, point-to-point communication channel for client-server apps </li></ul>
  8. 8. What Is a Socket? <ul><li>A socket is one endpoint of a two-way communication link between two programs running on the network. </li></ul><ul><li>A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent. </li></ul>
  9. 9. How do Sockets work? <ul><li>A server runs on a specific computer and has a socket that is bound to a specific port number. </li></ul><ul><li>Client knows the hostname and port of server and tries to make a connection request </li></ul>
  10. 10. Connection established <ul><li>If the server accepts the connection it gets a new socket bound to a different port. </li></ul><ul><li>It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket </li></ul>
  11. 11. How does Java support Sockets <ul><li>The package provides a class, Socket , that implements one side of a two-way connection between your Java program and another program on the network </li></ul><ul><li>It also includes the ServerSocket class, which implements a socket that servers can use to listen for and accept connections to client </li></ul>
  12. 12. Echo Echo Echo import*; import*; public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; // …
  13. 13. Establish the Socket connection try { echoSocket = new Socket(“image &quot;, 7777); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch … Host Port Output Input
  14. 14. Need to Catch Exceptions } catch (UnknownHostException e) { System.err.println(&quot;Don't know about host: avatar.&quot;); System.exit(1); } catch (IOException e) { System.err.println(&quot;Couldn't get I/O for &quot; + &quot;the connection to: avatar.&quot;); System.exit(1); }
  15. 15. Simple Socket Example BufferedReader stdIn = new BufferedReader( new InputStreamReader(; String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println(&quot;echo: &quot; + 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. 16. Close up Shop on Client side out.close( ); in.close( ); stdIn.close( ); echoSocket.close( );
  17. 17. Basic Steps <ul><li>Open a socket. </li></ul><ul><li>Open an input stream and output stream to the socket. </li></ul><ul><li>Read from and write to the stream according to the server's protocol. </li></ul><ul><li>Close the streams. </li></ul><ul><li>Close the socket. </li></ul>
  18. 18. Same Basic Steps <ul><li>This client program is straightforward and simple because the Echo server implements a simple protocol </li></ul><ul><li>Even with more complicated protocols such as HTTP server, your client program while more complicated will follow the same basics as this simple example </li></ul>
  19. 19. Server <ul><li>A server must open a SeverSocket </li></ul><ul><li>ServerSocket server = new ServerSocket( 7777 ); </li></ul><ul><li>Call accept on that socket creating a new socket </li></ul><ul><li>Socket socket = server.accept(); </li></ul><ul><li>Socket acts as socket from client </li></ul>
  20. 20. If a socket is a pipe … <ul><li>We could conceptualize this like so: </li></ul>Client Server Ports The Socket Plumbing The things flowing through the Plumbing
  21. 21. The Answer Is .. <ul><li>A Number of things can conceptually flow through the pipe </li></ul><ul><li>We will focus on two: </li></ul><ul><ul><li>Objects </li></ul></ul><ul><ul><li>Characters from a String </li></ul></ul><ul><li>We looked at several examples last time </li></ul><ul><ul><li>The first was a simple echo program – a very simple protocol – give me back what I gave you (Strings) </li></ul></ul><ul><ul><li>We also looked at simpleprotocol example (Protocol Objects) </li></ul></ul>
  22. 22. Objects flow through the Pipe <ul><li>Let first address the case where we want to have objects flowing over the pipe </li></ul><ul><li>Must have at least the following mechanisms for </li></ul><ul><ul><li>Objects to be written by the server </li></ul></ul><ul><ul><li>Objects to be read by the client </li></ul></ul>
  23. 23. The newprotocol Client public class Client { Socket socket = new Socket( &quot;;, 9999 ); // ObjectInputStream input = new ObjectInputStream(socket. getInputStream () ); // read using serialization NewProtocol protocol = ( NewProtocol )(input .readObject () ); System.out.println(“Protocol: “ + protocol); socket.close();
  24. 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. 25. Reading and Writing Objects <ul><li>An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. </li></ul><ul><li>The objects can be read (reconstituted) using an ObjectInputStream. </li></ul><ul><li>General Mechanism </li></ul><ul><ul><li>This works for the sockets as was just shown but is actually more general </li></ul></ul><ul><ul><li>Persistent storage of objects can be accomplished by using a file for the stream. </li></ul></ul>
  26. 26. File example <ul><li>For example to write an object that can be read by the example in ObjectInputStream </li></ul>FileOutputStream ostream = new FileOutputStream(“;); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeInt(12345); p.writeObject(&quot;Today&quot;); p.writeObject(new Date()); p.flush(); ostream.close();
  27. 27. The read counterpart FileInputStream istream = new FileInputStream(&quot; &quot;); ObjectInputStream p = new ObjectInputStream(istream); int i = p.readInt(); String today = (String)p.readObject(); Date date = (Date)p.readObject(); istream.close();
  28. 28. The Needed Java Framework <ul><li>Only objects that support the interface can be written to streams. </li></ul><ul><li>The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects </li></ul><ul><li>This relates to introspection/reflection which we will discuss shortly </li></ul>
  29. 29. More about the Framework <ul><li>The default deserialization mechanism for objects restores the contents of each field to the value and type it had when it was written. </li></ul><ul><ul><li>Marshalling of Objects (Serialize) </li></ul></ul><ul><ul><li>Un marshaling of Object (Serialize) </li></ul></ul>
  30. 30. Deserialization& Object Reflection <ul><li>Fields declared as transient or static are ignored by the deserialization process. </li></ul><ul><li>References to other objects cause those objects to be read from the stream as necessary . </li></ul><ul><ul><li>Graphs of objects are restored correctly using a reference sharing mechanism. </li></ul></ul><ul><ul><li>New objects are always allocated when deserializing, which prevents existing objects from being overwritten </li></ul></ul>Reflection
  31. 31. Reflection Allows <ul><li>Determination of the class of an object. </li></ul><ul><li>Creation of an instance of a class whose name is not known until runtime. </li></ul><ul><li>Obtaining information about a class's modifiers, fields, methods, constructors, and superclasses. </li></ul><ul><li>Determination of constants and method declarations that belong to an interface </li></ul>
  32. 32. Reflection Also Allows <ul><li>Allows one to get and set the value of an object's field, even if the field name is unknown to your program until runtime. </li></ul><ul><li>Allows one to invoke a method on an object, even if the method is not known until runtime. </li></ul><ul><li>Create a new array, whose size and component type are not known until runtime, and then modify the array's components. </li></ul>
  33. 33. Examining Classes <ul><li>A way to get information about classes at runtime </li></ul><ul><li>For each class, the Java Runtime Environment (JRE) maintains an immutable Class object that contains information about the class. A Class object represents, or reflects, the class </li></ul><ul><li>To get this information you need to get the Class object that reflects the class </li></ul>
  34. 34. Retrieving Class Objects <ul><li>You can retrieve a Class object in several ways: </li></ul><ul><li>Class c = foo.getClass() // for some object named foo </li></ul><ul><li>Bar b = new Bar(); </li></ul><ul><li>Class c = b.getClass(); </li></ul><ul><li>Class s = c.getSuperclass(); </li></ul>Foo Bar
  35. 35. Other Ways of Retrieving Class Objects <ul><li>If you know the name of the class at compile time, you can retrieve its Class object by appending .class to its name: </li></ul>Class c = java.awt.Button.class; <ul><li>You can also use the Class.forName static method: </li></ul>Class c = Class.forName(commandNameToken)
  36. 36. Getting the Class Name <ul><li>Every class in the Java programming language has a name. When you declare a class, the name immediately follows the class keyword </li></ul><ul><li>At runtime, you can determine the name of a Class object by invoking the getName method. The String returned by getName is the fully-qualified name of the class. </li></ul><ul><li>A good home study question: Given an instance prints the names of the classes its inheritance hierarchy from least specific to most specific excluding Object </li></ul>
  37. 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