Previous | Home | Next |
Here are some new extra Features of Jdk1.5 as follows:
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)
Solution:
- Tell the compiler what type your collection is
- Compiler can fill in casts for you
- Guaranteed to succeed.
Generics are NOT templates:
- No code size increase
- No hideous complexity
- No "template metaprogramming"
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 classes fix the previously broken semantics of the java memory model, which defines how threads interact through memory.
Enhanced for Loop (for each):
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)
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)
Automatic conversions between primitive types (such as int) and primitive wrapper classes (such as integer).
Auto-boxing of primitive types:
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
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.
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.
Varargs 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 !!!.
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)
- 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 |