Previous | Home | Next |
-
Generics: provides compile-time (static) type safety for collections and eliminates the need for most typecasts (type conversion).
Problem:
- Collection element types
- Cannot be checked at compile time
- Assignment must use cast
- Can cause runtime errors ( ClassCastException)
- Tell the compiler what type your collection is
- Compiler can fill in casts for you
- Guaranteed to succeed.
Generics Example
Old code:- List l = new LinkedList(); l.add(new Integer(0)); Integer i = (Integer)l.iterator.next(); New code:- List<Integer> l = new LinkedList<Integer>(); l.add(new Integer(0)); Integer i = l.iterator.next();
Generics are NOT templates
- No code size increase
- No hideous complexity
- No “template metaprogramming”
-
Enhanced for each loop: the for loop syntax is extended with special syntax for iterating over each member of either an array or any iterable, such as the standard collection classesfix the previously broken semantics of the java memory model, which defines how threads interact through memory.
Problem:
- Iterating over collections is tricky
- Often, iterator only used to get an element
- Iterator is error prone (Occurs three times in a for loop)
- Can produce subtle runtime errors
Solution:
- Let the compiler do it
- New for loop syntax
- for (variable : collection)
Enhanced for loop example
Old code:- void cancelAll(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) { TimerTask task = (TimerTask)i.next(); task.cancel(); } } New Code:- void cancelAll(Collection<TimerTask> c) { for (TimerTask task : c) task.cancel(); } Also works for arrays
-
Metadata: also called annotations; allows language constructs such as classes and methods to be tagged with additional data, which can then be processed by metadata-aware utilities.
Problem:
Some APIs require lots of standard code
How to indicate this to a tool
Solution:
Annotated source code
e.g. @remote getPrice(Product p)
-
Auto boxing/unboxing: Automatic conversions between primitive types (such as int) and primitive wrapper classes (such as integer).
Problem:
Conversion between primitive types and
wrapper objects (and vice-versa)
Needed when adding primitives to a collection
Solution:
Let the compiler do it Integer intObj = 22; // Boxing conversion int i = (int)intObj // Unboxing conversion ArrayList al = new ArrayList(); al.add(22); // Boxing conversion
-
Enumerations: the enum keyword creates a typesafe, ordered list of values (such as day.monday, day.tuesday, etc.). Previously this could only be achieved by non-typesafe constant integers or manually constructed classes (typesafe enum pattern).
Typesafe Enumerations
Problem:
Variable needs to hold limited set of values
e.g. Card suit can only be Spade, Diamond, Club, Heart
Solution:
New type of class declaration
enum type has public, self-typed members for
each enum constant
new keyword, enum
works with switch statement
Enumeration Examples
public enum Coin { penny(1), nickel(5), dime(10), quarter(25); Coin(int value) { this.value = value; } private final int value; public int value() { return value; } }
-
Var args: the last parameter of a method can now be declared using a type name followed by three dots (e.g. Void drawtext(string... Lines)). In the calling code any number of parameters of that type can be used and they are then placed in an array to be passed to the method, or alternatively the calling code can pass an array of that type.
Problem:
To have a method that takes a variable number of parameters
Can be done with an array, but not nice Look at java.text.MessageFormat
Solution: Let the compiler do it for you
New syntax:
public static String format(String fmt,Object... args); Java gets printf !!!
-
Static imports
Problem:
Having to fully qualify every static
Referenced from external classes
Solution:
New import syntax
import static TypeName . Identifier ;
import static Typename . * ;
Also works for static methods and enums
e.g Math.sin(x) becomes sin(x)
-
Concurrency Utilities
Goal: Beat C performance in high end
server side applications
New framework for locks to provide greater
flexibility over synchronized
No more threads, use Executors
Use anExecutor.execute(aRunnable)
Not new Thread(aRunnable).start();
Runnable and Callable
Callable for things that return values and/or exceptions
Previous | Home | Next |