COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

void doIt(int x, double... values) { //do something } ... doIt(10, d1); doIt(10, d1, d2, d3, d4); //Java treats the variable-length list of arguments ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 20
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 31-Jan-08
  • Lecture Set 7
  • Assignment 2 Solutions, JavaDocs, OO programming
    cont., New array features

2
Assignment 2 Solutions
  • Solutions will be discussed in class

3
JavaDocs
  • See JavaDocs notes
  • and in class discussion

4
Classes and Objects Composition
  • Composition when a class has reference to
    objects of other classes as it members (or
    fields).
  • Also referred to as a has-a relationship

5
Classes and Objects The object life cycle
  • We have already studied object creation and
    initialization.
  • What about the end of the object life cycle?
  • How are objects finalized and destroyed?

6
Finalization / Destruction
  • Finalization is the opposite of initialization
  • In Java, Memory occupied by object is
    automatically reclaimed when object is no longer
    needed
  • Process is called garbage collection
  • Not something specific to Java has been done in
    many other languages such as Lisp
  • Essentially an automated version of calling the
    free() method or the delete operator in languages
    such as C and C.
  • You dont have to remember to destroy every
    object you create --- makes Java easier to work
    with.

7
Garbage Collection How it works
  • Java interpreter (JVM) knows what objects and
    arrays it has allocated.
  • Checks to see which local variables refer to
    which objects/arrays (and which objects/arrays
    refer to which other objects/arrays)
  • Interpreter determines when an allocated object
    is no longer referenced by another object or
    variable
  • When such an object is found, object can be
    safely destroyed.

8
Garbage Collection How it works cont.
  • Garbage collector runs as low-priority thread
  • Does majority of work during idle time
  • Will run against a high-priority thread if it
    detects dangerously low available memory
  • Rare, as the low-priority thread is generally
    able to clean things up in the background.

9
Garbage Collection Efficiency
  • The idea of garbage collection seems slow, memory
    intensive
  • This was the case with early garbage collectors
  • Modern garbage collectors are efficient
  • Nothing is as efficient as a well-written program
    with explicit memory allocation and deallocation.

10
Memory Leaks
  • Occurs when memory is allocated but never
    reclaimed
  • Garbage collection reduces the incidence of these
    bugs
  • but it does not prevent all memory leaks.
  • What is a valid (but unused) reference to an
    unused object is left hanging around?

11
Memory Leaks
  • Simple example
  • Array is created in main method
  • Array is used, and then is no longer needed
  • GC cant reclaim memory until method returns
  • but main method doesnt return
  • So we have to kill the reference to the array
    to make the GC reclaim the memory.
  • See SimpleMemoryLeak.java

12
Object Finalization
  • Garbage collection automatically frees up memory
    resources used by objects
  • but what about other resources?
  • Open file handles
  • Network connections
  • Etc
  • You must declare a finalizer method to handle
    these tasks

13
Finalizers
  • There can only be one finalizer per class
  • Must be named finalize()
  • Takes no arguments
  • Returns no value
  • Typically declared protected, but can be declared
    public
  • Should include any code needed to free up the
    resources that were in use by the object

14
Finalizers
  • If an object has a finalizer, it will be invoked
    sometime after the object becomes unused (but
    before the GC reclaims the object).
  • As with GC, there is no guarantee about when the
    object will be finalized.
  • Java interpreter can exit without GC, and thus
    without calling all finalizers
  • In this case, it is up to the OS to free
    outstanding resources.

15
New array features as of Java 1.5
  • Some new features were added in Java 1.5.0 that
    deal with arrays
  • Enhanced for loops
  • Variable-length parameter lists

16
Enhanced for loops
  • Enhanced for loops
  • Allow you to iterate through an array without
    using a counter
  • Format
  • for (paramter arrayName)
  • loop body

17
Enhanced for loops example
  • int values new int10
  • int total 0
  • //Traditional loop
  • for (int i 0 I lt values.length i)
  • total total valuesi
  • //Enhanced for loop
  • for (int number values)
  • total total number

18
Variable-length Parameter Lists
  • Allows a method to have an unspecified number of
    arguments (of a specified type)
  • Use ellipsis () following the type specified
  • Can only be used once, and must be the last
    argument in the parameter list

19
Variable-Length Parameter Lists Example
  • void doIt(int x, double... values)
  • //do something
  • //The above method can be called by with the
  • //following
  • doIt(5, 3.14, 5,5)
  • double d1, d2, d3, d4
  • doIt(10, d1)
  • doIt(10, d1, d2, d3, d4)
  • //Java treats the variable-length list of
    arguments
  • //as an array. So, the values arguments is really
  • //double values
Write a Comment
User Comments (0)
About PowerShow.com