Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
The Future of Java Development with Java 8/9 Jeffrey Poore Senior Application Architect Bank of America About Me • Graduated from MIT in 1998, Bachelors in Computer Science and Engineering • Software Developer in the areas of Telephony, Finance, Health Care and Utilities • Most recently moved into a Senior Architect Role at Bank of America • Been developing in Java for over 10 years • Love Spring, Hibernate, JBoss, and IntelliJ Overview • • • • Java 1.0 – Java SE/EE 7 – A Walk Down Memory Lane Java 8 – Lambda Expressions, Method References, Stream APIs Java 9 – Modular JDK, Jshell Q/A Java 1.0 – Java SE/EE 7 • Java 1.0 (Oak) – January 23, 1996: • • • • • • • Classes, methods, basic java.lang libraries Alpha and Beta JDKs Abstract methods as in C++ (no abstract class) Private access was actually package private All exceptions unchecked Java Applets Hot Java Java 1.0 – Java SE/EE 7 • Java 1.1 – February 19, 1997: • • • • • • • Inner classes JavaBeans API Java Database Connectivity (JDBC) Java Remote Method Invocation (RMI) Reflection (introspection only) Just-in-Time Compiler (JIT) [on Windows, produced by Symantec] Internationalization and Unicode Java 1.0 – Java SE/EE 7 • J2SE 1.2 (Playground, aka Java 2) – December 8, 1998: • • • • • • strictfp keyword (portability of floating point calculations) Swing Graphical UI JIT in the Sun JVM Java Plug-in for Web Browsers Java IDL (Corba) Collections framework Java 1.0 – Java SE/EE 7 • J2EE 1.2 – December 12, 1999: • • • • • • Java Servlet API JavaServer Pages (JSP) Enterprise JavaBeans API (EJB) Java Naming and Directory Interface Specification (JNDI) Java Message Service API (JMS) Java Transaction API (JTA) Java 1.0 – Java SE/EE 7 • J2SE 1.3 (Kestrel) – May 8, 2000: • HotSpot JVM • JNDI included in core libraries • Java Platform Debugger Architecture (JPDA) – debugging framework • J2EE 1.3 – September 24, 2001: • • • • Java API for XML Processing (JAXP) JavaServer Pages Standard Tag Library (JSTL) J2EE Connector Architecture – for connecting to legacy enterprise systems Java Authentication and Authorization Service (JAAS) Java 1.0 – Java SE/EE 7 • J2SE 1.4 (Merlin) – February 6, 2002: • • • • • • • • • Java Community Process (JSR) assert keyword Regular Expressions (modeled after Perl) IPv6 Support Non-blocking I/O (NIO) Logging API Integrated security and cryptography extensions (JCE, JSSE, JAAS) Java Web Start Preferences API (java.util.prefs) Java 1.0 – Java SE/EE 7 • J2EE 1.4 – November 11, 2003: • • • • • • Web Services for J2EE Java API for XML-based RPC (JAX-RPC) JavaServer Faces (JSF) Java Authorization Service Provider Contract for Containers (JACC) Java Management Extensions (JMX) Enterprise Edition Management/Deployment APIs Java 1.0 – Java SE/EE 7 • J2SE 5.0 (Tiger) – September 30, 2004 • Version number changed to “better reflect the level of maturity, stability and security of the J2SE” • Generics • Annotations • Autoboxing • Enumerations (enum keyword) • Varargs • Enhanced for • for (String s : List<String>) { } • Static Imports • Concurrency Utilities Java 1.0 – Java SE/EE 7 • J2EE 5 – May 11, 2006: • • • • • Java Architecture for XML Binding (JAXB) SOAP with Attachments API (SAAJ) Streaming API for XML (StAX) EJB 3.0 Java Persistence API (JPA) E J B 3 Java 1.0 – Java SE/EE 7 • Java SE 6 (Mustang) – December 11, 2006: • • • • • Mucked with the versioning again (sigh) Scripting language support Java API for XML Web Services (JAX-WS) Java Compiler API – API to allow a Java program to compile code at runtime New garbage collection algorithms • Java EE 6 – December 10, 2009: • Java API for RESTful Web Services (JAX-RS) • Java APIs for XML Messaging (JAXM) • JSP Expression Language (EL) Java 1.0 – Java SE/EE 7 • Java SE 7 (Dolphin) – July 7, 2011: • • • • Oracle buys Sun (January 27, 2010) JVM support for dynamic languages Strings in switch statement Automatic resource management in try statement • try (InputStream is = openStream()) { … } • Improved type inference for Generics • List<String> lst = new ArrayList<>(); • Binary integer literals and underscores in numeric literals • 0b1111_0101_0011, 1_204_304.44 • Multiple Exception Catch • catch (IOException | NullPointerException e) { … } • Java EE 7 – June 12, 2013: • Java API for WebSocket • Java API for JSON Processing Java SE 8 – March 18, 2014 • No cute nickname, sorry • Features: • • • • • • • Lambda Expressions Method References Stream APIs Annotation on Java Types / Repeating Annotations Unsigned Integer Arithmetic New Date/Time API Removed Perm Gen • Java EE 8 still in JSR Process Lambda Expressions • A Lambda Expression is similar to a function pointer in C () -> System.out.println("Hello World!") // no parameters a -> a // identity function (a, b) -> a + b // multiple parameters (long id, String name) { … } // explicit typing (a, b) -> { return a + b; } // with a code block (id, defaultPrice) -> { // multiple lines Optional<Product> product = productList.stream().filter(p -> p.getId() == id).findFirst(); return product.map(p -> p.getPrice()).orElse(defaultPrice); } Lambda Expressions • Lambda Expressions are used where functional interfaces are expected • interfaces that only contain one abstract method • the interfaces can have default or static methods • a lambda expression is a valid return value Lambda Expressions public class Calculator { interface Operation { int execute(int a, int b); default Operation swap() { return (a, b) -> execute(b, a); } } public static int apply(int a, int b, Operation op) { return op.execute(a, b); } ... } Lambda Expressions public static void main(String … args) { Operation addition = (a, b) -> a + b; Operation subtraction = (a, b) -> a – b; println("40 + 2 = " + Calculator.apply(40, 2, addition); // "40 + 2 = 42" println("20 – 10 = " + Calculator.apply(20, 10, subtraction); // "20 – 10 = 10" println("10 – 20 = " + Calculator.apply(20, 10, subtraction.swap()); // "10 – 20 = -10" } Method References • A method reference is similar to a lambda function • Instead of providing an implementation, you provide a reference to an existing one • The method referenced doesn't have to be a part of a class that implements the given interface • only the signature of the method needs to match Method References class Person { public static int compareByAge(Person a, Person b) { return a.dateOfBirth.compareTo(b.dateOfBirth); } class PersonAgeComparator implements Comparator<Person> { public int compare(Person a, Person b) { return Person.compareByAge(a, b); } } public static void main(String … args) { Person[] people = getPeople(); Arrays.sort(people, new PersonAgeComparator()); } Method References • There has to be a better way, right? • There is: • sort has this signature: static <T> void sort(T[] a, Comparator<? super T> c); • Comparator is a functional interface, therefore a Lambda expression could be used: Arrays.sort(people, (a, b) -> return Person.compareByAge(a, b)); • But why create a lambda expression only to call to a method with the same signature? • This is what a method reference is for! Method References class Person { public static int compareByAge(Person a, Person b) { return a.dateOfBirth.compareTo(b.dateOfBirth); } public static void main(String … args) { Person[] people = getPeople(); Arrays.sort(people, Person::compareByAge); } Stream API • Stream API is an optimized way of working with Collections (List, Map, etc) • Uses Lambda Expressions and Method References • Brings Java in line with other languages that allow for quick collection processing (Perl, Python, Ruby) Without Stream API List<String> strList = getStrings(); List<String> result = new ArrayList<String>(); for (String s : strList) { if (s.startsWith("abc")) { result.add(s); } } for (int idx = 0; idx < result.size(); idx++) { result.set(idx, result.get(idx).toUpperCase()); } result.sort(new Comparator<String>() { … return StringUtil.reverseSort(a, b); … } for (String s : result) { System.out.println(s); } With Stream API List<String> strList = getStrings(); strList.stream() // opens the stream .filter(s -> s.startsWith("abc")) // filter the list .map(s -> s.toUpperCase()) // transform each item .parallel() // convert to parallel stream .sorted(StringUtil::reverseSort) // sort with existing method .forEach(s -> System.out.println(s)) // print each item Java SE 9 – TBD – Early 2017 • Codename: Jigsaw • Features: • • • • • • • • • Built-in Module System Jshell – interactive Java! Private Interface Methods Improved Process API Stackwalker Centralized Logging API Publisher/Subscriber Streams Collection Factory Methods HTTP/2 Support Module System • Some of us are familiar with the package-info.java file, which is for package level javadocs • Java 9 introduces a module-info.java file that contains module info: module com.funcompany { requires java.logging; requires java.xml; exports com.funcompany.public.api; exports com.funcompany.public.domain; } Module System • Allows modules to declare dependencies (including potentially versions, still being refined) • Allows modules to declare exported classes (i.e., code using the module will not be allowed to load private classes, even for reflection) • Module relationships are documented with Javadoc • Also allows for defining "services" for explicitly naming classes that provide service-like functionality Jshell • • • • • • Interactive Java Shell Define classes, methods, variables, etc List what is in scope (what has been defined) Call methods, assign values to variables, import types Basically a playground for Java Demo Q&A