Download Lecture Slides on Java Review: Programming Language Technologies and Paradigms | CMSC 433 and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!
CMSC433, Fall 2008
Programming Language Technology and
Paradigms
Java Review
Adam Porter
2
Java
- Descended from Simula67, SmallTalk, others
- Superficially similar to C, C++
- Fully specified, compiles to virtual machine
- Secure
- bytecode verification (“type-safe”)
- security manager
3
Object 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
- behavior more stable than implementation
- … but procedure structure still useful 4
Example
public 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); } }
7
Project 1: Interfaces
public 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(...); 8
Interfaces
- 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 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 null
9
Interface Inheritance
- Interfaces can extend other interfaces
- Sometimes convenient form of reuse (see project 1)
- Given interfaces I1 and I2 where I2 extends I
- If C implements I2 , then C implements I
- Since a class can implement multiple interfaces,
interface extensions are often not needed
10
Inheritance
- 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 one
13
Java Design
- Everything inherits from Object*
- Even arrays
- Allows sharing, generics, and more
- Well, almost: there are primitive int, long, float, etc.
Object
Thread
Integer
Number
14
No Multiple Inheritance
- A class type can implement many interfaces
- But can only extend one superclass
- Not a big deal
- There are ways to get around it
- And it’s complicated to implement well
- Never-ending debate on this point
15
Abstract 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 abstract 16
Example from JDK
public 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, since it’s abstract
- (Note: They may want to override anyhow for efficiency)
19
I/O Classes
- OutputStream – byte stream going out
- Writer – character stream going out
- InputStream – byte stream coming in
- Reader – character stream coming in 20
Some 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
- StringWriter
21
Applications and I/O
- Java “external interface” is a public class
- public static void main(String [] args)
- args[0] is first argument
- 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 use 22
Java Networking
- class Socket
- class ServerSocket
- Server-side “listen” socket
- Awaits and responds to connection requests
25
Example Client/Server
ServerSocket 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 code
server client ?
Client
code
26
Example Client/Server
ServerSocket 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 code
server client Note: The server can still accept other connection requests on port 5001
Client
code
27
Example Client/Server
ServerSocket 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 code
server client in out
Client
code
28
Example Client/Server
ServerSocket 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 code
server client in out in out
Client
code
31
Demo
- Run a basic echo server 32
Objects and Variables
- Variables of a primitive type contain values
- e.g., byte, char, int, ...
- int i = 6;
- Uninitialized values contain 0
- Assignment copies values
- Variables of other types contain references to the heap
- int[] a = new int[3];
- Objects are allocated with new
- Uninitialized object references are null
- Assignment copies references , not the objects themselves
33
Example
int i = 6; int j; // uninitialized int [] a = {1, 3, 5, 7, 9}; int [] b = new int[3]; String s = “abcdef”; String t = null; 34
Example: Assignments
j = i; b = a; t = s;
37
Example: Mutability
a[1] = 0; // also changes b[1]
careful when you have
aliasing
- Multiple references to the same object 38
Method Invocation
- Syntax o.m(arg1, arg2, ..., argn);
- Run the m method of object o with arguments arg1...argn
- Two ways to reuse method names:
- Methods can be overridden
- Methods can be overloaded
39
Overriding
- Define a method also defined by a superclass class Parent { int cost; void add(int x) { cost += x; } } class Child extends Parent { void add(int x) { if (x > 0) cost += x; } } 40
Overriding (cont’d)
- Method with same name and argument types in
child class overrides method in parent class
- Arguments and result types must be identical
- otherwise you are overloading the method
- Must raise the same or fewer exceptions
- Can override/hide instance variables
- both variables will exist, but don’t do it