java 7 prathiksha jain - copy

50
Java 7 Features and Enhancements By : Prathiksha Jain. [email protected] om prathikshajain.blogspot.

Upload: prathikshajain

Post on 10-May-2015

1.037 views

Category:

Education


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Java 7   prathiksha jain - copy

Java 7 Features and Enhancements

By :Prathiksha [email protected]/

Page 2: Java 7   prathiksha jain - copy

Abstract : Java version history.

A slate of small language changes(under Coin).

Changes to Jvm (Da Vinci Machine).

Page 3: Java 7   prathiksha jain - copy

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

Page 4: Java 7   prathiksha jain - copy

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.

Page 5: Java 7   prathiksha jain - copy

Java 7 :

Page 6: Java 7   prathiksha jain - copy

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.

Page 7: Java 7   prathiksha jain - copy

Virtual machine :

Page 8: Java 7   prathiksha jain - copy

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 ?

Page 9: Java 7   prathiksha jain - copy

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;

Page 10: Java 7   prathiksha jain - copy

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.

Page 11: Java 7   prathiksha jain - copy

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

Page 12: Java 7   prathiksha jain - copy

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.

Page 13: Java 7   prathiksha jain - copy

JVM

JVM

Page 14: Java 7   prathiksha jain - copy

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.

Page 15: Java 7   prathiksha jain - copy

How invokevirtual works ?

Input params

Return

type

Other requir

ed detailsInvokevirtual #4

Page 16: Java 7   prathiksha jain - copy

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

Page 17: Java 7   prathiksha jain - copy

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.

Page 18: Java 7   prathiksha jain - copy

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.

Page 19: Java 7   prathiksha jain - copy

ExplanationStatically Typed

How ?

Dynamically Typed

Page 20: Java 7   prathiksha jain - copy

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.

Page 21: Java 7   prathiksha jain - copy

Enhancing JVMStatically Typed + invokedynamic

Intermediate Layer

Dynamically Typed

Page 22: Java 7   prathiksha jain - copy

Function Call

Page 23: Java 7   prathiksha jain - copy

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.

Page 24: Java 7   prathiksha jain - copy

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.

Page 25: Java 7   prathiksha jain - copy

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.

Page 26: Java 7   prathiksha jain - copy

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.

Page 27: Java 7   prathiksha jain - copy

Earlier System of Method Call

invoke_____Your Method

in X language

Method Details

Page 28: Java 7   prathiksha jain - copy

The New System of invokedynamic

Invokedynamic

MHYour Method

in X language

Page 29: Java 7   prathiksha jain - copy

Questions ?

Page 30: Java 7   prathiksha jain - copy

Java 7

Language Changes Under Project Coin.

Page 31: Java 7   prathiksha jain - copy

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.

Page 32: Java 7   prathiksha jain - copy

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);

Page 33: Java 7   prathiksha jain - copy

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"); }

Page 34: Java 7   prathiksha jain - copy

Multi-Catch

try { Class c = Example.class; Field f = c.getDeclaredField("field"); f.set(null, 12); } catch (NoSuchFieldException | IllegalAccessException x) { x.printStackTrace(); }

Page 35: Java 7   prathiksha jain - copy

Current catch :

Page 36: Java 7   prathiksha jain - copy

Enhanced :

Page 37: Java 7   prathiksha jain - copy

Multi-catch catch (ServletException | MessagingException |

IOException ex) {    

Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);

}

Page 38: Java 7   prathiksha jain - copy

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.

Page 39: Java 7   prathiksha jain - copy

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(); }

Page 40: Java 7   prathiksha jain - copy

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();

}

Page 41: Java 7   prathiksha jain - copy

Improved Type Inference for Generic Instance Creation (diamond)

Before:

List<Book>bList=new ArrayList<Book>;

Current :

List<Book> bList=new ArrayList();

Page 42: Java 7   prathiksha jain - copy

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;

Page 43: Java 7   prathiksha jain - copy

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; }

Page 44: Java 7   prathiksha jain - copy

Earlier :

Page 45: Java 7   prathiksha jain - copy

Current :

Page 46: Java 7   prathiksha jain - copy

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;

Page 47: Java 7   prathiksha jain - copy

Modularity/API(Directory):

Page 49: Java 7   prathiksha jain - copy

Questions ?

Page 50: Java 7   prathiksha jain - copy

Thank you.