Title: Programming
1Programming
- Aris Papadopoulos
- ap7_at_doc.ic.ac.uk
2Course Outline
- Day 1 - Introduction
- Java VM and Memory Model
- Java Object-Orientation
- Key language features
- Day 2 - The standard library
- Collections
- Input/Output
- Generics (new Java 1.5 features)
- GUI programming and the Swing
3Course Outline
4Materials
- http//www.doc.ic.ac.uk/ap7/java.html
5Why Java?
6Compiling C Programs
include ltstdiogtmain( int argc, char argv)
// do something
7Java Architecture
- Wrapping up the power of
- Java language
- Java Virtual Machine
- Java file format
- Java API
8The Java Virtual Machine
javac
9The Java Virtual Machine
Compile-time
Run-time
Your programs source files
Your programs class files
A.java
B.java
C.java
A.class
B.class
C.class
Java Virtual Machine
Java compiler
A.class
B.class
C.class
Object.class
String.class
Your programs class files
Java APIs class files
10The Java Virtual Machine
class Hello public static void main() //
do something
Mac JVM
Linux JVM
Win JVM
11The Java Virtual Machine
- One at a time
- Just-in-time compiler
- Adaptive optimizer
JVM
Class loader
bytecodes
Execution engine
12Memory Management (C)
- C has two ways of creating objects
int main( int argc , char argv ) Dog
d(rover) Cat c(fluffy) d.bark() Dog
dp new Dog(sirius) dp-gtbark() delete
dp
On the stack (automatic)
On the heap (with new)
Need to delete
13Memory Management (C)
int main( int argc , char argv ) for( int
i0 ilt100 i ) Dog dp new
Dog(sirius) dp-gtbark()
14Memory Management (Java)
- All Java objects are created on the heap
public static void main( String args ) Dog
d new Dog(rover) Cat c new
Cat(fluffy) d.bark() Dog dp new
Dog(sirius) dp.bark()
No stars or arrows
15Memory Management (Java)
public static void main( String args ) for(
int i0 ilt100 i ) Dog dp new
Dog(sirius) dp.bark()
Javas heap is garbage collected
16Memory Management (Java)
public static void main( String args ) for(
int i0 ilt100 i ) Dog dp new
Dog(sirius) dp.bark()
17Garbage Collection Reference Counting
- Object creation Reference count 1
- New reference count
- Reference out of scope OR new value count--
- If reference count 0 object can be freed.
18A First Program
All programs must have at least one class
At least one class must have a main method with
this signature
A standard library function to print text on the
console
class Hello public static void main( String
args ) System.out.println(Our first Java
program)
19More Classes
- Programs comprise sets of classes
- Classes can have
- Fields
- Methods
- Constructors
20A Dog Class
A constructor. No destructor as Java has garbage
collection
Field declarations
class Dog String name int age Dog (
String p_name ) name p_name void
bark() System.out.println(Woof! Woof!)
Methods are defined inside the class
21Some other Animals
Abstract class
Dog
Cat
Fish
Bird
Group classes in a package
22Some other Animals
package statement
package animals abstract class Animal
String name int age public abstract void
makeNoise()
abstract class
Dog
Cat
Fish
Bird
abstract method
23Some other Animals
package animals class Dog extends Animal
Dog ( String p_name ) name p_name
public void makeNoise() bark() void bark()
System.out.println(Woof! Woof!)
inheritance Dog extends Animal
Dog
Cat
Fish
Bird
implementing abstract method
24Using our Animals
Import types defined in animals package
import animals. class PetShop public static
void main( String args ) Animal pet new
Dog(rover) pet.makeNoise() pet new
Bird() pet.makeNoise()
Creating an object with the new keyword
25Using our Animals
import animals. class PetShop public static
void main( String args ) Animal pet new
animals.Dog(rover) pet.makeNoise()
pet new Bird() pet.makeNoise()
26Access Modifiers
Classes and methods accessed from outside package
must be public
package animals public class Dog extends Animal
public Dog ( String p_name ) name
p_name public void makeNoise() bark()
private void bark() System.out.println(
Woof! Woof!)
Methods called only in this class should be
private
27Class member access
28Java has Single Inheritance
What if we want them both to be Performers?
29Interfaces
Human
Animal
Interface implementation
Elephant
RingMaster
Inheritance -RingMaster extends Human
30Performer Interface
Interfaces define named types
public interface Performer public void
perform()
No body for the method given
Interfaces define sets of methods
31Implementing the Interface
Declare class as implementing interface
public class Elephant extends Animal
implements Performer public void
makeNoise() trumpet() public void
perform() walk() trumpet()
Implement method from Performer
32Using Performers
import animals. class Circus public static
void main( String args ) Performer p
new RingMaster() p.perform() p new
Elephant() p.perform()
Anything that implements Performer may be
assigned to p
Can only call methods from Performer interface on
p
33Exceptions
- Sometimes a method may fail
- Exceptional behaviour
- Catch and handle this special case
- Methods can throw an Exception to signal that
something is wrong
34Exceptions
Throwable
Error
Exception
IOException
RuntimeException
35Documentation for FileReader
FileReader public FileReader(File file) throws
FileNotFoundException Creates a new FileReader,
given the File to read from. Parameters file -
the File to read from Throws FileNotFoundExcept
ion - if the file does not exist, is a directory
rather than a regular file, or for some other
reason cannot be opened for reading.
36Try and Catch
class Test public void readFile()
File myFile new File( data.txt ) try
FileReader fr new FileReader( myFile )
catch ( FileNotFoundException fnfe )
System.out.println( File not found. )
37Alternatively, Throw
class Test public void readFile() throws
FileNotFoundException
File myFile new File( filename )
FileReader fr new FileReader( myFile )
38Throwing your own exceptions
class Test public void unlockDoor()
throws
AlreadyOpenException if ( open ) throw
new AlreadyOpenException() class
AlreadyOpenException extends Exception
39and Finally
public void readFile( filename ) File myFile
new File( filename ) FileReader fr new
FileReader( myFile ) try int i
fr.read() catch ( IOException ioe )
System.err.println( Error reading file )
finally try if ( fr ! null ) fr.close()
catch ( IOException ioe )
System.err.println( Error closing stream )
40Todays lab 2-4pm Rm 202-206
Helpers
- Writing a program using fundamental Java concepts
- Using the tools from the Java Development Kit
Lin, Tanapan,David,Pete,Matthew