Java 7 Tomasz Wróbel
Schedule <ul><li>Java SE 6 (December 11, 2006) </li></ul><ul><li>2010/12/23 Feature Complete (M11) </li></ul><ul><li>2011/...
Project Coin <ul><li>Strings in switch </li></ul><ul><li>Binary integral literals </li></ul><ul><li>Underscores in numeric...
Binary literals <ul><li>integral types ( byte ,  short ,  int , and  long ) can also be expressed using the binary number ...
Underscores in Numeric Literals <ul><li>long creditCardNumber = 1234_5678_9012_3456L </li></ul><ul><li>long phoneNo = 0031...
Type Inference for Generic Instance Creation <ul><li>Map<String, Integer> jdk6map    = new HashMap<String, Integer>(); </l...
Catching Multiple Exception Types <ul><li>try { </li></ul><ul><li>Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInst...
twr - The try-with-resources Statement <ul><li>try (Connection connection =   DriverManager.getConnection(url, &quot;root&...
NIO.2 <ul><li>java.nio.file  package </li></ul><ul><ul><li>Path </li></ul></ul><ul><ul><li>Files </li></ul></ul><ul><ul><l...
java.nio.file.Files
java.nio.file.WatchService <ul><li>ENTRY_CREATE </li></ul><ul><li>ENTRY_DELETE </li></ul><ul><li>ENTRY_MODIFY </li></ul>
JSR 292 - Support for dynamically-typed languages <ul><li>Java </li></ul><ul><li>String s = &quot;Hello World&quot;; </li>...
invokedynamic <ul><li>invokedynamic </li></ul><ul><li>+ </li></ul><ul><li>method handle </li></ul><ul><li>= </li></ul><ul>...
Method handles <ul><li>java.lang.invoke.MethodHandle </li></ul><ul><li>Provide the linkage mechanism that enables </li></u...
Other benefits of JSR 292 <ul><li>code coverage tools </li></ul>
A lightweight fork/join framework <ul><li>if (my portion of the work is small enough) </li></ul><ul><li>do the work direct...
New Classes, Interfaces and Enums <ul><li>hundreds of changes to API </li></ul><ul><li>java.util.Objects </li></ul><ul><ul...
URLClassLoader.close() <ul><li>A method that frees the underlying resources, such as open files, held by a  URLClassLoader...
Beyond <ul><li>JAVA 8 is expected in late 2012 </li></ul><ul><li>Project Lambda - Lambda expressions aka &quot;closures&qu...
Resources <ul><li>JDK 7 - http://wikis.sun.com/display/JDK7/Home </li></ul><ul><li>http://openjdk.java.net/projects/nio/ <...
Upcoming SlideShare
Loading in …5
×

Java 7

3,067 views
2,984 views

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,067
On SlideShare
0
From Embeds
0
Number of Embeds
155
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Hi, I would like to highlight what&apos;s coming in Java 7. Java 7 has codename Dolphin. That&apos;s the longest release ever. It was eventually split into two parts. And the second part will become Java 8.
  • Java 6 was release in December 2006. It took 4 years to meet feature complete release. That&apos;s where list of features is closed. Last scheduled milestone is in June. By then all bugs should be fixed. And Java 7 should be generally available in late July.
  • The goal of Project Coin was to determine what set of small language changes should be added to JDK 7. There was an open call for proposals that ran in March 2009. Out of nearly 70 proposal few were chosen. Strings in switch statements described here if time allows.
  • In Java 7 the integral types can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number. I already see people using it during interviews :)
  • In Java 7 any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code. Think of a credit card or telephone number.
  • In Java 7 you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (&lt;&gt;). However this will only be possible if compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond operator. It will certainly reduce the noise in the Java code.
  • In Java 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.
  • The try-with-resources statement is a try statement that declares one or more resources. And a resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements new AutoCloseable interface can be used as a resource. There are 50 interfaces extending AutoCloseable interface - important ones are Connection , ResultSet and Statement (as part of JDBC 4.1) There are over 100 classes implementing AutoCloseable interface - all kind of Streams
  • In Java 7 we will know not only about files and directories but also about symbolic links, file attributes, permissions, owner, etc. The Path is a main class and has methods for manipulating a path in the filesystem. The FileSystem class has a variety of methods for obtaining information about the file system. Java 7 brings a fully-functional filesystem provider for zip and jar archives, which you will be able to treat as files system. A Path also defines the resolve and resolveSibling methods to combine paths. The relativize method that can be used to construct a relative path between two paths. Paths can be compared, and tested against each other using the startsWith and endWith methods.
  • The Files class has methods for file operations, such as move, copy, delete, and also methods for retrieving and setting file attributes. This class offers also a rich set of static methods for reading, writing, and manipulating files and directories. Several Files methods, such as move, can perform certain operations atomically in some file systems. An atomic file operation is an operation that cannot be interrupted or &amp;quot;partially&amp;quot; performed.
  • A WatchService was introduced that watches registered objects for changes and events. You may use a watch service to monitor a directory for changes to be able to update UI when files are created or deleted. The main goal here was to help with performance issues in applications that previously were forced to poll the file system.
  • Bit of a background. There are two parts of Java really: Java, the language and Java Virtual Machine, so called JVM. Java code is translate or compiled into bytecode that is executed on JVM. There are lots of other languages that can be compiled to bytecode (like Ruby in form of a JRuby). However due to nature of dynamic languages people needed to use workaround or hacks to generate a bytecode and this resulted in worse performance.
  • In JDK 7, dynamically typed languages should run faster in the JVM than they do today. A key part of support for this is the addition of a new Java bytecode instruction invokedynamic , for method invocation, and an accompanying linkage mechanism that involves a new construct called a method handle.
  • In summary, method handles provide the linkage mechanism that enables the JVM to invoke the correct method in response to an invokedynamic bytecode instruction. When the JVM sees an invokedynamic bytecode, it uses method handles to get to the method it needs. In a way it could be used for things Java Reflection API was used so far. However, compared to using the Reflection API, access checking is performed when the method handle is created as opposed to every time the method is called, thus is faster.
  • Although JSR-292 was intended to help dynamically typed languages it will bring huge benefits in other areas. There is a Oracle employee blogging on these other goodies. One of them is that code coverage tools will get faster. Record a visit in a bootstrap method and use method handle to no-op method. Java Specfication Request (JSR)
  • There are few concurrency updates in Java 7. New is the fork/join framework that helps you take advantage of multiple processors. It is designed for work that can be broken into smaller pieces recursively. This is a style of parallel programming in which problems are solved by splitting them into subtasks that are solved in parallel, waiting for them to complete, and then composing results. Fork/Join framework was intended to be included in the JDK 5, but finally did not make it.
  • As always there are numerous additions to JAVA API. They are mostly related to changes mentioned earlier, but there are also some utility methods added that might be useful, like Objects class. j ava.util.Objects boolean equals(Object a, Object b) boolean deepEquals(Object a, Object b) int hashCode(Object o) int hash(Object... values) String toString(Object o) String toString(Object o, String nullDefault) &lt;T&gt; int compare(T a, T b, Comparator&lt;? super T&gt; c) &lt;T&gt; T requireNonNull(T obj) &lt;T&gt; T requireNonNull(T obj, String message)
  • Java 7 adds a close() method to URLClassLoader. This is a methods that frees the underlying resources, such as open files, held by a URLClassLoader. It is a particular problem on Windows, because open files cannot be deleted or replaced.
  • Leftover from JAVA 7 Lambda expressions are anonymous functions A lot of current API requiring interface with only one method will be much easier to use. The other bit, Project Jigsaw, aims to modularize JDK, split it into smaller parts, provide profiles and download what&apos;s necessary as you go.
  • Java 7

    1. 1. Java 7 Tomasz Wróbel
    2. 2. Schedule <ul><li>Java SE 6 (December 11, 2006) </li></ul><ul><li>2010/12/23 Feature Complete (M11) </li></ul><ul><li>2011/02/17 Developer Preview (M12) </li></ul><ul><li>2011/06/02 Last scheduled build (M13) </li></ul><ul><li>2011/07/28 General Availability </li></ul>
    3. 3. Project Coin <ul><li>Strings in switch </li></ul><ul><li>Binary integral literals </li></ul><ul><li>Underscores in numeric literals </li></ul><ul><li>Improved type inference for generic instance creation (diamond) </li></ul><ul><li>Multi-catch and more precise rethrow </li></ul><ul><li>try-with-resources statement </li></ul><ul><li>Simplified varargs method invocation </li></ul>
    4. 4. Binary literals <ul><li>integral types ( byte , short , int , and long ) can also be expressed using the binary number system </li></ul><ul><li>0b00101000 </li></ul><ul><li>0B01001000 </li></ul>
    5. 5. Underscores in Numeric Literals <ul><li>long creditCardNumber = 1234_5678_9012_3456L </li></ul><ul><li>long phoneNo = 0031_645_142_631L </li></ul><ul><li>long bytes = 0b11010010_01101001_10010100_10010010 </li></ul>
    6. 6. Type Inference for Generic Instance Creation <ul><li>Map<String, Integer> jdk6map = new HashMap<String, Integer>(); </li></ul><ul><li>Map<String, Integer> jdk7map = new HashMap <> (); </li></ul>
    7. 7. Catching Multiple Exception Types <ul><li>try { </li></ul><ul><li>Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance(); </li></ul><ul><li>} catch ( InstantiationException | ClassNotFoundException | IllegalAccessException e ) { </li></ul><ul><li>e.printStackTrace(); </li></ul><ul><li>} </li></ul>
    8. 8. twr - The try-with-resources Statement <ul><li>try (Connection connection = DriverManager.getConnection(url, &quot;root&quot;, &quot;admin&quot;); </li></ul><ul><li>Statement st = connection.createStatement(); </li></ul><ul><li>ResultSet rs = st.executeQuery(&quot;Select * from CITY&quot;);) { </li></ul><ul><li>while (rs.next()) { </li></ul><ul><li>// some code </li></ul><ul><li>} </li></ul><ul><li>} catch (SQLException e) { </li></ul><ul><li>e.printStackTrace(); </li></ul><ul><li>} </li></ul>
    9. 9. NIO.2 <ul><li>java.nio.file package </li></ul><ul><ul><li>Path </li></ul></ul><ul><ul><li>Files </li></ul></ul><ul><ul><li>FileSystem </li></ul></ul><ul><li>NIO.2 filesystem provider for zip/jar archives </li></ul>
    10. 10. java.nio.file.Files
    11. 11. java.nio.file.WatchService <ul><li>ENTRY_CREATE </li></ul><ul><li>ENTRY_DELETE </li></ul><ul><li>ENTRY_MODIFY </li></ul>
    12. 12. JSR 292 - Support for dynamically-typed languages <ul><li>Java </li></ul><ul><li>String s = &quot;Hello World&quot;; </li></ul><ul><li>System.out.println(s); </li></ul><ul><li>bytecode (executed on JVM – Java Virtual Machine) </li></ul><ul><li>ldc #2 </li></ul><ul><li>astore_1 </li></ul><ul><li>getstatic #3 </li></ul><ul><li>aload_1 </li></ul><ul><li>invokevirtual #4 </li></ul>
    13. 13. invokedynamic <ul><li>invokedynamic </li></ul><ul><li>+ </li></ul><ul><li>method handle </li></ul><ul><li>= </li></ul><ul><li>much better performance of </li></ul><ul><li>dynamically typed languages on the JVM </li></ul>
    14. 14. Method handles <ul><li>java.lang.invoke.MethodHandle </li></ul><ul><li>Provide the linkage mechanism that enables </li></ul><ul><li>the JVM to invoke the correct method </li></ul><ul><li>in response to an invokedynamic bytecode </li></ul><ul><li>instruction </li></ul>
    15. 15. Other benefits of JSR 292 <ul><li>code coverage tools </li></ul>
    16. 16. A lightweight fork/join framework <ul><li>if (my portion of the work is small enough) </li></ul><ul><li>do the work directly </li></ul><ul><li>else </li></ul><ul><li>split my work into two pieces </li></ul><ul><li>invoke the two pieces and wait for the results </li></ul>
    17. 17. New Classes, Interfaces and Enums <ul><li>hundreds of changes to API </li></ul><ul><li>java.util.Objects </li></ul><ul><ul><li>boolean equals(Object a, Object b) </li></ul></ul><ul><ul><li>int hash(Object... values) </li></ul></ul><ul><ul><li><T> T requireNonNull(T obj) </li></ul></ul><ul><ul><li>String toString(Object o, String default) </li></ul></ul><ul><li>New static methods for comparing primitives </li></ul><ul><ul><li>e.g. Integer.compare(int,int) </li></ul></ul>
    18. 18. URLClassLoader.close() <ul><li>A method that frees the underlying resources, such as open files, held by a URLClassLoader </li></ul>
    19. 19. Beyond <ul><li>JAVA 8 is expected in late 2012 </li></ul><ul><li>Project Lambda - Lambda expressions aka &quot;closures&quot; </li></ul><ul><ul><li>Collections.sortBy(people, #{ Person p -> p.getLastName() }); </li></ul></ul><ul><li>Modularization (Project Jigsaw) </li></ul>
    20. 20. Resources <ul><li>JDK 7 - http://wikis.sun.com/display/JDK7/Home </li></ul><ul><li>http://openjdk.java.net/projects/nio/ </li></ul><ul><li>File I/O (Featuring NIO.2) http://download.oracle.com/javase/tutorial/essential/io/fileio.html </li></ul><ul><li>Enhancements in Java SE 7 http://download.java.net/jdk7/docs/technotes/guides/language/enhancements.html#javase7 </li></ul><ul><li>Fork/Join http://download.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html </li></ul><ul><li>http://www.java7developer.com/blog/ </li></ul><ul><li>http://www.java.net/blogs/forax/ </li></ul><ul><li>JDK 7: New Interfaces, Classes, Enums, and Methods http://marxsoftware.blogspot.com/2011/03/jdk-7-new-interfaces-classes-enums-and.html </li></ul>

    ×