Programming - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Programming

Description:

Wrapping up the power of: Java language. Java Virtual Machine. Java file format. Java API ... At least one class must have a main method with this signature ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 41
Provided by: robertc101
Category:

less

Transcript and Presenter's Notes

Title: Programming


1
Programming
  • Aris Papadopoulos
  • ap7_at_doc.ic.ac.uk

2
Course 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

3
Course Outline
4
Materials
  • http//www.doc.ic.ac.uk/ap7/java.html

5
Why Java?
6
Compiling C Programs
include ltstdiogtmain( int argc, char argv)
// do something
7
Java Architecture
  • Wrapping up the power of
  • Java language
  • Java Virtual Machine
  • Java file format
  • Java API

8
The Java Virtual Machine
javac
9
The 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
10
The Java Virtual Machine
class Hello public static void main() //
do something
Mac JVM
Linux JVM
Win JVM
11
The Java Virtual Machine
  • One at a time
  • Just-in-time compiler
  • Adaptive optimizer

JVM
Class loader
bytecodes
Execution engine
12
Memory 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
13
Memory Management (C)
int main( int argc , char argv ) for( int
i0 ilt100 i ) Dog dp new
Dog(sirius) dp-gtbark()
14
Memory 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
15
Memory 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
16
Memory Management (Java)
public static void main( String args ) for(
int i0 ilt100 i ) Dog dp new
Dog(sirius) dp.bark()
17
Garbage 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.

18
A 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)
19
More Classes
  • Programs comprise sets of classes
  • Classes can have
  • Fields
  • Methods
  • Constructors

20
A 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
21
Some other Animals
Abstract class
Dog
Cat
Fish
Bird
Group classes in a package
22
Some 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
23
Some 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
24
Using 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
25
Using 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()
26
Access 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
27
Class member access
28
Java has Single Inheritance
What if we want them both to be Performers?
29
Interfaces
Human
Animal
Interface implementation
Elephant
RingMaster
Inheritance -RingMaster extends Human
30
Performer Interface
Interfaces define named types
public interface Performer public void
perform()
No body for the method given
Interfaces define sets of methods
31
Implementing 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
32
Using 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
33
Exceptions
  • Sometimes a method may fail
  • Exceptional behaviour
  • Catch and handle this special case
  • Methods can throw an Exception to signal that
    something is wrong

34
Exceptions
Throwable
Error
Exception
IOException
RuntimeException
35
Documentation 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.
36
Try 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. )

37
Alternatively, Throw
class Test public void readFile() throws
FileNotFoundException
File myFile new File( filename )
FileReader fr new FileReader( myFile )
38
Throwing your own exceptions
class Test public void unlockDoor()
throws
AlreadyOpenException if ( open ) throw
new AlreadyOpenException() class
AlreadyOpenException extends Exception
39
and 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 )

40
Todays 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
Write a Comment
User Comments (0)
About PowerShow.com