Upload
prathikshajain
View
1.039
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Java 7 :JSR 292: Supporting Dynamically Typed Languages on the Java Platform (Invoke Dynamic) by John Rose;JSR 334: Small Enhancements to the Java Programming Language (Project Coin) by Joe Darcy; JSR 203: More New I/O APIs for the Java Platform (NIO.2) byAlan Bateman; and, last but not least,JSR 336: Java SE 7, the Umbrella JSR for the Platform itself.
Citation preview
Java 7 Features and Enhancements
By :Prathiksha [email protected]/
Abstract : Java version history.
A slate of small language changes(under Coin).
Changes to Jvm (Da Vinci Machine).
Versions: JDK1.0 JDK 1.1 Codename Oak J2SE 1.2 (December 8, 1998) Codename Playground J2SE 1.3 (May 8, 2000) Codename Kestrel J2SE 1.4 (February 6, 2002) Codename Merlin
Continued…. J2SE 5.0 (September 30, 2004) Codename Tiger. Java SE 6 (December 11, 2006) codename Mustang.
------------------------------------------------------------
Since J2SE 1.4, evolution of the Java language governed by the Java Community Process (JCP),
Java Specification Requests (JSRs) to propose and specify additions and changes to the Java platform.
The language is specified by the Java Language Specification (JLS); changes to the JLS are managed under JSR 901.
Java 7 :
Java 7 :
JSR 292: Sup port ing Dy nam i cally Typed Lan guages on the Java Plat form (Invoke Dynamic) by John Rose;
JSR 334: Small En hance ments to the Java Pro gram ming Lan guage (Pro ject Coin) by Joe Darcy;
JSR 203: More New I/O APIs for the Java Plat form (NIO.2) byAlan Bate man;
and, last but not least,
JSR 336: Java SE 7, the Um brella JSR for the Plat form it self.
Virtual machine :
Questions Support for Dynamically-Typed Languages
What is Type System and why is it required ?
Static Typed Dynamic Typed
Dynamically Typed Language and JVM
What is JVM ? How JVM makes a function call ? What are the issues in function call made in Dynamically
Typed Language and Translated in byte code ?
Answers – Type System Type System
The system of associating type with a sequence of bits that informs the program as well as programmer, How that sequence of bit should be understood and interpreted.
Example private int x;public String
employeeName;private Double salary;
Static and Dynamic Java is Static Typed Language, However JavaScript is Dynamic Typed
Language, Why ?
In Java, While Declaring a variable we must declare its type.
In JavaScript we just say that a variable is “var “ and do not specify its exact type.
Static OR Dynamic ? Which Type System is better ?
Static Typed Languages Dynamic Typed Languages
Type Checking while compiling the code.
Type Checking during Run Time
Compiled Code that executes Quickly
Allows compiler to run quickly
Knowledge of Data Type Helps Compiler to Optimize
the Machine Code
Reduces the EDIT-COMPILE-TEST-DEBUG
Time.
-------- Helps in Duck Typing and Code Reuse
Problem in Translating Dynamically Typed Language in .class format
Problem was with function call.
How is a function called by JVM ?
Since its inception, the Java Virtual Machine Specification has specified four bytecodes for method invocation:
1. invokevirtual - Invokes a method on a class. This is the typical type of method invocation.
2. invokeinterface - Invokes a method on an interface.
3. invokestatic - Invokes a static method on a class. This is the only kind of invocation that lacks a receiver argument.
4. invokespecial - Invokes a method without reference to the type of the receiver. Methods called this way can be constructors, superclass methods, or private methods.
JVM
JVM
Answers - JVM
What is JVM ? JVM is the component of the technology
responsible for its hardware – operating system independence.
The JVM knows nothing about Java Programming Language.
How invokevirtual works ?
Input params
Return
type
Other requir
ed detailsInvokevirtual #4
Calling a Method – An Example Consider the following Java code snippet:
String s = "Hello World"; System.out.println(s);
Equivalent Byte Code :
ldc #2 // Push the String "Hello World" onto the stack
astore_1 // Pop the value "Hello World" from the stack and store it in local variable #1
getstatic #3 // Get the static field java/lang/System.out:Ljava/io/PrintStream
from the class
aload_1 // Load the String referenced from local variable #1
invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
Example – Continued… The invokevirtual bytecode instruction is different than the other bytecode
instructions in the example.
It invokes a method rather than manipulating the operand stack. The comment for the invokevirtual bytecode instruction might look odd too. It identifies the following:
The receiver class providing the method: java.io.PrintStream The method name: println The type of the method argument: (Ljava/lang/String;) for String The method's return type: V for void
This information provides a signature for the method invocation. In response to the invokevirtual bytecode instruction, the JVM looks for a method with the supplied signature, in this case, println:(Ljava/lang/String;)V in the java.io.PrintStream class. If the method isn't in that class, the JVM searches up the chain of the class's superclasses.
Why We Cannot Translate Method Call of Dynamic Typed Language to Static Typed Language ?
Dynamic Typed Language do not specify the receiver of the method.
JVM needs to link to and invoke a real method on a real type
During Method Call we must satisfy specifications of either of the four method invocation bytecodes. Eg. Invokevirtual.
ExplanationStatically Typed
How ?
Dynamically Typed
Solution in Java - 7 The invokedynamic Instruction
The syntax of the new invokedynamic bytecode instruction is :
invokedynamic <method-specification> <n>
The invokedynamic bytecode instruction enables an implementer of a dynamic language to translate a method invocation into bytecode without having to specify a target type that contains the method. The method specification in this case is a simplified constant pool reference, which specifies only a method name and type descriptor. The descriptor specifies the return type of the call and the type of method arguments.
Enhancing JVMStatically Typed + invokedynamic
Intermediate Layer
Dynamically Typed
Function Call
invokedynamic The invokedynamic bytecode instruction
enables an implementer of a dynamic language to translate a method invocation into bytecode without having to specify a target type that contains the method. The method specification in this case is a simplified constant pool reference, which specifies only a method name and type descriptor. The descriptor specifies the return type of the call and the type of method arguments.
How does the JVM find the method if the receiver type isn't
supplied? A New Dynamic Linkage Mechanism:
Method Handles
The new linkage mechanism for dynamically typed languages involves a new structure called method handles. JDK 7 includes a new package, java.dyn, that contains the classes associated with dynamic language support in the Java platform. One of the classes in the package is MethodHandle.
MethodHandle A method handle is a simple object of
type java.dyn.MethodHandle that contains an anonymous reference to a JVM method. A method handle is callable just like a named reference to a method.
It is accessed through a pointer structure, as opposed to a linked name.
Bootsrap Method The bootstrap mechanism provides a way for a
language runtime to participate in method dispatching at runtime, but the approach also enables the JVM to bypass the language runtime if the method dispatch decision doesn't change. The first time the JVM sees an invokedynamic bytecode with a receiver and argument, it calls a bootstrap method. Calling into a language-supplied method like this is termed an up-call.
Bootstrap is a customized method, so decisions pertaining to the receiver of the call can be made here.
Earlier System of Method Call
invoke_____Your Method
in X language
Method Details
The New System of invokedynamic
Invokedynamic
MHYour Method
in X language
Questions ?
Java 7
Language Changes Under Project Coin.
Features and Enhancements Virtual Machine
Language Project COIN/JSR 334:
Binary integral literals and underscores in numeric literals.
Strings in switch. Improved type inference for generic instance creation. Multi-catch and more precise rethrow. try-with-resources statement. Modularization.
Project Coin Language Changes :
Binary integral literals and underscores in numeric literals
int thirdBitSet = 0b00000100; System.out.println(thirdBitSet); int bigNumber = 1_000_000_000; System.out.println(bigNumber);
Strings In Switch :
Project Coin Language Changes :
String data = "Howdy, all"; switch (data) { case "Howdy, all": System.out.println("It works!"); default: System.out.println("No love"); }
Multi-Catch
try { Class c = Example.class; Field f = c.getDeclaredField("field"); f.set(null, 12); } catch (NoSuchFieldException | IllegalAccessException x) { x.printStackTrace(); }
Current catch :
Enhanced :
Multi-catch catch (ServletException | MessagingException |
IOException ex) {
Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
}
Automatic Resource Management
Some resources in Java need to be closed manually like InputStream, Writers, Sockets, Sql classes. This language feature allows the try statement itself to declare one of more resources. These resources are scoped to the try block and are closed automatically.
try-with-resources
try (FileReader fr = new FileReader("App.java"); BufferedReader br = new BufferedReader(fr)) { String line = null; while ((line = br.readLine()) != null) System.out.println(">> " + line); } catch (IOException ioEx) { ioEx.printStackTrace(); }
Example BufferedReader br = new BufferedReader(new FileReader(path));
try { return br.readLine();} finally { br.close(); }
BufferedReader br = new BufferedReader(new FileReader(path)); try {
return br.readLine();}
finally { // Removebr.close();
}
Improved Type Inference for Generic Instance Creation (diamond)
Before:
List<Book>bList=new ArrayList<Book>;
Current :
List<Book> bList=new ArrayList();
Underscores in numeric literals
Long numbers are hard to read. You can now split them up using an underscore in ints and longs:
int one_million = 1_000_000;
Strings in switch Currently you can only use numbers or
enums in switch statements. String has been added as a candidate.
String s = ... switch(s) { case "quux": processQuux(s); break;case "bar": processFooOrBar(s);
break; case "baz": processBaz(s); break;
processDefault(s); break; }
Earlier :
Current :
Binary literals
Java code, due to its C heritage, has traditionally forced programmers to represent numbers in only decimal, octal, or hexadecimal.
As quite a few domains are bit orientated, this restriction can introduce errors. You can now create binary numbers using an 0b prefix.
int binary = 0b1001_1001;
Modularity/API(Directory):
References : /Explore… http://java.sun.com/developer/technical
Articles/DynTypeLang/ http://en.wikipedia.org/wiki/Java_version
_history http://www.oracle.com/us/corporate/eve
nts/java7/index.html http://java.sun.com/developer/media/de
epdivejdk7.jsp http://en.wikipedia.org/wiki/Deprecation http://en.wikipedia.org/wiki/Java_virtual_
machine
Questions ?
Thank you.