Modern Compiler Design Java Tutorial
Modern Compiler Design Java Tutorial
Modern Compiler Design Java Tutorial
Java Tutorial
Object-Oriented Programming
Functional/procedural programming:
program is a list of instructions to the computer
Object-oriented programming
program is composed of a collection objects
that communicate with each other
Main Concepts
Object
Class
Inheritance
Encapsulation
Objects
identity unique identification of an object
attributes data/state
services methods/operations
supported by the object
within objects responsibility to provide these
services to other clients
Class
type
object is an instance of class
class groups similar objects
same (structure of) attributes
same services
object holds values of its classs attributes
Inheritance
Class hierarchy
Generalization and Specialization
subclass inherits attributes and services from its
superclass
subclass may add new attributes and services
subclass may reuse the code in the superclass
subclasses provide specialized behaviors (overriding
and dynamic binding)
partially define and implement common behaviors
(abstract)
Encapsulation
Separation between internal state of the object
and its external aspects
How ?
control access to members of the class
interface type
What does it buy us ?
Modularity
source code for an object can be written and maintained
independently of the source code for other objects
easier maintainance and reuse
Information hiding
other objects can ignore implementation details
security (object has control over its internal state)
but
shared data need special design patterns (e.g., DB)
performance overhead
mainly for c++ programmer
myprog.c myprog.exe
gcc machine code
C source code
OS/Hardware
Platform Independent
myprog.java myprog.class
javac bytecode
Java source code
JVM
OS/Hardware
Primitive types
int 4 bytes
short 2 bytes
long 8 bytes Behaviors is
byte 1 byte exactly as in
float 4 bytes C++
double 8 bytes
char Unicode encoding (2 bytes)
boolean {true,false} Note:
Primitive type
always begin
with lower-case
Primitive types - cont.
Constants
37 integer
37.2 float
42F float
0754 integer (octal)
0xfe integer (hexadecimal)
Wrappers
Is:
In Java
Animal[][] arr=
new Animal[2][2]
// error :
public static Color getColor()
{ return myColor_; }
}
Static - [2/4] cont.
Usage:
System.out.println(We have +
TeaPot.howManyTeaPots()+ Tea Pots);
Static - [3/4]
Block
Code that is executed in the first reference to the class.
Several static blocks can exist in the same class
( Execution order is by the appearance order in the
class definition ).
Only static members can be accessed.
class RandomGenerator {
private static int seed_;
static {
int t = System.getTime() % 100;
seed_ = System.getTime();
while(t-- > 0)
seed_ = getNextNumber(seed_);
}
}
}
String is an Object
Constant strings as in C, does not exist
class B() {
System.out.println("In classB()");
}
}
Abstract
abstract member function, means that the function does not have an implementation.
abstract class, is class that can not be instantiated.
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
new AbstractTest();
^
1 error
NOTE:
An abstract class is not required to have an abstract method in it.
But any class that has an abstract method in it or that does
not provide an implementation for any abstract methods declared
in its superclasses must be declared as an abstract class.
Example
Abstract - Example
package java.lang;
public abstract class Shape {
public abstract void draw();
public void move(int x, int y) {
setColor(BackGroundColor);
draw();
setCenter(x,y);
setColor(ForeGroundColor);
draw();
}
}
package java.lang;
public class Circle extends Shape {
public void draw() {
// draw the circle ...
}
}
Interface
Interfaces are useful for the following:
Capturing similarities among unrelated
classes without artificially forcing a class
relationship.
Declaring methods that one or more classes
are expected to implement.
Revealing an object's programming interface
without revealing its class.
Interface
abstract class
*
- The correct term is to implement Example
an interface
Interface
interface IChef {
void cook(Food food);
}
Collection Map
SortedSet
Collection Interface
Basic Operations
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element);
boolean remove(Object element);
Iterator iterator();
Bulk Operations
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();
Array Operations
Object[] toArray(); <T> T[] toArray(T[] a); }
General Purpose Implementations
Collection Map
SortedSet
}
}
IO - Introduction
Definition
Stream is a flow of data
characters read from a file
bytes written to the network
Philosophy
All streams in the world are basically the same.
Streams can be divided (as the name IO suggests) to Input and
Output streams.
Implementation
Incoming flow of data (characters) implements Reader (InputStream for
bytes)
Outgoing flow of data (characters) implements Writer (OutputStream for
bytes eg. Images, sounds etc.)
Exception - What is it and why do I care?
Exception is an Object
Exception class must be descendent of Throwable.
Exception - What is it and why do I care?(2)
readFile {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
}
1: Separating Error Handling Code from "Regular" Code (2)
errorCodeType readFile {
initialize errorCode = 0;
open the file;
if (theFileIsOpen) {
determine the length of the file;
if (gotTheFileLength) {
allocate that much memory;
if (gotEnoughMemory) {
read the file into memory;
if (readFailed) {
errorCode = -1;
}
} else {
errorCode = -2;
}
} else {
errorCode = -3;
}
close the file;
if (theFileDidntClose && errorCode == 0) {
errorCode = -4;
} else {
errorCode = errorCode and -4;
}
} else {
errorCode = -5;
}
return errorCode;
}
1: Separating Error Handling Code from "Regular" Code (3)
readFile {
try {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
} catch (fileOpenFailed) {
doSomething;
} catch (sizeDeterminationFailed) {
doSomething;
} catch (memoryAllocationFailed) {
doSomething;
} catch (readFailed) {
doSomething;
} catch (fileCloseFailed) {
doSomething;
}
}
2: Propagating Errors Up the Call Stack
method1 {
try {
call method2;
} catch (exception) {
doErrorProcessing;
}
}
method2 throws exception {
call method3;
}
method3 throws exception {
call readFile;
}