As we know Oracle released Java 8 in its Annual conference Java One recently . So before saying adieu to java 7 let us revisit the features of it again , that differentiate it from earlier versions .
Given below are the list of wonderful features added as part of Java 7 .
1. Diamond Operator
2. Using strings in switch statement
3. Automatic resource management
4. Numeric literals with underscore
5. Improved Exception Handling
6. New File System API (NIO 2.0)
7. Fork and Join
Read Also : Java 8 features : Biggest change to Java since Java
1. Diamond Operator
We used to have declarations like below until java 1.6
Map<Object,Object> mpObj = new HashMap<Object,Object>();
Here,if you would observe , there is a repetition of
both on the left hand side and right hand side
Hence with Java 1.7 , you don't have to do a repeat on the right hand side
This means you can do something like this on the code
Map<Object,Object> mpObj = new HashMap <>();
Because of the symbol hence created , the name "Diamond Operator"
was coined in by Ronaldinho
2. Using Strings in switch statements
Until Java 1.6 , we have only int values (convertible) , enum's as contestants of switch cases . Starting
Java 1.7 , we have even java.lang.String was nominated to be a contestant for the switch cases .
for example :
String caseContestant= "Apple";
switch(caseContestant){ the location i was talking about is this position . This position only convertible ints
and enums had the keys to enter into the gates , now in java 1.7 ,duplicate key
has been given to even java.lang.String
case "Apple" :
System.out.print("I am apple");
break;
case "Mango" :
System.out.print("I am mango");
}
3. Automatic Resource Management
This means that the management of resources will not be anymore done by developers, but will be
automatically taken care by Java (JVM)
Resource 1 passed as argument Resource 2 passed as argument
try ( FileOutputStream fos=new FileOutputStream("movies.txt") ; DataOutputStream dos= new DataOutputStream(fos); ) This is the way we specify JAVA/JVM , mentioning that we want
this list of resources separated by semi colon to be handled .
{
// try block code
}
Now the question arises How the hell JVM come to know anything about the elements mentioned in the above argument list separated by semi colons.
The answer to the question is that all of the above mentioned elements in the list implement an interface called java.lang.AutoCloseable . This interface declares a method called "void close() throws Exception" . This method is completely used for management of resources .
4. Numeric literals with underscores
This means that we can have underscores in between the numbers for readability purposes .
int thousand = 1_000 ; We can introduce underscores in between the numbers for
readability purposes; still the value remains the same
int tenthousand = 10_000 ;
5. Improved Exception Handling
In Java 7 , This is technically called multi-catch clause
catch(Exception1 | Exception 2 | Excception 3 | Exception 4 exceptionObject)
In the catch block you can pass multiple exceptions separated by
" | " separator . If there any exceptions happening fall in this list ,
it will all be caught by this catch block .
6. New File System API (NIO 2.0)
NIO stands for New Input Output .
There is complete change in the framework when it comes to file handling , input/output operations
and others .
All elements that make up the new framework are all placed under the package java.nio .
To give a brief understanding on this , let's try to understand below picture
As described in the above figure , the architecture/framework always gives you a component that is strongly coupled with the underlying architecture. But these components still share a common interface . We will call it as "Platform based architecture" , actually it is Factory Pattern exemplified at its best .Having this in mind lets analyze the new wonderful things that come as part of java.nio
Path
The first change is the introduction of the Path (java.nio.file.path) component in the place of File (java.io)
.And for Paths , Paths (Java.nio.file.Paths) act as the Component provider . The Component structure of the
Path looks like this
Now the component provider for the same would be Paths from java.nio.file package
Hence the file object instantiation which had been like
File file= new File ("fileName");
Would now look like
Path path= new Paths.get("fileName");
Paths is the Component provider
Note that there is something common between the above two statements , both are just object /virtual entities . No links exists between these objects and the storage devices , until some action like create ,
delete , modification of the file is done .
For example , in the old API , it would be something like the below to create a file.
file.createNewFile();
In the new API it is something like ,
Files.createFile(path);
This being the utility class from the java.nio.file package
Accordingly we have delete,deleteExists,copy,move,createSymbolicLink(for creating shortcut for files) for different actions from the java.nio.file.Files utility class .
File Change Notification:
Consider a scenario :
You as a policeman (the program you write) , call out on a FBI (who is the WatchService in java terms)
to monitor a robber (directory/file) . The FBI informs you back using signals (WatchKeys in this case) .
Steps involved in accomplishing the file notification functionality .
1. Create a WatchService .This service consists of a queue to hold WatchKeys.
2. Register the directory/file you wish to monitor with this WatchService .
3. While registering ,specify the types of events you wish to recieve (create , modify or delete events) .
4. You have to start an infinite loop to listen to events .
5. When an event occurs , a WatchKey is placed into the queue.
6. Consume the WatchKey and invoke queries on it .
Code example :
7. Fork and Join
This is a mechanism through which you make the best use of the processors available in your systems .
Fork a term coined by the UNIX bad boys , saying to create a child process .
Join also a term coined by the UNIX bad boys , saying to wait for a child process to complete and join
Following are the steps you do to make the dangerous move with Fork and Join mechanism of Java
Step 1:
ForkJoinPool pool = new ForkJoinPool(Runtime.getRunTime().availableProcessors())
The first step informs the master (ForkJoinPool) , the number of processors available in the system for it to manage with .
Step 2:
pool.invoke(task);
Here you will give the task you want to be executed to the ForkJoinPool ,and it will take care of delegating this work to the free processor.
Given below are the list of wonderful features added as part of Java 7 .
1. Diamond Operator
2. Using strings in switch statement
3. Automatic resource management
4. Numeric literals with underscore
5. Improved Exception Handling
6. New File System API (NIO 2.0)
7. Fork and Join
Read Also : Java 8 features : Biggest change to Java since Java
1. Diamond Operator
We used to have declarations like below until java 1.6
Map<Object,Object> mpObj = new HashMap<Object,Object>();
Here,if you would observe , there is a repetition of
both on the left hand side and right hand side
Hence with Java 1.7 , you don't have to do a repeat on the right hand side
This means you can do something like this on the code
Map<Object,Object>
Because of the symbol hence created , the name "Diamond Operator"
was coined in by Ronaldinho
2. Using Strings in switch statements
Until Java 1.6 , we have only int values (convertible) , enum's as contestants of switch cases . Starting
Java 1.7 , we have even java.lang.String was nominated to be a contestant for the switch cases .
for example :
String caseContestant= "Apple";
switch(caseContestant){ the location i was talking about is this position . This position only convertible ints
and enums had the keys to enter into the gates , now in java 1.7 ,duplicate key
has been given to even java.lang.String
case "Apple" :
System.out.print("I am apple");
break;
case "Mango" :
System.out.print("I am mango");
break;
}
3. Automatic Resource Management
This means that the management of resources will not be anymore done by developers, but will be
automatically taken care by Java (JVM)
Resource 1 passed as argument Resource 2 passed as argument
try ( FileOutputStream fos=new FileOutputStream("movies.txt") ; DataOutputStream dos= new DataOutputStream(fos); ) This is the way we specify JAVA/JVM , mentioning that we want
this list of resources separated by semi colon to be handled .
{
// try block code
}
Now the question arises How the hell JVM come to know anything about the elements mentioned in the above argument list separated by semi colons.
The answer to the question is that all of the above mentioned elements in the list implement an interface called java.lang.AutoCloseable . This interface declares a method called "void close() throws Exception" . This method is completely used for management of resources .
4. Numeric literals with underscores
This means that we can have underscores in between the numbers for readability purposes .
int thousand = 1_000 ; We can introduce underscores in between the numbers for
readability purposes; still the value remains the same
int tenthousand = 10_000 ;
5. Improved Exception Handling
In Java 7 , This is technically called multi-catch clause
catch(Exception1 | Exception 2 | Excception 3 | Exception 4 exceptionObject)
In the catch block you can pass multiple exceptions separated by
" | " separator . If there any exceptions happening fall in this list ,
it will all be caught by this catch block .
6. New File System API (NIO 2.0)
NIO stands for New Input Output .
There is complete change in the framework when it comes to file handling , input/output operations
and others .
All elements that make up the new framework are all placed under the package java.nio .
To give a brief understanding on this , let's try to understand below picture
As described in the above figure , the architecture/framework always gives you a component that is strongly coupled with the underlying architecture. But these components still share a common interface . We will call it as "Platform based architecture" , actually it is Factory Pattern exemplified at its best .Having this in mind lets analyze the new wonderful things that come as part of java.nio
Path
The first change is the introduction of the Path (java.nio.file.path) component in the place of File (java.io)
.And for Paths , Paths (Java.nio.file.Paths) act as the Component provider . The Component structure of the
Path looks like this
Now the component provider for the same would be Paths from java.nio.file package
Hence the file object instantiation which had been like
File file= new File ("fileName");
Would now look like
Path path= new Paths.get("fileName");
Paths is the Component provider
Note that there is something common between the above two statements , both are just object /virtual entities . No links exists between these objects and the storage devices , until some action like create ,
delete , modification of the file is done .
For example , in the old API , it would be something like the below to create a file.
file.createNewFile();
In the new API it is something like ,
Files.createFile(path);
This being the utility class from the java.nio.file package
Accordingly we have delete,deleteExists,copy,move,createSymbolicLink(for creating shortcut for files) for different actions from the java.nio.file.Files utility class .
File Change Notification:
Consider a scenario :
You as a policeman (the program you write) , call out on a FBI (who is the WatchService in java terms)
to monitor a robber (directory/file) . The FBI informs you back using signals (WatchKeys in this case) .
Steps involved in accomplishing the file notification functionality .
1. Create a WatchService .This service consists of a queue to hold WatchKeys.
2. Register the directory/file you wish to monitor with this WatchService .
3. While registering ,specify the types of events you wish to recieve (create , modify or delete events) .
4. You have to start an infinite loop to listen to events .
5. When an event occurs , a WatchKey is placed into the queue.
6. Consume the WatchKey and invoke queries on it .
Code example :
Path path = Paths.get("C:\\Temp\\temp\\");
WatchService watchService = FileSystems.getDefault().newWatchService();
path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
WatchKey key=null;
while(true)
{
try
{
key=watchService.take();
for(WatchEventkey : key.pollEvents())
{
Kind kind=event.kind();
System.out.println("Event on" + event.context().toString() + "is" +kind);
}
}
catch(InterruptedException e)
{
System.out.println("InterruptedException:" + e.getMessage());
}
key.reset(); To restart the Watching duty
}
WatchService watchService = FileSystems.getDefault().newWatchService();
path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
WatchKey key=null;
while(true)
{
try
{
key=watchService.take();
for(WatchEventkey : key.pollEvents())
{
Kind kind=event.kind();
System.out.println("Event on" + event.context().toString() + "is" +kind);
}
}
catch(InterruptedException e)
{
System.out.println("InterruptedException:" + e.getMessage());
}
key.reset(); To restart the Watching duty
}
7. Fork and Join
This is a mechanism through which you make the best use of the processors available in your systems .
Fork a term coined by the UNIX bad boys , saying to create a child process .
Join also a term coined by the UNIX bad boys , saying to wait for a child process to complete and join
Following are the steps you do to make the dangerous move with Fork and Join mechanism of Java
Step 1:
ForkJoinPool pool = new ForkJoinPool(Runtime.getRunTime().availableProcessors())
The first step informs the master (ForkJoinPool) , the number of processors available in the system for it to manage with .
Step 2:
pool.invoke(task);
Here you will give the task you want to be executed to the ForkJoinPool ,and it will take care of delegating this work to the free processor.