Java 7   prathiksha jain - copy
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Java 7 prathiksha jain - copy

  • 1,295 views
Uploaded on

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.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,295
On Slideshare
1,271
From Embeds
24
Number of Embeds
2

Actions

Shares
Downloads
12
Comments
0
Likes
0

Embeds 24

http://prathiksh-java.blogspot.com 15
http://prathiksh-java.blogspot.in 9

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Jvm understands Byte code. Javac provides the byte code of the perticular language.[ the method invocation in our java program is translated to the Byte code, where the method code is translated as invokevirtual(in case Statically typed) invokeDynamic in case(Dynamically typed), when Javm reads the class file(Byte code) and whenever it encounters the method invocation it will refer to the perticular memory location given by invokeVirtualmethod to get the return type of it and the type of parameters you pass to the method. And then continues with the execution.But in case of Dynamically typed languages we where not provide with any such methods to make them understandable to JVm, But in Java 7 they have come up with new method called Invoke dynamic for Dynamically typed languages i.e each methos call in dynamically typed language will be translated to invokedynamic and provide with the reference of the memory where the operands required to exe it. But in this the type of the operand will only be understood @ runtime, now compiler writer must write the code such a way that all the operands will be loaded prior to the method invocation and when Jvm stars with the exe and encounters the invokedynamic it will refer to the memory location where the type of variables and the return type of it are stored. By fetching the data it continues with the exe.We did not have any platform to run dynamically typed language and make them platform independent.But with java7 they have made it possible for the compiler writer to write the compiler code easier than ever.So if we are coming up with our own programming language with the Byte code of it then we can actually run it on JVM of Java 7. which supports both static typed + Dynamic typed language byte code.
  • There is no link between the Java Program (source code) and the JVM. JVM will not understand java pgm it only deals with the byte code version of the Java Program. Byte code for the particular language has been provided by compiler designer.(i.e) Compiler must convert the source code to Byte code.Javac : Compliler which interacts with the Source code then converts it on to Byte code. i.e the particular complier for particular language has been implemented with the byte code version of it.
  • You can come up with any programming language with the compiler for it(complier which converts them on to byte code).Then u can run them on JVM. Earlier it was restricted to StaticType, Now they have come up with the dynamic type even.If we have a byte code version for the perticular Dynamic type Programing then we can easiely run on any JVm.
  • How Does it Possible to Run any Dynamic Programming language to run JVM : Jvm has been provided with the enhanced method invocation s called invokedynamic i.e Invocation takes place @ runtime with setting up the parameters for it.
  • In Dynamically typed language : The Byte code will have the method called invokeDynamic , it is the new construct added in Java 7 in case of dynamically typed langage.It will fix some memory location address of this location will be known to the compiler @ runtime it will load this memory with all the details required for the method then it will invoke dynamic methos passing this memory location (reference) as an parameter. @ runtime by JVM exe takes place.
  • Jvm is the component , which makes technology(java app) responsible for its independence.Javac compiles. Jvm class loader,RE,JIT.Jvm doesn’t know anything about the Java Programming: I just interacts with the Byte code version of the Java source code.
  • #4 is the JVM equivalent of register.Load statements.Register which will have it memory in Microprocessor, they are very fast.All the operation should be inregister stores result in register for faster access.
  • Method Handle :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.How does the JVM find the method if the receiver type isn't supplied?A New Dynamic Linkage Mechanism: Method HandlesThe 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.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.
  • Both the sides we must say that this is the map of book, this is called diamond at left hand side it looks diamond.

Transcript

  • 1. Java 7 Features and EnhancementsBy :Prathiksha Jain.prathikshajain@hotmail.comprathikshajain.blogspot.com/
  • 2. Abstract : Java version history.A slate of small language changes(under Coin). Changes to Jvm (Da Vinci Machine).
  • 3. 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
  • 4. 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.
  • 5. Java 7 :
  • 6. 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.
  • 7. Virtual machine :
  • 8. 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 ?
  • 9. 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;
  • 10. Static and Dynamic Javais 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.
  • 11. Static OR Dynamic ? Which Type System is better ? Static Typed Languages Dynamic Typed Languages Type Checking while compiling Type Checking during Run Time the code. Compiled Code that executes Allows compiler to run quickly Quickly Knowledge of Data Type Helps Reduces the Compiler to Optimize the EDIT-COMPILE-TEST-DEBUG Machine Code Time. -------- Helps in Duck Typing and Code Reuse
  • 12. 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. 1. invokespecial - Invokes a method without reference to the type of the receiver. Methods called this way can be constructors, superclass methods, or private methods.
  • 13. JVM JVM
  • 14. Answers - JVM What is JVM ?  JVM is the component of the technology responsible for its hardware – operating system independence.  TheJVM knows nothing about Java Programming Language.
  • 15. How invokevirtual works ? Input params Return type Other required details
  • 16. 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
  • 17. 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 methods 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 isnt in that class, the JVM searches up the chain of the classs superclasses.
  • 18. Why We Cannot Translate MethodCall of Dynamic Typed Language toStatic 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.
  • 19. ExplanationDynamically Typed How ? Statically Typed
  • 20. 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 animplementer of a dynamic language to translate a methodinvocation into bytecode without having to specify a targettype that contains the method. The method specification inthis case is a simplified constant pool reference, whichspecifies only a method name and type descriptor. Thedescriptor specifies the return type of the call and the type ofmethod arguments.
  • 21. Enhancing JVM Dynamically Typed Intermediate LayerStatically Typed + invokedynamic
  • 22. Function Call
  • 23. 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.
  • 24. How does the JVM find themethod if the receiver type isnt 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.
  • 25. 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. Itis accessed through a pointer structure, as opposed to a linked name.
  • 26. 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 doesnt 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.
  • 27. Earlier System of Method Call Your Methodinvoke_____ Method Details in X language
  • 28. The New System of invokedynamic Your MethodInvokedynamic MH in X language
  • 29. Java 7Language Changes UnderProject Coin.
  • 30. 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.
  • 31. 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);
  • 32. Project Coin Language Changes :Strings In Switch : String data = "Howdy, all"; switch (data) { case "Howdy, all": System.out.println("It works!"); default: System.out.println("No love"); }
  • 33. Multi-Catchtry{ Class c = Example.class; Field f = c.getDeclaredField("field"); f.set(null, 12);}catch (NoSuchFieldException | IllegalAccessException x){ x.printStackTrace();}
  • 34. Current catch :
  • 35. Enhanced :
  • 36. Multi-catch catch (ServletException | MessagingException | IOException ex) { Logger.getLogger(TestServlet.class.getName()).log(Le vel.SEVERE, null, ex); }
  • 37. 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.
  • 38. 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(); }
  • 39. 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 { // Remove br.close(); }
  • 40. Improved Type Inference for Generic Instance Creation (diamond)Before:List<Book>bList=new ArrayList<Book>;Current :List<Book> bList=new ArrayList();
  • 41. 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;
  • 42. Strings in switch Currentlyyou can only use numbers or enums in switch statements. String has been added as a candidate. Strings = ... switch(s) { case "quux": processQuux(s); break; case "bar": processFooOrBar(s); break; case "baz": processBaz(s); break; processDefault(s); break; }
  • 43. Earlier :
  • 44. Current :
  • 45. 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;
  • 46. Modularity/API(Directory):
  • 47. References : /Explore… http://java.sun.com/developer/technical Articles/DynTypeLang/ http://en.wikipedia.org/wiki/Java_version _history http://www.oracle.com/us/corporate/ev ents/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
  • 48. Questions ?
  • 49. Thank you.