There
are a number of features in java 7 that will please developers.
1. Automatic Resource Management: -
In java 1.6 resources
such as
be closed manually by the developer by writing bog-standard code. Usually we use a
Connections
, Files
, Input/OutStreams
, etc. shouldbe closed manually by the developer by writing bog-standard code. Usually we use a
try-finally
block to close the respective resources. See the current practice
of creating a resource, using it and finally closing it:try{ FileOutputStream fos = new FileOutputStream(“movies.txt”); DataOutputStream dos = new DataOutputStream(fos); dos.writeUTF(“java 7 feature”); }catch(IOException e){ e.printStackTrace(); }finally{ fos.close(); }
However, Java
7 has introduced another cool feature to manage the resources automatically. It
is simple in operation, too. All we have to do is declare the resources in the
try
as follows:
Syntax:
try(resources to be cleaned){ }
Example:
try(FileOutputStream fos=new FileOutputStream(“movies.txt”);DataOutputStream dos new DataOutputStream(fos)){ dos.writeUTF(“java 7 feature”); }catch(IOException e){ e.printStackTrace(); }The above code also represents another aspect of this feature: working with multiple resources. The
FileOutputStream
and DataOutputStream
resources are encl ach one separated by a semicolon (;) separator. We do not have to nullify or
close the streams manually, as they are closed automatically once the control
exists the try block.Behind the
scenes, the resources that should be auto closed must implement java.lang.AutoCloseable i
nterface. Any resource
that implements AutoCloseble
interface can be a candidate for automatic resource management.
The AutoCloseable
is the parent of java.io.Closeable
interface and has just one method close () that would be called by the JVM when the control comes out of the try block.2. multi-catch functionality: -
There
are a couple of improvements in the exception handling area. Java 7 introduced
multi-catch functionality to catch multiple exception types using a single
catch block.
Let's
say you have a method that throws three exceptions. In the current state, you
would deal them individually as shown in below:
try{ code to generate 3 exceptions; }catch(Type1Exception e){ }catch(Type2Exception e){ }catch(Type3Exception e){ }
Catching an endless number of exceptions one after the other in
a catch block looks cluttered. And I have seen code that catches a dozen
exceptions, too. This is incredibly inefficient and error prone. Java 7 has
brought in a new language change to address this ugly duckling. See the
improved version of the code below:
try{ code to generate 3 exceptions; }catch(Type1Exception |Type2Exception e | Type3Exception e){ }
The
multiple exceptions are caught in one catch block by using a '|' operator. This
way, you do not have to write dozens of exception catches. However, if you have
bunch of exceptions that belong to different types, then you could use "multi multi-catch"
blocks too. The following snippet illustrates this:
try{ code to generate 3 exceptions; }catch(Type1Exception e){ }catch(Type2Exception | Type3Exception e){ }
3. Diamond operator: -
In java 1.6
if we have to declare a map of Customers using Generics, we write the code as follows:
Map<String, List<Customer>> custMap = new HashMap<String, List<Customer>> ();
The not-so-nice
thing about this declaration is that we must declare the types on both the
sides, although the right-hand side seems a bit redundant. Can the compiler
infer the types by looking at the left-hand-side declaration? Not unless you're
using Java 7. In 7, it's written like this:
Map<String, List<Customer>> custMap = new HashMap<> ();
How cool is
that? You don't have to type the whole list of types for the instantiation.
Instead you use the <> symbol, which is called
diamond operator.
4. Using Strings in switch statement: -
Till java 1.6, switch
statement works with only primitive types or enumerated types.But in java 1.7 we can use String types also with switch statement.
String availability = "available"; switch(availability) { case "available": //code break; case "unavailable": //code break; case "merged": //code default: //code break; }
5. Numeric literals with underscores: -
Numerical literals are definitely eye strainers. It's quite error
prone and cumbersome to identify a literal if it's a million or a billion
unless we count the places from right to left. Not anymore. Java 7 introduced
underscores in identifying the places.
Example: we can
declare 1000,1000000(one million) as shown below:
int thousand = 1_000; int million = 1_000_000;
6. Binary literals: -
In the earlier versions of java we have to represent the numbers
only in the decimal, octal, or hexadecimal.
But now in java 7 we can represent the numbers in binary also. To
represent the numbers in the binary we need to add the prefix ‘0b’ for binary
number.
Example:
Int a=0b1000; System.out.println (a); O/P: 8 (In earlier versions code doesn’t compile)
No comments:
Post a Comment