Philly Java Users Group What’s new in What’s new in Java 2 Standard Edition 1.4 Java 2 Standard Edition 1.4 Kirk k B. Spadt padt ki kirk@ k@spadt spadt.com com
Java 2 Standard Edition 1.4 Java 2 Standard Edition 1.4 • Availability: F Availability: February 13, 2002 ebruary 13, 2002 • Size: Over 11,000 classes (API’s + impl) Size: Over 11,000 classes (API’s + impl) • Changes: 33% Changes: 33% new or changed methods new or changed methods • Performance: Major enhancements Performance: Major enhancements • Functionality: Enterprise connectivity unctionality: Enterprise connectivity • Scalability: 64-bit core support Scalability: 64-bit core support • Ease of programming: More API’s Ease of programming: More API’s • Ease of use: Drag/drop, wheel, install Ease of use: Drag/drop, wheel, install • Compatibility with earlier releases Compatibility with earlier releases
Perform Performance Enhancem ance Enhancements ents • 50-100% 50-100% I/O improvement I/O improvement • 10x Reflection performance increase 10x Reflection performance increase • Garbage collection - concurrent (MP) Garbage collection - concurrent (MP) • 30% 30% Swing improvement (pipeline) Swing improvement (pipeline) • Hardware acceleration (DirectDraw) Hardware acceleration (DirectDraw) • 20% 20% reduction in RAM footprint reduction in RAM footprint • 20% 20% reduction in startup time reduction in startup time
API’s M API’s Moved into J2SE v1.4 oved into J2SE v1.4 • JAXP: Java API for XML Processing JAXP: Java API for XML Processing • JDBC-2, 2.1 and JDBC 3.0 JDBC-2, 2.1 and JDBC 3.0 • CORBA enhancements CORBA enhancements • DNS Service Provider for JNDI DNS Service Provider for JNDI • JCE: Java Cryptography Extensions JCE: Java Cryptography Extensions • JSSE: Java Secure Socket Extension JSSE: Java Secure Socket Extension • JAAS: Java Authentication/Authorization JAAS: Java Authentication/Authorization • GSS-API: Kerberos Single sign-on GSS-API: Kerberos Single sign-on
New Functions New Functions • Assertions • XML Persistence Assertions XML Persistence • Regular expressions • Fast 2D Graphics Regular expressions ast 2D Graphics • New I/O API • Redesigned focus New I/O API Redesigned focus • Printing API • Swing drag-n-drop Printing API Swing drag-n-drop • Logging API Logging API • Java Web Start Java Web Start • Preferences API Preferences API
Code Disclaim Code Disclaimer er • Subsequent sections include a Subsequent sections include a sample sample pro program gram to illustrate use to illustrate use of the new of the new classes. classes. • Written to be BRIEF Written to be BRIEF (single slide) (single slide) The code works…, but… he code works…, but… Ignores exceptions Ignores exceptions Breaks MVC & some accepted practices Breaks MVC & some accepted practices • Code is AS Code is AS-IS IS, and , and use in nuclear facilities use in nuclear facilities or life-support applications is prohibited. or life-support applications is prohibited.
Assertions Assertions • Tests a condition you “know” is true ests a condition you “know” is true Confirms programmer assumptions Confirms programmer assumptions Improves program reliability Improves program reliability • assert : New Java language keyword : New Java language keyword javac –source 1.4 Foo.java to compile to compile java –ea Foo to run to run • throws an AssertionError throws an AssertionError java.lang.Error subclass (unchecked) java.lang.Error subclass (unchecked)
package com.spadt.jdk14; /** * Demonstrates the use of the assert keyword. */ public class Assert { public static void countChars(String s) { assert s != null : "string cannot be null"; System.out.println("String [" + s + "] has " + s.length() + " chars."); } public static void main(String[] args) { countChars("valid string"); countChars(null); } }
Compiling: Assertions disabled >javac com\spadt\jdk14\Assert.java com\spadt\jdk14\Assert.java:7: warning: as of release 1.4, assert is a keyword, and may not be used as an identifier assert s != null : "string cannot be null"; ^ com\spadt\jdk14\Assert.java:7: ';' expected assert s != null : "string cannot be null"; ^ com\spadt\jdk14\Assert.java:7: cannot resolve symbol symbol : class assert location: class com.spadt.jdk14.Assert assert s != null : "string cannot be null"; ^ com\spadt\jdk14\Assert.java:7: s is already defined in countChars(java.lang.String) assert s != null : "string cannot be null"; ^ Compiling: Assertions enabled
Running: Assertions disabled >java com.spadt.jdk14.Assert String [valid string] has 12 chars. Exception in thread "main" java.lang.NullPointerException at com.spadt.jdk14.Assert.countChars(Assert.java:8) at com.spadt.jdk14.Assert.main(Assert.java:13) Running: Assertions enabled >java -ea com.spadt.jdk14.Assert String [valid string] has 12 chars. Exception in thread "main" java.lang.AssertionError: string is null at com.spadt.jdk14.Assert.countChars(Assert.java:7) at com.spadt.jdk14.Assert.main(Assert.java:13)
Assertions Strategy Assertions Strategy • YES: Programmer errors or omissions YES: Programmer errors or omissions if() … else if() … else assert if() … else if() … else assert switch(x) … case 1: … default: assert switch(x) … case 1: … default: assert verifying ownership of object locks verifying ownership of object locks how could I ever get here? how could I ever get here? • NO: User, system, or configuration errors NO: User, system, or configuration errors User entry errors – detailed message User entry errors – detailed message SQL, Remote, etc errors – log them SQL, Remote, etc errors – log them Config errors – log for deployers Config errors – log for deployers
Regular Expressions Regular Expressions • Powerful tools to match and process text. Powerful tools to match and process text. • Familiar to unix shell and perl programmers amiliar to unix shell and perl programmers • Matching, extraction, and substitution Matching, extraction, and substitution • Examples for matches: Examples for matches: “[0-9]+” – matches one or more digits [0-9]+” – matches one or more digits “dog|cat” – matches either “dog” or “cat” dog|cat” – matches either “dog” or “cat” (.* (.*)\r?\n” – the (.* )\r?\n” – the (.*) matches a text line ) matches a text line “[a-zA-Z [a-zA-Z][a-zA-Z ][a-zA-Z_0-9]* _0-9]*” – a java name ” – a java name
Regular Expression Classes Regular Expression Classes • java.util.regex: New ew package package • Pat atter ern n cl class: ass: speci specify y a a regul egular ar expr expressi ession on mat atches( ches(): an an easy easy test est for or a a mat atch ch spl split(): br breaks eaks a a st string ng usi using ng del delimiter ers Com ompi pile( e(): Par arse se expr expr ahead ahead - per perfor ormance ance • Mat atche cher class: s: mat atch ch and nd par arse se text ext usi using ng the he regul egular ar expr expressi ession on mat atches( ches(): a a fast ast test est for or a a mat atch ch find( nd(): test ests s if text ext cont contai ains ns pat patter ern st star art(), end( end() – – indexes ndexes of of mat atch ch gr group( oup() – – a a st string ng or or subst substring ng that hat mat atched ched
package com.spadt.jdk14; import java.io.*; import java.util.regex.*; /** Cool stuff to do with java.util.Regex */ public class RegexTricks { public static void main(String[] args) throws IOException { String info = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Pattern emailPattern = Pattern.compile("(.+)@(.+\\.(com|net|org))"); Matcher em = emailPattern.matcher(""); while((info = in.readLine()).length() > 0) { if(Pattern.matches("[0-9]*", info)) { System.out.println("Number=" + info); } else if(em.reset(info).matches()) { System.out.println("User=" + em.group(1) + ", host=" + em.group(2) + ", a dot-" + em.group(3)); } else { String[] parts = Pattern.compile("[.,]").split(info); for(int i = 0; i < parts.length; i++) System.out.println("Part " + i + "=" + parts[i]); } } }
from public class RegexTricks Pattern emailPattern = Pattern.compile("(.+)@(.+\\.(com|net|org))"); Matcher em = emailPattern.matcher(""); while((info = in.readLine()).length() > 0) { if(Pattern.matches("[0-9]*", info)) { System.out.println("Number=" + info); } else if(em.reset(info).matches()) { System.out.println("User=" + em.group(1) + ", host=" + em.group(2) + ", a dot-" + em.group(3)); } else { String[] parts = Pattern.compile("[.,]").split(info); for(int i = 0; i < parts.length; i++)
Recommend
More recommend