Modern Compiler Design: Java Tutorial
Modern Compiler Design: Java Tutorial
Java Tutorial
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
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)
How ?
control access to members of the class
interface “type”
Easy to learn
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
13 NFC on Java- suresh
Primitive types
• int 4 bytes
• short 2 bytes
• long 8 bytes
Behaviors is
exactly as in
• byte 1 byte
C++
• float 4 bytes
• double 8 bytes
• char Unicode encoding (2 bytes) Note:
Primitive type
14 • NFC
boolean
on Java- suresh {true,false}
always begin
with lower-case
Primitive types - cont.
• Constants
37 integer
37.2 float
42F float
0754 integer (octal)
0xfe integer (hexadecimal)
Is:
• In Java
Animal[][] arr=
new Animal[2][2]
Class TeaPot {
private static int numOfTP = 0;
private Color myColor_;
public TeaPot(Color c) {
myColor_ = c;
numOfTP++;
}
public static int howManyTeaPots()
{ return numOfTP; }
// error :
public static Color getColor()
{ return myColor_; }
23 NFC on Java- suresh }
Static - [2/4] cont.
Usage:
System.out.println(“We have “ +
TeaPot.howManyTeaPots()+ “Tea Pots”);
class RandomGenerator {
private static int seed_;
static {
int t = System.getTime() % 100;
seed_ = System.getTime();
while(t-- > 0)
seed_ = getNextNumber(seed_);
}
}
}
25 NFC on Java- suresh
String is an Object
do/while
int i=5; switch
if/else do { char
If(x==4) { // act1 c=IN.getChar();
// act1 i--; switch(c) {
} else { } while(i!=0); case „a‟:
// act2 case „b‟:
} for // act1
break;
int j;
default:
for(int i=0;i<=9;i++)
// act2
{
}
j+=i;
27 }
NFC on Java- suresh
Packages
class B() {
System.out.println("In classB()");
}
34 } NFC on Java- suresh
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.
35 NFC on Java- suresh
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 ...
}
}
36 NFC on Java- suresh
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.
*
38
- The correct term is “to implement”
NFC on Java- suresh
Example
an interface
Interface
interface IChef {
void cook(Food food);
}
39 * access
NFC rights
on Java- suresh(Java forbids reducing of access rights)
When to use an interface ?
Collection Map
SortedSet
Collection Map
SortedSet
}
47 NFC on Java- suresh }
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 is an Object
• Exception class must be descendent of Throwable.
readFile {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
}
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;
}
53 NFC on Java- suresh
}
2: Propagating Errors Up the Call Stack
method1 {
try {
call method2;
} catch (exception) {
doErrorProcessing;
}
}
method2 throws exception {
call method3;
}
method3 throws exception {
call readFile;
}
54 NFC on Java- suresh