Java 9 Features
Java 9 Features
—------------------------
1. Private Methods in Interfaces
2. try-with-resources Enhancements
3. Diamond Operator Enhancement in JAVA.
4. Factory Methods for Unmodifiable Collections.
5. JSHELL
6. JPMS
7. Enhancements in Stream API
8. Process API Updations
9. JLinker
—----
—----
public interface I{
public default void meth(){
—----
}
}
EX:
public interface Calculator{
public int add(int i, int j);
public default int sub(int i, int j){
—----
}
}
public class CalculatorImpl implements Calculator{
public int add(int i, int j){
—-----
}
}
EX:
interface Transaction{
private void preTransaction(){
[Link]("Open Account Database.....");
[Link]("Begin Transaction.....");
}
private void postTransaction(){
[Link]("Commit / Rollback
Transation.....");
[Link]("End Transaction......");
[Link]("Close Account
Database......");
}
public default void deposit(){
preTransaction();
[Link]("*****Deposit Logic******");
postTransaction();
}
public default void withdraw(){
preTransaction();
[Link]("*****Withdraw Logic******");
postTransaction();
}
public default void transferFunds(){
preTransaction();
[Link]("*****Transafer Funds
Logic******");
postTransaction();
}
}
class TransactionImpl implements Transaction{
}
public class Main {
public static void main(String[] args) {
Transaction transaction = new TransactionImpl();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
try-with-resources Enhancements:
—-------------------------------
In general, in Java applications, we may use resources like Streams,
Sockets, Database Connections,.... When we perform operations with
these resources we may get exceptions, to handle these exceptions if
we use try-catch-finally then we have to use the following
conventions.
Syntax:
try(Resource-1; Resource-2;....Resource-n;){
—----
}catch(Exception e){
—----
}
){
—-----
}catch(Exception e){
[Link]();
}
}
}
Up to JAVA 8 version , it is not possible to declare and create
resources before try block, we must declare and create resources along
with try keyword.
Syntax:
Resource1 r1 = new Resource1();
Resource1 r2 = new Resource1();
—----
—----
Resource1 rn = new Resource1();
try(r1;r2;....rn;){
}catch(Exception e){
[Link]();
}
EX:
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
Connection con = [Link](--);
Socket s = new Socket(---);
try(
br;con;s;
){
—-----
}catch(Exception e){
[Link]();
}
EX:
import [Link].*;
try (bufferedReader;fileOutputStream;fileInputStream) {
[Link]("Enter Data : ");
String data = [Link]();
[Link]([Link]());
byte[] btArray = new
byte[[Link]()];
[Link](btArray);
[Link](new String(btArray));
}catch (Exception e){
[Link]();
}
}
}
In the above Generic Type Parameter declaration, we must use the same
type at both left side Type Parameter and Right side Type Parameter,
in this case JAVA7 version has provided a flexibility to remove Type
Parameter at right side of the expression in <>, here empty <> is
called diamond operator.
Up to JAVA 8 version, we are able to use <> for the Collection classes
in the generic classes declaration and in the Collection classes
objects creation, but it is not possible to use <> operator to the
anonymous inner classes.
EX:
import [Link];
import [Link];
}
}
To overcome the above problems JAVA 1.2 version has provided the
following methods in Collections class to make immutable Collection or
Unmodifiable Collections.
import [Link];
import [Link];
import [Link];
list = [Link](list);
[Link](list);
//[Link]("EEE"); --->
[Link]
[Link](list);
}
}
EX:
import [Link];
import [Link];
import [Link];
EX:
import [Link];
import [Link];
import [Link];
map = [Link](map);
[Link](map);
//[Link](3, "XXX"); --->
[Link]
}
}
In the JAVA 9 version, Java has provided the following factory methods
to make immutable List, Immutable Set and Immutable Map.
EX:
import [Link];
import [Link];
import [Link];
}
}
import [Link];
import [Link];
import [Link];
}
}
JSHELL
—-------
JSHELL is an interactive Mode to check code blocks instantly.
JSHELL is also called REPL tool
R : Read
E : Evaluate
P : Program
L : Loop
Note: JSHELL is mainly for checking the code instantly, it is not for
the development of the applications.
JSHELL
EX:
C:\Users\Administrator>JSHELL
| Welcome to JShell -- Version 17.0.6
| For an introduction type: /help intro
jshell>
/help intro
EX:
jshell> /help intro
|
| intro
| =====
|
| The jshell tool allows you to execute Java code, getting immediate
results.
| You can enter a Java definition (variable, method, class, etc),
like: int x = 8
| or a Java expression, like: x + x
| or a Java statement or import.
| These little chunks of Java code are called 'snippets'.
|
| There are also the jshell tool commands that allow you to
understand and
| control what you are doing, like: /list
|
| For a list of commands: /help
jshell>
/help
EX:
jshell> /help
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [<name or id>|-all|-start]
| list the source you have typed
| /edit <name or id>
| edit a source entry
| /drop <name or id>
| delete a source entry
| /save [-all|-history|-start] <file>
| Save snippet source to a file
| /open <file>
| open a file as source input
| /vars [<name or id>|-all|-start]
| list the declared variables and their values
| /methods [<name or id>|-all|-start]
| list the declared methods and their signatures
| /types [<name or id>|-all|-start]
| list the type declarations
| /imports
| list the imported items
| /exit [<integer-expression-snippet>]
| exit the jshell tool
| /env [-class-path <path>] [-module-path <path>] [-add-modules
<modules>] ...
| view or change the evaluation context
| /reset [-class-path <path>] [-module-path <path>] [-add-modules
<modules>]...
| reset the jshell tool
| /reload [-restore] [-quiet] [-class-path <path>] [-module-path
<path>]...
| reset and replay relevant history -- current or previous (-
restore)
| /history [-all]
| history of what you have typed
| /help [<command>|<subject>]
| get information about using the jshell tool
| /set editor|start|feedback|mode|prompt|truncation|format ...
| set configuration information
| /? [<command>|<subject>]
| get information about using the jshell tool
| /!
| rerun last snippet -- see /help rerun
| /<id>
| rerun snippets by ID or ID range -- see /help rerun
| /-<n>
| rerun n-th previous snippet -- see /help rerun
|
| For more information type '/help' followed by the name of a
| command or a subject.
| For example '/help /list' or '/help intro'.
|
| Subjects:
|
| intro
| an introduction to the jshell tool
| keys
| a description of readline-like input editing
| id
| a description of snippet IDs and how use them
| shortcuts
| a description of keystrokes for snippet and command
completion,
| information access, and automatic code generation
| context
| a description of the evaluation context options for /env
/reload and /reset
| rerun
| a description of ways to re-evaluate previously entered
snippets
jshell>
jshell> $1
$1 ==> "Welcome to JSHELL"
jshell> str
str ==> "Welcome To JSHELL"
jshell> [Link](str);
| Error:
| package SYstem does not exist
| [Link](str);
| ^--------^
jshell> [Link](str);
Welcome To JSHELL
jshell> 10+20
$7 ==> 30
jshell> [Link](a-b);
-10
jshell>
/imports
EX:
jshell> /imports
| import [Link].*
| import [Link].*
| import [Link].*
| import [Link].*
| import [Link].*
| import [Link].*
| import [Link].*
| import [Link].*
| import [Link].*
| import [Link].*
jshell>
EX:
jshell> List<String> list = [Link]("AAA", "BBB","CCC","DDD");
list ==> [AAA, BBB, CCC, DDD]
jshell> list
list ==> [AAA, BBB, CCC, DDD]
jshell> list
list ==> [AAA, BBB, CCC, DDD]
jshell> list1
list1 ==> [aaa, bbb, ccc, ddd]
jshell>
EX:
jshell> import [Link].*;
jshell> [Link](123456789.4567890);
$24 ==> "123.456.789,457"
jshell>
IN JSHELL, if we want to list out all the code snippets which we have
provided up to now we have to use the following command on JSHELL.
/list
EX:
jshell> /list
1 : "Welcome to JSHELL";
2 : $1
3 : String str = "Welcome To JSHELL";
4 : str
5 : [Link]("Welcome To Durgasoft");
6 : [Link](str);
7 : 10+20
8 : int a = 10;
9 : int b = 20;
10 : a*b
11 : [Link](a-b);
13 : List<String> list = [Link]("AAA", "BBB","CCC","DDD");
14 : list
15 : List<String> list1 = [Link]().map(str-
>[Link]()).collect([Link]());
16 : list
17 : list1
19 : drop d;
20 : import [Link].*;
21 : Connection con =
[Link]("jdbc:odbc:nag","system","durga");
22 : import [Link].*;
23 : NumberFormat nf = [Link](new
Locale("it","IT"));
24 : [Link](123456789.4567890);
25 : Locale l = new Locale("it","IT");
26 : DateFormat df = [Link](0,l);
27 : [Link](new [Link]());
In JSHELL, we are able to get the complete history of the JSHELL by
using the following command.
/history
EX:
jshell> /history
/help intro
/help
"Welcome to JSHELL";
$1
String str = "Welcome To JSHELL";
str
[Link]("Welcome To Durgasoft");
[Link](str);
[Link](str);
10+20
int a = 10;
int b = 20;
a*b
[Link](a-b);
Date d = new Date();
/imports
List<String> list = [Link]("AAA", "BBB","CCC","DDD");
list
List<String> list1 = [Link]().map(str-
>[Link]()).collect([Link]());
List<String> list1 = [Link]().map(str-
>[Link]()).collect([Link]());
list
list1
/help
import [Link].*;
drop d;
import [Link].*;
Connection con =
[Link]("jdbc:odbc:nag","system","durga");
import [Link].*;
NumberFormat nf = [Link](new Locale("it","IT"));
[Link](123456789.4567890);
Locale l = new Locale("it","IT");
DateFormat df = [Link](0,l);
[Link](new Date());
[Link](new [Link]());
/list
/history
jshell>
Variables in JSHELL:
In JSHELL , there are two types of variables.
1. Implicit variables or Scratch variables
2. Explicit Variables
$num
EX:
jshell> "abc"
$1 ==> "abc"
jshell> 10+20
$2 ==> 30
jshell> "abc"+"xyz"
$3 ==> "abcxyz"
jshell> $1
$1 ==> "abc"
jshell> $2
$2 ==> 30
jshell> $3
$3 ==> "abcxyz"
Explicit Variables:These variables must be declared by the developers
explicitly.
EX:
jshell> String firstName = "Durga";
firstName ==> "Durga"
jshell> [Link](firstName+","+lastName+","+age+","+qual);
Durga,N,22,BTech
jshell> firstName
firstName ==> "Durga"
jshell> lastName
lastName ==> "N"
jshell> age
age ==> 22
jshell> qual
qual ==> "BTech"
jshell>
/vars
jshell> /vars
| String $1 = "abc"
| int $2 = 30
| String $3 = "abcxyz"
| String firstName = "Durga"
| String lastName = "N"
| int age = 22
| String qual = "BTech"
jshell>
/vars varName
jshell>
drop varName
jshell> /vars
| String $1 = "abc"
| int $2 = 30
| String $3 = "abcxyz"
| String lastName = "N"
| int age = 22
| String qual = "BTech"
| drop firstName = (not-active)
jshell>
Methods in JSHELL:
It is a set of instructions as a single unit representing a particular
action.
We can access the methods directly by using method names like local
methods.
EX:
jshell> void sayHello(){
...> [Link]("Hello User!");
...> }
| created method sayHello()
jshell> sayHello();
Hello User!
jshell> welcome("Durga");
Hello Durga
Welcome To JSHELL
jshell> add(20,5);
$22 ==> 25
jshell> sub(10,5);
$24 ==> 5
If we want to get all the declared methods from JSHELL then we have to
use the following command.
/methods
jshell> /methods
| void sayHello()
| void welcome(String)
| int add(int,int)
| int sub(int,int)
jshell>
/methods methodName
/drop methodName
jshell> /methods
| void welcome(String)
| int add(int,int)
| int sub(int,int)
IN JSHELL, we are able to prepare methods with the same name and with
the different parameters list.
jshell> mul(10,20);
$28 ==> 200
jshell> mul(10,20,30);
$29 ==> 6000
EX:
...> }
...>
jshell> [Link]();
Employee Number : 111
Employee Name : AAA
jshell> [Link]();
Employee Number : 111
Employee Name : AAA
jshell> [Link]();
Employee Number : 111
Employee Name : AAA
jshell>
/edit
If we use the above command then JSHELL Editor will open with the list
of data that we provided , where we can modify, delete,... finally we
have to click on the “Accept” button to reflect all modifications to
JSHELL.
EX:
jshell> /set editor "C:/Program Files/EditPlus/[Link]"
| Editor set to: C:/Program Files/EditPlus/[Link]
jshell> /edit
EX:
jshell> /edit
| created class ToyotoCar
jshell> [Link]();
Innova Crysta.......
jshell> [Link]([Link]);
AVAILABLE
jshell> [Link]([Link]);
BUSY
jshell> [Link]([Link]);
IDLE
/save fileNameAndLocation
/open fileNameLocation
jshell> getStudentDetails();
STudent Details.....
EX:
E:/abc/xyz/[Link]
import [Link].*;
class EmployeeDao{
public void getEmployeeDetails(){
Connection con = null;
try{
[Link]("[Link]");
con =
[Link]("jdbc:mysql://localhost:3306/durgadb","roo
t","root");
Statement st = [Link]();
ResultSet rs = [Link]("select * from emp1");
[Link]("ENO\tENAME\tESAL\tEADDR");
[Link]("-----------------------------");
while([Link]()){
[Link]([Link]("ENO")+"\t");
[Link]([Link]("ENAME")+"\t");
[Link]([Link]("ESAL")+"\t");
[Link]([Link]("EADDR")+"\n");
}
}catch(Exception e){
[Link]();
}finally{
try{
[Link]();
}catch(Exception e){
[Link]();
}
}
}
}
EX:
[Link]
String wishMessage = "Good Evening Nagoor";
[Link](wishMessage);
[Link]
package p1;
import p2.*;
public class Employee{
public void getEmpDetails(){
[Link]("Employee Details......");
Account account = new Account();
[Link]();
}
}
[Link]
package p2;
import p3.*;
public class Account{
public void getAccountDetails(){
[Link]("Account Details.....");
Bank bank = new Bank();
[Link]();
}
}
[Link]
package p3;
public class Bank{
public void getBankDetails(){
[Link]("Bank Details.....");
}
}
D:\java6\jpms\app01>javac -d . *.java
D:\java6\jpms\app01>jar -cvf [Link] p1
D:\java6\jpms\app01>jar -cvf [Link] p2
D:\java6\jpms\app01>jar -cvf [Link] p3
[Link]
import p1.*;
class Test{
public static void main(String[] args){
Employee employee = new Employee();
[Link]();
}
}
D:\java6\jpms\app01>javac [Link]
[Link]: error: package p1 does not exist
import p1.*;
^
[Link]: error: cannot find symbol
Employee employee = new Employee();
^
symbol: class Employee
location: class Test
[Link]: error: cannot find symbol
Employee employee = new Employee();
^
symbol: class Employee
location: class Test
3 errors
D:\java6\jpms\app01>set classpath=[Link];
D:\java6\jpms\app01>javac [Link]
D:\java6\jpms\app01>java Test
Employee Details......
Exception in thread "main" [Link]: p2/Account
D:\java6\jpms\app01>set classpath=[Link];[Link];
D:\java6\jpms\app01>javac [Link]
D:\java6\jpms\app01>java Test
Employee Details......
Account Details.....
Exception in thread "main" [Link]: p3/Bank
D:\java6\jpms\app01>set classpath=[Link];[Link];[Link];
D:\java6\jpms\app01>javac [Link]
D:\java6\jpms\app01>java Test
Employee Details......
Account Details.....
Bank Details.....
EX:
[Link]
package p1;
import p2.*;
public class Employee{
public void getEmpDetails(){
[Link]("Employee Details......");
Account account = new Account();
[Link]();
}
}
[Link]
package p2;
import p3.*;
public class Account{
public void getAccountDetails(){
[Link]("Account Details.....");
Bank bank = new Bank();
[Link]();
}
}
[Link]
package p3;
public class Bank{
public void getBankDetails(){
[Link]("Bank Details.....");
}
}
[Link]
import p1.*;
class Test{
public static void main(String[] args){
Employee employee = new Employee();
[Link]();
}
}
D:\java6\jpms\app01>javac -d . [Link]
D:\java6\jpms\app01>set path=C:\java\jdk1.7.0_80\bin;
D:\java6\jpms\app01>javac -d . [Link]
D:\java6\jpms\app01>set path=C:\java\jdk-17\bin;
D:\java6\jpms\app01>javac -d . [Link]
D:\java6\jpms\app01>set path=C:\java\jdk1.8.0_202\bin;
D:\java6\jpms\app01>set classpath=[Link];[Link];[Link];
D:\java6\jpms\app01>javac [Link]
[Link]: error: cannot access Employee
Employee employee = new Employee();
^
bad class file: [Link](p1/[Link])
class file has wrong version 61.0, should be 52.0
Please remove or make sure it appears in the correct subdirectory
of the classpath.
1 error
D:\java6\jpms\app01>
Security Problems:
In Java applications, we are able to use multiple jar files, we are
able to set them in the “classpath” environment variable, in this
context, if we set any JAR file in the classpath environment variable
then we are able to use all packages which are available in jar file,
there is no chance to hide some of the package, here JAR files are not
providing Security to the packages.
EX:
If we compile the module by using the above command then the Compiler
will create the output folder like below.
D:\java6\app01
Out
|------moduleA
|------pack1
| |------[Link]
|------[Link]
1. exports
2. requires
[Link]
module moduleB{
requires moduleA;
}
EX:
D:\java6\jpms
app02
|-----src
| —------moduleEmp
| |-------com
| | |---durgasoft
| | | |-------emp
| | | | |------[Link]
| |-------[Link]
|
|--------moduleTest
|--------com
| |-----durgasoft
| | |------test
| | | |-----[Link]
|---------[Link]
appo2/src/moduleEmp/com/durgasoft/emp/[Link]
package [Link];
public class Employee{
app02/src/moduleEmp/[Link]
module moduleEmp{
exports [Link];
}
app02/src/moduleTest/com/durgasoft/test/[Link]
package [Link];
import [Link].*;
public class Test{
public static void main(String[] args){
Employee emp = new Employee(111, "Durga", 5000, "Hyd");
[Link]();
}
}
[Link]
module moduleTest{
requires moduleEmp;
}
D:\java6\jpms\app02>
EX:
moduleEmp Elements:
[Link]
package [Link];
public class Employee{
[Link]
module moduleEmp{
exports [Link];
}
moduleStd Elements:
[Link]
package [Link];
public class Student{
[Link]
module moduleStd{
exports [Link];
}
moduleTest elements:
[Link]
package [Link];
import [Link].*;
import [Link].*;
public class Test{
public static void main(String[] args){
Student std = new Student("S-111", "Durga", "Hyd");
[Link]();
[Link]();
}
}
[Link]
module moduleTest{
requires moduleEmp;
requires moduleStd;
}
Employee Details
-------------------------
Employee Number : 111
Employee Name : Durga
Employee Salary : 50000.0
Employee Address : Hyd
D:\java6\jpms\app03>
Note: In JPMS, module does not exports its packages and if we use that
packages in other modules through requires then the compiler will not
raise any error, but JVM will raise an exception like “
[Link]”.