Professional Documents
Culture Documents
GENERIC PROGRAMMING
Topics
• background and goals of generic programming
• basics of generic classes = parameterized types
• generic methods for general algorithms
• inheritance rules for generic types
• bounded type parameters
• generic code and the Java Virtual Machine
• restrictions and limitations
• wildcard types and wildcard type capture
Why generic programming
Background
• old version 1.4 Java collections were Object-based and
required the use of ugly casts
– cannot specify the exact type of elements
– must cast to specific classes when accessing
Java generics
• lets you write code that is safer and easier to read
• is especially useful for general data structures, such as
ArrayList
• generic programming = programming with classes and
methods parameterized with types
Why generic programming (cont.)
Java generics
• in principle, supports statically-typed data structures
– early detection of type violations
• cannot insert a string into ArrayList <Number>
– also, hides automatically generated casts
• superficially resembles C++ templates
– C++ templates are factories for ordinary classes and
functions
• a new class is always instantiated for given distinct generic
parameters (type or other)
• in Java, generic types are factories for compile-time
entities related to types and methods
Definition of a simple generic class
}
• when calling a generic method, you can specify type
String s = Algorithms.<String>getMiddle (names);
• but in most cases, the compiler infers the type:
String s = Algorithms. getMiddle (names);
Inheritance rules for generic types
Comments on inheritance relations
IOException
ArithmeticException
Exception AWTException
NullPointerException
RuntimeException
IndexOutOfBoundsException
Object Throwable Several more classes
IllegalArgumentException
VirtualMachineError
Error
AWTError
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
The final block is always
try { executed
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
Next statement in the
try { method is executed
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { Suppose an exception of
statement1; type Exception1 is thrown in
statement2
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { The exception is handled.
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { The final block is always
statement1; executed.
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { The next statement in the
statement1; method is now executed.
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; statement2 throws an
statement2; exception of type
Exception2.
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; Handling exception
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; Execute the final block
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; Rethrow the exception and
statement2; control is transferred to the
statement3; caller
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Creating Custom Exception Classes
• Use the exception classes in the API whenever
possible.
• Create custom exception classes if the
predefined classes are not sufficient.
• Declare custom exception classes by
extending Exception or a subclass of
Exception.
Assertions
• An assertion is a Java statement that enables
you to assert an assumption about your
program. An assertion contains a Boolean
expression that should be true during program
execution. Assertions can be used to assure
program correctness and avoid logic errors.
Declaring Assertions
An assertion is declared using the new Java
keyword assert in JDK 1.4 as follows:
assert assertion; or
assert assertion : detailMessage;
For the first assert statement with no detail message, the no-
arg constructor of AssertionError is used. For the second assert
statement with a detail message, an appropriate
AssertionError constructor is used to match the data type of
the message. Since AssertionError is a subclass of Error, when
an assertion becomes false, the program displays a message on
the console and exits.
Executing Assertions Example
public class AssertionDemo {
public static void main(String[] args) {
int i; int sum = 0;
for (i = 0; i < 10; i++) {
sum += i;
}
assert i == 10;
assert sum > 10 && sum < 5 * 10 : "sum
is " + sum;
}
}
Compiling Programs with Assertions
Since assert is a new Java keyword introduced in
JDK 1.4, you have to compile the program using
a JDK 1.4 compiler. Furthermore, you need to
include the switch –source 1.4 in the compiler
command as follows: