DOC PREVIEW
UMD CMSC 433 - Java Review

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 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 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC433, Spring 2004 Programming Language Technology andParadigms Java ReviewJeff FosterJanuary 29, 20042Administrivia• Project 1 will be posted later today– Due February 11• Reading: (today) Liskov ch. 1, 2, (Tue) ch. 4– Supplemental: Eckel ch. 1, 7, 8, 93Java• Descended from Simula67, SmallTalk, others– Superficially similar to C, C++• Fully specified, compiles to virtual machine– machine-independent• Secure– bytecode verification (“type-safe”)– security manager4Object Orientation• Combining data and behavior– objects, not developers, decide how to carry outoperations• Sharing via abstraction and inheritance– similar operations and structures are implemented once• Emphasis on object-structure rather thanprocedure structure– behavior more stable than implementation– … but procedure structure still useful5Examplepublic 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); }}6Using 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);}7The Class Hierarchy• Classes by themselves are a powerful tool– Support abstraction and encapsulation• Java also provides two other abilities– Interfaces allow different classes to be treated the same– Inheritance allows code reuse• Note: When you inherit from a class, you also“implement” the class’s “interface”8Project 1: Interfacespublic 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(...);9Interfaces• An interface lists supported (public) methods– No constructors or implementations allowed– Can have final static variables• A class can implement (be a subtype of) zero ormore interfaces• Given some interface I, declaring I x = ... means– x must refer to an instance of a class that implements I,or else null10Interface Inheritance• Interfaces can extend other interfaces– Sometimes convenient form of reuse (see project 1)• Given interfaces I1 and I2 where I2 extends I1– If C implements I2, then C implements I1• Since a class can implement multiple interfaces,interface extensions are often not needed11Inheritance• Each Java class extends or inherits code fromexactly one superclass• Permits reusing classes to define new objects– Can define the behavior of the new object in terms ofthe old one12Exampleclass 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 be13Subtyping• Both inheritance and interfaces allow one class tobe used where another is specified– This is really the same idea: subtyping• We say that A is a subtype of B if– A extends B or a subtype of B, or– A implements B or a subtype of B14Java Design• Everything inherits from Object*– Even arrays– Allows sharing, generics, and more* Well, almost: there are primitive int, long, float, etc.ObjectThreadIntegerNumber…15No 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 oftenbadly used– And it’s complicated to implement well16Abstract Classes• Sometimes want a class with some code, but withsome 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 thecode• You can’t instantiate such a class• Instead, we can mark such a class as abstract– And mark the unimplemented methods as abstract17Example 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 thesecond version of write(...)– But they do need to override the first one, since it’s abstract– (Note: They may want to override anyhow for efficiency)18Example from Project 1public abstract class ServletFilter extendsOutputStream implements MiniServlet{ public abstract void write(int b) ...; public abstract void close() ...; public abstract void flush() ...; public abstract void setArg(String arg); public abstract void setServlet(MiniServlet s); public abstract void setOutputStream(OutputStream out);}19InStreamOutStreamProcessI/O streams• Raw communication takes place using streams• Java also provides readers and writers– character streams• Applies to files, network connections, strings, etc.20I/O Classes• OutputStream – byte stream going out• Writer – character stream going out• InputStream – byte stream coming in• Reader – character stream coming in21Some OutputStreams and Writers• Example classes– ByteArrayOutputStream – goes to byte []– FileOutputStream – goes to file• OutputStreamWriter– wraps around OutputStream to get a Writer– takes characters, converts to bytes– can specify encoding used to convert• Other wrappers– PrintWriter – supports print, println– StringWriter22Applications 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– should be PrintWriter, but would break 1.0 code– System.out.print(…) prints a string– System.out.println(…) prints a string with a newline• System.in is an InputStream– not quite so easy to use23Java Networking• class Socket– Communication channel• class ServerSocket– Server-side “listen” socket– Awaits and responds to connection requests24Example 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


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?