{"id":370,"date":"2011-08-29T23:53:23","date_gmt":"2011-08-29T22:53:23","guid":{"rendered":"http:\/\/blogs.kent.ac.uk\/mik\/?p=370"},"modified":"2020-05-15T15:45:56","modified_gmt":"2020-05-15T15:45:56","slug":"java-7-for-intro-programming","status":"publish","type":"post","link":"https:\/\/blogs.kcl.ac.uk\/proged\/2011\/08\/29\/java-7-for-intro-programming\/","title":{"rendered":"Java 7 for introductory programming \u2014 does it make a difference?"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2011\/08\/java71.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-372\" style=\"margin-left: 8px;margin-right: 8px\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2011\/08\/java71.png\" alt=\"\" width=\"240\" height=\"189\" \/><\/a>The recently released <a href=\"http:\/\/www.bluej.org\/\">BlueJ 3.0.5<\/a> brought full support for Java 7. While it is always good to be up-to-date with the latest versions \u2014 what does that actually mean for introductory programming teaching?<\/p>\n<p>Java 7 brought a considerable list of new features to the Java system. Most of them deal with fairly advanced or specialised topics, and will have no influence on introductory teaching with Java. Some, however, will become visible even close to the beginning of programming. Here, I give a short overview of what\u2019s new, and what you \u2014 as a teacher of introductory programming \u2014 should be aware of.<\/p>\n<p><strong>Java 7 &#8211; What\u2019s new<\/strong><\/p>\n<p>Most of the new features in Java 7 are under the hood or in specialised libraries. These are very unlikely to affect an introductory programming course.<\/p>\n<p>Improvements to the internals of the JDK include changes to the VM to support dynamically typed languages and improvements to the class loader architecture. On the library side, there are new frameworks for concurrency, cryptography and new versions of JDBC and Unicode. All these will not be relevant for most introductory courses.<\/p>\n<p>The two areas that are relevant are \u201cProject Coin\u201d, the code name given to a project defining several small language enhancements, and NIO.2, a new library for I\/O. We\u2019ll discuss these in more detail in a moment.<\/p>\n<p>Equally important, what\u2019s\u00a0<em>not<\/em>\u00a0included in this release includes support for closures (known as \u201c<a href=\"http:\/\/openjdk.java.net\/projects\/lambda\/\">Project Lambda<\/a>\u201d). This would have been the most significant change to Java since the introduction of generics in Java 5 or, arguably, since the original definition of the Java language in 1995. Project Lambda has, however, been deferred to JDK 8 in what\u2019s known as\u00a0<a href=\"http:\/\/mreinhold.org\/blog\/plan-b\">Plan B<\/a>. JDK 8 is currently scheduled for release in late 2012.<\/p>\n<p>So, which of the new Java 7 features are actually relevant for our intro programming course?<\/p>\n<p><strong>NIO.2<\/strong><\/p>\n<p>The I\/O classes in Java have been improved several times over the years. The release of JDK 1.4 in 2002 brought some significant improvements in the somewhat short-sightedly named\u00a0<em>NIO<\/em>\u00a0library. The naming is rather less than ideal, since it stands for \u201cNew I\/O\u201d, and the passing of time dictates that everything new will eventually get old.<\/p>\n<p>Which brings us to Java 7 and the New New I\/O. So what will this be called now \u2014 NNIO? Well, the designers settled on NIO.2.<\/p>\n<p>NIO.2 brings a number of new classes and interface, some with very useful methods. If you are doing any programming that accesses the file system, it\u2019s worth\u00a0<a href=\"http:\/\/download.oracle.com\/javase\/7\/docs\/api\/\">familiarising yourself with these<\/a>. Particularly interesting are the\u00a0<em>Files<\/em>\u00a0and\u00a0<em>Paths<\/em>\u00a0classes, and the\u00a0<em>Path<\/em>\u00a0interface, which you can find in the\u00a0<em>java.nio.file<\/em>\u00a0package.<\/p>\n<p>All other relevant new features are part of Project Coin. They are <em>diamond notation<\/em>, <em>strings-in-switch<\/em> and <em>improved exception handling<\/em>.<\/p>\n<p><strong>Project Coin<\/strong><\/p>\n<p><strong><em>Diamond notation<\/em><\/strong><\/p>\n<p>Generic type details on the right hand side of an assignment can now be inferred. For example, where in Java 6 we had to write<\/p>\n<pre>   HashSet&lt;String, Monster&gt; monsters = new HashSet&lt;String, Monster&gt;();<\/pre>\n<p>to declare a HashSet variable and initialise it with a fresh HashSet object, in Java 7 we can write<\/p>\n<pre>   HashSet&lt;String, Monster&gt; monsters = new HashSet&lt;&gt;();<\/pre>\n<p>In other words: We do not have to repeat the generic types of the HashSet on the right hand side, and can instead just write &lt;&gt;. (This syntax is the reason that this construct is known as the \u201cdiamond notation\u201d). The effect of this is exactly as the Java 6 version above. It is a purely syntactic shortcut. The compiler will fill in the generic types by copying them from the variable declaration on the left, and all behaves just as before. Of course, the old notation is still allowed.<\/p>\n<p><strong><em>Strings in switch statements<\/em><\/strong><\/p>\n<p>The variable used in switch statements can now be of type String. For example<\/p>\n<pre>   switch (command) {\n       case \"go\":\n          goForward();\n          break;\n       case \"help\":\n          showHelp();\n          break;\n       default:\n          unknownCommand();\n          break;\n   }<\/pre>\n<p>Before Java 7, strings could not be used in switch statements.<\/p>\n<p><strong><em>Improved exception handling 1: multi-catch<\/em><\/strong><\/p>\n<p>It is now possible to catch multiple exceptions in a single exception handler. For example:<\/p>\n<pre>   try {\n      file = new File(\"readme.txt\");\n      process(file);\n   }\n   catch (FileNotFoundException | UnsupportedEncodingException ex) {\n      ...\n   }<\/pre>\n<p>As you can see in this example, the\u00a0<em>catch<\/em>\u00a0clause can list multiple types of exception in its header, separated with an\u00a0<em>OR<\/em>\u00a0symbol, to catch any of these types of exception.<\/p>\n<p><strong><em>Improved exception handling 2: try-with-resources<\/em><\/strong><\/p>\n<p>The second new feature relating to exceptions is called\u00a0<em>try-with-resources<\/em>. It solves a hard problem: It was previously surprisingly hard to correctly guarantee that resources (such as files or network connections) were correctly closed in the case of an exception.<\/p>\n<p>The new construct solves this. Look at this example:<\/p>\n<pre>   try (FileWriter writer = new FileWriter(filename)) {\n      ...\n   }\n   catch (IOException e) {\n      ...\n   }<\/pre>\n<p>Before Java 7, the standard way to close a resource (a FileWriter in this example) would have been in a\u00a0<em>finally<\/em>\u00a0block, which followed the catch block. In Java 7, the FileWriter is opened in round brackets following the\u00a0<em>try<\/em>\u00a0keyword, marking it as a resource to be auto-closed. Once the try\/catch block is completed, the resource will automatically be closed by the Java runtime system.<\/p>\n<p>For classes to be used with the auto-close mechanism, they must implement the new\u00a0<em>AutoCloseable<\/em>\u00a0interface. All the relevant classes in the Java library have been retrofitted to implement this interface.<\/p>\n<p>That already sums up the relevant Java 7 changes. As we can see \u2014 not much to worry about here. (This will be different next year, when JDK 8 will bring us closures. I expect we will have to have a lively discussion then about how to treat these in a first-year course. But we can leave that for a while.)<\/p>\n<p>To find out more about the new constructs in Java 7, see\u00a0<a href=\"http:\/\/openjdk.java.net\/projects\/jdk7\/features\/\">this summary<\/a>. The next version of the Object First book (<a href=\"http:\/\/www.pearsonhighered.com\/educator\/product\/Objects-First-with-Java-A-Practical-Introduction-Using-BlueJ\/9780132492669.page\">5th edition, to be released in late October<\/a>) includes discussion of these new features.<\/p>\n<p><em><br \/>\n<span style=\"color: #888888\">Note: This post was first published in the <a href=\"http:\/\/blueroom.bluej.org\/\"><span style=\"color: #888888\">Blueroom<\/span><\/a>.<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The recently released BlueJ 3.0.5 brought full support for Java 7. While it is always good to be up-to-date with the latest versions \u2014 what does that actually mean for introductory programming teaching? Java 7 brought a considerable list of &hellip; <a href=\"https:\/\/blogs.kcl.ac.uk\/proged\/2011\/08\/29\/java-7-for-intro-programming\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":179,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,8,13],"tags":[],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-bluej-software-tools","category-programming","category-teaching"],"_links":{"self":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/users\/179"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/comments?post=370"}],"version-history":[{"count":1,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":1012,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/370\/revisions\/1012"}],"wp:attachment":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}