Java 5 SE New features

Download Report

Transcript Java 5 SE New features

EG-JUG
Java 5 Standard Edition
Amr Ali
Software Engineer
IBM-Egypt
Slides © :
Tamer Mahfouz
Architect
IBM-Egypt
Tamer Mahfouz
7/17/2015
EG-JUG
Session Objective and/or Brief Description
 Provide an introduction to the new features in
Java 5 Standard Edition and some examples,
including:
– Language changes
– JVM changes
– Performance enhancements
– Base library changes
– Miscellaneous changes
2
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Agenda (1:15 hour)
•
Overview of Java 5 new features
• Language enhancements
•
•
•
•
•
•
•
•
Generics
Enhanced for loop
Autoboxing/Unboxing
Enumerations
Varargs
Static Import
Metadata (Annotations)
JVM and Performance enhancements
• Base libraries and miscellaneous enhancements
• Examples
• Future
•
•
•
3
Java 5 EE
Java 6 (Mustang)
Java 7 (Dolphin)
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Roles Recommended to Take the Course
 This session is intended for EGJUG members,
who know J2SE 1.4 and are interested in
understanding Java 5 SE
4
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Pre-Requisites
 Recommended Knowledge of J2SE 1.4
5
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Java 5
 Sun has remove the "2" from all names. J2SE will be Java SE,
J2ME becomes Java ME, and J2EE will become Java EE.
 There is no Java 5.1, the next major release is 6.0, but there will be
5.0 updates
 The timeline for feature releases will be 18-24 months. Intermediate
releases will be bug fix updates.
 There have been more than 100,000,000 downloads of the J2SE 5.0.
 The greatest major changes in Java since its release.
 Main Design targets:
– compatibility, stability, quality
– ease of development
6
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Java Versions
Oak: Designed for embedded devices
pre-Java
Java: Original, not very good version (but
it had applets)
Java 1
JDK 1.1: Adds inner classes and a completely new
event-handling model
JDK 1.2: Includes “Swing” but no new syntax
JDK 1.3: Additional methods and packages, but no
new syntax
JDK 1.4: More additions and the assert statement
Java 2
JDK 1.5: Generics, enums, new for loop,
and other new syntax
Java 5.0
7
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Overview of Java 5 new features
 Language enhancements
– Generics (C++ templates)
– Enhanced for loop (for-each)
– Autoboxing/Unboxing (primitive types as objects)
– Enumerations (type-safe enums)
– Varargs (variable arguments for methods)
– Static Import (static calls without class reference)
– Metadata (Annotations)
 JVM and Performance enhancements
 Base libraries and miscellaneous enhancements
8
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Language enhancements
9
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Generics
 Similar to C++ templates
 Used mainly for type-safe collections
 Less typecasts
 Compile-time vs. run-time type checking
 Details in a follow-on session
10
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Generics – cont.

JDK 1.4:
List strList = new ArrayList();
strList.add(”Test”);
removeConditional(strList);
static void removeConditional(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); )
if (((String) i.next()).length() == 4)
i.remove();
}

Java 5:
List<String> strList = new ArrayList<String>();
strList.add(”Test”);
removeConditional(strList);
static void removeConditional(Collection<String> c) {
for (Iterator<String> i = c.iterator(); i.hasNext(); )
if (i.next().length() == 4)
i.remove();
}
11
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Enhanced for loop
 Eliminates the drudgery and error-proneness of iterators and index
variables when iterating over collections and arrays
void cancelAll(Collection<TimerTask> c) {
for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); )
i.next().cancel();
}
void cancelAll(Collection<TimerTask> c) {
for (TimerTask t : c)
t.cancel();
}
 Applies to arrays
12
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Autoboxing/Unboxing
 Eliminates the problems of manual conversion between
primitive types (such as int) and wrapper types (such as
Integer)
List <Integer> lstInt = new ArrayList<Integer>();
lstInt.add(5);
int x = lstInt.get(0);
 Note the performance costs
13
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Enumerations
 Old style (JDK 1.4):
Public interface Season {
public static final int SEASON_WINTER = 0;
public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL
= 3;
}
14

Not typesafe - Since a season is just an int you can pass in any other int value where a season is
required, or add two seasons together (which makes no sense).

No namespace - You must prefix constants of an int enum with a string (in this case SEASON_) to
avoid collisions with other int enum types.

Brittleness - Because int enums are compile-time constants, they are compiled into clients that use
them. If a new constant is added between two existing constants or the order is changed, clients
must be recompiled. If they are not, they will still run, but their behavior will be undefined.

Printed values are uninformative - Because they are just ints, if you print one out all you get is a
number, which tells you nothing about what it represents, or even what type it is.
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Enumerations
 New Java 5 style:
enum Season { WINTER, SPRING, SUMMER, FALL }
 Allows you to create enumerated types with arbitrary
methods and fields
 It provides all the benefits of the Typesafe Enum pattern
 “enum” is now a reserved word
 enums contains toString() for display purposes
 Iteration on all values
for (Season season : Season.values())
System.out.println(season.toString());
15
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
More on Enums
 Base class java.lang.Enum
 Can add normal methods, possibly abstract ones
 Can’t extend classs, but can implement interfaces
 Example
enum Somethings implements Xyz{
X
{
},
public abstract Something getSomething(){
return newSomething();
}
Y
{
};
public abstract Something getSomething(){
return newSomething();
}
public abstract Something getSomething();
}
16
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Varargs
 Used for variable number of arguments in a method call
 JDK 1.4 style:
public String format(String pattern,
Object[] arguments);
Object[] arguments = { new Integer(7), new Date(), “a disturbance
in the Force” };
String result = messageFormat.format("on {1} there was {2} on
planet {0}", arguments);
 Java 5 style:
public String format(String pattern,
Object... arguments);
String result = MessageFormat.format("on {1}, there was {2} on
planet {0}" new Integer(7), new Date(), “a disturbance in the
Force”);
17
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Static Import
 To access static members, it is no longer necessary to qualify
references with the class they came from
double r = Math.cos(Math.PI * theta);
 In Java 5:
import static java.lang.Math.PI;
or
import static java.lang.Math.*;
double r = cos(PI * theta);
18
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Metadata (Annotations)

Avoids boilerplate code

Deployment descriptors is no longer needed

Available in the binary .class

JDK 1.4
/**
* @Copyright("2002 Yoyodyne Propulsion Systems")
*/
public class OscillationOverthruster { ... }

Java 5
@Copyright("2002 Yoyodyne Propulsion Systems")
public class OscillationOverthruster { ... }
// Associates a copyright notice with the annotated API element.
public @interface Copyright {
String value();
}
19
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Metadata (Annotations) - cont
/**
* Indicates that the specification of the annotated API element
* is preliminary and subject to change.
*/
public @interface Preliminary { }
@Preliminary public class TimeTravel { ... }
20
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
JVM and Performance enhancements
 Class Data Sharing
– Reduces application startup time and footprint
 Server-Class Machine Detection
 Garbage Collector enhancements
 High-Precision Timing Support
– System.nanoTime() has been added
 StringBuilder class (unsynchronized StringBuffer)
 Java 2D performance enhancements
 Image I/O performance enhancements
21
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Base libraries and miscellaneous enhancements
 Lang and Util Packages
– Collections Framework
– ProcessBuilder (modified sub-process environment)
– Formatter
– Scanner
 Networking
 Security
 Internationalization
 Improved Support for Environment Variables
22
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Base libraries and miscellaneous enhancements – cont.
 Reflection
 Java API for XML Processing (JAXP) enhancements
– JSR 206 (DOM Level 3 and SAX 2.0.2)
 Bit Manipulation Operations
 Math
 Java Database Connectivity (JDBC)
– RowSet five standard implementations
 Java Naming and Directory Interface (JNDI)
 OS & Hardware Platforms
– 64-Bit AMD Opteron Processors
23
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Examples
 JDK 1.4
Map map = new HashMap();
map.put(new Integer(1), "first");
map.put(new Integer(2), "second");
...
String val = (String) map.get(new Integer(2));
 Java 5
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "first");
map.put(2, "second");
...
String val = map.get(2);
24
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Examples – cont.
Auto-Unboxing:
Boolean canMove = new Boolean(true);
...
if(canMove){
System.out.println("This code is legal in Java 5!");
}
Static import:
import static java.lang.System.out;
...
out.print("stuff");
out.print("more stuff");
25
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Examples – cont.
 Enumerations:
Season season;
...
if (season == Season.WINTER) {
...
}
switch(season) {
case WINTER:
...
case SPRING: ...
case SUMMER: ...
case FALL: ...
}
26
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Future
 Java 5 EE
 Java 6 (Mustang)
 Java 7 (Dolphin)
27
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Java 5 EE
 Specs developed under the Java Community Process - JSR-000244
 New Features:
– EJB 3.0
• New Persistence mechanism for Entity EJBs (POJO)
• No deployment descriptors needed (using Metadata annotations)
– JavaServer Faces
– Other enhancements
 Timeline
– August 2005 - Proposed final draft
– Q4 2005 - beta release
– Q1 2006 - Final release
 GlassFish project
28
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Java 6 (Mustang)

JSR 105: XML Digital Signature http://jcp.org/en/jsr/detail?id=105

JSR 199: Java Compiler API http://jcp.org/en/jsr/detail?id=199

JSR 202: Java Class File Specification Update http://jcp.org/en/jsr/detail?id=202

JSR 221: JDBC 4.0 http://jcp.org/en/jsr/detail?id=221

JSR 222: JAXB 2.0 http://jcp.org/en/jsr/detail?id=222

JSR 223: Scripting for the Java Platform http://jcp.org/en/jsr/detail?id=223

JSR 224: JAX-RPC 2.0 http://jcp.org/en/jsr/detail?id=224

JSR 260: Javadoc Tag Update http://jcp.org/en/jsr/detail?id=260

JSR 268: Java Smart Card I/O API http://jcp.org/en/jsr/detail?id=268

JSR 269: Pluggable Annotation Processing API http://jcp.org/en/jsr/detail?id=269

JSR TBD: JAXP.next
 Timeline
– Early Draft - April 2005
– Public Review - Late 2005
– Final Release - Mid 2006
29
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Java 7 (Dolphin)
 The following features are under consideration:
– direct xml support
– friends for cross package refs
– method references
– JVM bytecodes for dynamic languages
– beanshell scripting language
– new IO APIs
– Beyond future
• One platform, many languages
30
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
References
 http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html
 http://java.sun.com/j2se/1.5.0/docs/index.html
 http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
 http://jcp.org/aboutJava/communityprocess/pfd/jsr244/
31
© Tamer Mahfouz 2005
7/17/2015
EG-JUG
Questions?
32
© Tamer Mahfouz 2005
7/17/2015