DOC PREVIEW
UMD CMSC 433 - Java Review

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC433, Spring 2007Programming Language Technology and ParadigmsJava ReviewAtif Memon02/06/2007-02/13/2007 2Java• Descended from Simula67, SmallTalk, others– Superficially similar to C, C++• Fully specified, compiles to virtual machine– machine-independent• Secure– bytecode verification (“type-safe”)– security manager3Object Orientation• Combining data and behavior– objects, not developers, decide how to carry out operations• Sharing via abstraction and inheritance– similar operations and structures are implemented once• Emphasis on object-structure rather than procedure structure– … but procedure structure still useful4Examplepublic class Complex {private double r, i;public Complex(double r, double i) {this.r = r; this.i = i;}public String toString() {return “(“ + r + “, “ + i + “)”;}public Complex plus(Complex that) {return new Complex(r + that.r, i + that.i);}}25Using Complexpublic static void main(String[] args) {Complex a = new Complex(5.5,9.2);Complex b = new Complex(2.3,-5.1);Complex c;c = a.plus(b);System.out.println(“a = “ + a);System.out.println(“b = “ + b);System.out.println(“c = “ + c);}•instantiation•println calls the java.io.PrintStream.print(Object) method•which calls the String.valueOf method•The String.valueOf method is very simple: public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } 6The Class Hierarchy• Classes by themselves are a powerful tool– Support abstraction and encapsulation• Java also provides– Inheritance allows code reuse• Note: When you inherit from a class, you also “implement” the class’s “interface” (will come back to interface)7Inheritance• Each Java class extends or inherits code from exactly one superclass• Permits reusing classes to define new objects– Can define the behavior of the new object in terms of the old one8Exampleclass Point {int getX() { ... }int getY() { ... }}class ColorPoint extends Point {int getColor() { ... }}• ColorPoint reuses getX() and getY() from Point• ColorPoint “implements” the Point “interface”– They can be used anywhere a Point can be39Java Design• Everything inherits from Object*–Even arrays– Allows sharing, generics, and more* Well, almost: there are primitive int, long, float, etc. ObjectThreadIntegerNumber…10Interfacespublic interface MiniServlet extends Runnable {void setArg(String arg);void setOutputStream(OutputStream out);}class HelloWorld implements MiniServlet { ... }class Print implements MiniServlet { ... }MiniServlet s = new HelloWorld();if (...) s = new Print();s.setArg(...);–Interfaces allow different classes to be treated the same11Interfaces• An interface lists supported (public) methods– No constructors or implementations allowed– Can have final static variables• A class can implement zero or more interfaces• Given some interface I, declaring I x = ... means– x must refer to an instance of a class that implements I, or else null12Interface Inheritance• Interfaces can extend other interfaces– Sometimes convenient form of reuse• Given interfaces I1 and I2 where I2 extends I1–If C implements I2, then C implements I1• Because a class can implement multiple interfaces, interface extensions are often not needed413No Multiple Inheritance• A class type can implement many interfaces• But can only extend one superclass• Not a big deal– Multiple inheritance rarely, if ever, necessary and often badly used– And it’s complicated to implement well14Abstract Classes• Sometimes want a class with some code, but with some methods unwritten– It can’t be an interface because it has code– It can’t be a regular class because it doesn’t have all the code• You can’t instantiate such a class• Instead, we can mark such a class as abstract– And mark the unimplemented methods as abstract15Example from JDKpublic abstract class OutputStream {public abstract void write(int b) ...;public void write(byte b[], int off, int len) ... {... write(b[off + i]);...}...}• Subclasses of OutputStream need not override the second version of write(...)– But they do need to override the first one as it is abstract16InStreamOutStreamProcessI/O streams• Raw communication takes place using streams• Java also provides readers and writers– character streams• Applies to files, network connections, strings, etc.517I/O Classes• OutputStream – byte stream going out• Writer – character stream going out• InputStream – byte stream coming in• Reader – character stream coming in18Applications and I/O• Java “external interface” is a public class– public static void main(String [] args)• args[0] is first argument– unlike C/C++• System.out and System.err are PrintStreams– System.out.print(…) prints a string– System.out.println(…) prints a string with a newline19Java Networking• class Socket– Communication channel• class ServerSocket– Server-side “listen” socket– Awaits and responds to connection requests20Example Client/ServerServerSocket s = new ServerSocket(5001);Socket conn = s.accept();InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Socket conn = new Socket (“www.cs.umd.edu”, 5001);InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Server codeserver clientClient code621Example Client/ServerServerSocket s = new ServerSocket(5001);Socket conn = s.accept();InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Socket conn = new Socket (“www.cs.umd.edu”, 5001);InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Server codeserver clientClient code22Example Client/ServerServerSocket s = new ServerSocket(5001);Socket conn = s.accept();InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Socket conn = new Socket (“www.cs.umd.edu”, 5001);InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Server codeserver client?Client code23Example Client/ServerServerSocket s = new ServerSocket(5001);Socket conn = s.accept();InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Socket conn = new Socket (“www.cs.umd.edu”, 5001);InputStream in = conn.getInputStream();OutputStream out = conn.getOutputStream();Server codeserver clientNote: The server can still accept other connection requests on


View Full Document

UMD CMSC 433 - Java Review

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Java Review
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Review and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Review 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?