Java 1'5 and Advanced Features - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Java 1'5 and Advanced Features

Description:

public static final int FAULT = 4; private int mode = OFF; Enumerated Types ... FAULT(4); private int errorCode; public FurnaceMode (int code) { errorCode = code; ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 15
Provided by: Robert9
Category:

less

Transcript and Presenter's Notes

Title: Java 1'5 and Advanced Features


1
Java 1.5 and Advanced Features
  • CS2335
  • Fall 2005

2
Generics
  • Problem How can we have type-safe data
    structures and methods, when we do not know what
    types programmers might invent later?
  • Solution Use placeholders for types that get
    filled in and expanded at compile time.

3
Generics Example
  • Old Way
  • Method 1
  • List myList new ArrayList()
  • What can I put in myList? Any Object!!!!
  • Player p (Player) myList.get()
  • Method 2 (Try to make safe)
  • public class PlayerList
  • List players
  • Player get( )
  • return (Player) players.get( )

4
Generic Collections
  • New
  • ListltPlayergt players new ArrayListltPlayergt()
  • Now we can only put players in the list.
  • players.add(new Player()) //OK
  • players.add(new String()) //Compile Error
  • Player p players.get( ) //no cast needed!

5
Can create our own generics
public class PairltX, Ygt private X first
private Y second public Pair (X fst, Y
scnd) first fst second scnd public
X getFirst( ) return first public Y
getSecond() return second PairltString,
intgt pair new PairltString, intgt(Bob, 34)
6
Improved For loop
  • To iterate over collections required us to use a
    formulaic for loop or while loop that might be
    error prone.
  • Old way
  • Iterator iter players.iterator()
  • while (iter.hasNext())
  • Player p (Player) iter.next()
  • for (int i 0 i lt array.length i)

7
Improved For Loop
for (Player p players )
p.doSomething() Works for arrays
too!!! int scores new int20 for ( int i
scores) total i
8
Enumerated Types
  • We have constants and special categories of
    things, and we want to treat them in a type safe
    manner, not just as integer codes. We want to
    stop an invalid code from being accepted.
  • Old Way
  • class Furnace
  • public static final int AUTOMATIC 1
  • public static final int OFF 2
  • public static final int MANUAL 3
  • public static final int FAULT 4
  • private int mode OFF

9
Enumerated Types
  • public enum FurnaceMode
  • OFF, AUTOMATIC, MANUAL, FAULT
  • class Furnace
  • private FurnaceMode mode
  • public void doSomething()
  • switch(mode)
  • case AUTOMATIC

10
But Wait, there is more
  • We can put behavior IN the enumerated type
  • public enum FurnaceMode
  • OFF (1),
  • AUTOMATIC (2),
  • MANUAL (3),
  • FAULT(4)
  • private int errorCode
  • public FurnaceMode (int code) errorCode
    code
  • public int value()
  • switch (this)
  • case OFF return errorCode

11
Static Initializer Blocks
  • We need to be able to initialize static data, but
    it requires more complex initialization than a
    simple assignment.
  • Old Way
  • public static final int RATE MyClass.initRate()

12
Static Initialization Blocks
  • New Way
  • public MyClass
  • public static final int RATE
  • static
  • TaxTable tt new TaxTable()
  • for (int i0 i lt 100 i)
  • tt.iterate()
  • RATE tt.getResult()

13
Instance Initializer
  • I need to initialize an instance variable, and I
    would like to put the code near the variable, not
    off in a constructor. Used most often to
    initialize arrays.

private int values new int13 for (int i
0 i lt values.length i) valuesi i
14
Reflection
  • Sometimes we need to manipulate objects and
    classes at runtime rather than at compile time.
    What if I know a game is called VideoPoker,
    and I want to make a class using that string, at
    Runtime?
Write a Comment
User Comments (0)
About PowerShow.com