Java7

22
Java 7 came with super power

description

JAVA7 features ppts with sample programs.

Transcript of Java7

Page 1: Java7

Java 7

came with super power

Page 2: Java7

2

agenda :

> what is new in literals?> switch case with string> diamond operator in collections> improved exception handling > automatic resource management> new features in nio api > join&forks in threads> swing enhancements> dynamic-typed languages> proposal for etrali> queries ..?

JAVA 7

Page 3: Java7

3

what is new in literals?

numeric literals with underscore:

underscore is allowed only for the numeric.

underscore is used identifying the numeric value.

For example:

1000 will be declared as int amount = 1_000.

Then with 1million? How easy it will to understand?

JAVA 7

Page 4: Java7

4

binary literals for numeric:

in java6, numeric literal can declared as decimal & hexadecimal.

in java7, we can declare with binary value but it should start with “0b”

for example:> Int twelve = 12; // decimal> int sixPlusSix = 0xC; //hexadecimal> int fourTimesThree = 0b1100;.//Binary value

New feature

JAVA 7

Page 5: Java7

5

switch case with string :

> if want the compare the string then we should use the if-else> now in java7, we can use the switch to compare.

JAVA6 JAVA7

Public void processTrade(Trade t) {

String status = t.getStatus();

If (status.equals(“NEW”)

newTrade(t);

Else if (status.equals(“EXECUTE”){

executeTrade(t);

Else

pendingTrade(t);

}

Public void processTrade(Trade t) {

Switch (t.getStatus()) {

Case NEW: newTrade(t);

break;

Case EXECUTE: executeTrader(t);

break;

Default : pendingTrade(t);

}

JAVA 7

Page 6: Java7

6

diamond operator in collections :

> java6, List<Trade> trader = new ArrayList<Trade>();> Java7, List<Trade> trader = new ArrayList<>();

> How cool is that? You don't have to type the whole list of types for the instantiation. Instead you use the <> symbol, which is called diamond operator.

> Note that while not declaring the diamond operator is legal, as trades = new ArrayList(), it will make the compiler generate a couple of type-safety warnings.

JAVA 7

Page 7: Java7

7

performance improved in collections :

> By using the Diamond operator it improve the performance increases compare to

> Performance between the JAVA5 to JAVA6 --

> Performance between the JAVA6 to JAVA7 -- 45% as be increased.

> this test percentage was given by ibm company.

JAVA 7

45%

15%

Page 8: Java7

8

Improved exception handling :

> java 7 introduced multi-catch functionality to catch multiple exception types using a single catch block.

> the multiple exceptions are caught in one catch block by using a '|' operator.

> this way, you do not have to write dozens of exception catches.> however, if you have bunch of exceptions that belong to different

types, then you could use "multi -catch" blocks too.

The following snippet illustrates this: try { methodThatThrowsThreeExceptions(); } catch (ExceptionOne e | ExceptionTwo | ExceptionThree e) {

// log and deal with ExceptionTwo and ExceptionThree}

kicked out

JAVA 7

Page 9: Java7

9

automatic resource management :

> resource such as the connection, files, input/output resource ect., should be closed manually by developer.

> usually use try-finally block close the respective resource. > in java7, the resource are closed automatically. By using the

AutoCloseable interface .> It will close resource the automatically when it come out of the try

block need for the close resource by the developer.

try (FileOutputStream fos = new FileOutputStream("movies.txt"); DataOutputStream dos = new DataOutputStream(fos)) { dos.writeUTF("Java 7 Block Buster"); } catch (IOException e) { // log the exception }

no need to close resources

JAVA 7

Page 10: Java7

10

New input/output api [jsr-203] :

> the NIO 2.0 has come forward with many enhancements. It's also introduced new classes to ease the life of a developer when working with multiple file systems.

> a new java.nio.file package consists of classes and interfaces such as Path, Paths, FileSystem, FileSystems and others.

> File.delete(), files.deleteIfExists(path),file.move(..) & file.copy(..) to act on a file system efficiently.

> one of my favorite improvements in the JDK 7 release is the addition of File Change Notifications.

JAVA 7

Page 11: Java7

11

> this has been a long-awaited feature that's finally carved into NIO 2.0.

> the WatchService API lets you receive notification events upon changes to the subject (directory or file).

> the steps involved in implementing the API are:

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 receive (create, modify or delete events)

4. you have to start an infinite loop to listen to events5. when an event occurs, a WatchKey is placed into the queue6. consume the WatchKey and invoke queries on it

JAVA 7

Page 12: Java7

12

fork & join in thread [jsr-166] :

> What is concurrency?

> The fork-join framework allows you to distribute a certain task on several workers and when wait for the result.

> fork&join should extend the RecursiveAction. RecursiveAction is abstract class should implement the compute().

> forkJoinPool implements the core work-stealing algorithm and can execute forktasks.

> goal is to improve the performance of the application.

JAVA 7

Page 13: Java7

13

dynamic-typed language[jsr-292] :

> supporting Dynamically Typed Languages on the Java Platform, which should ensure that dynamically typed languages run faster in the JVM than they did previously.

> languages like Ruby, or Groovy, will now execute on the JVM with performance at or close to that of native Java code

> what is static typed language?> what is dynamic typed language?

JAVA 7

Page 14: Java7

14

swing :

JLAYER:

> JLAYER class is a flexible & powerful decorator for swing components.

> It enables to draw on components & respond to component events without modifying the underlying component directly.

JAVA 7

Page 15: Java7

15

java8

> Java8 [mid of 2013]1. Language-level support for lambda expressions(->).2. Closure [not yet confirmed]3. Tight integration with JavaFX

Page 16: Java7

16

Proposal for etrali on up gradation :

what are the befits for Etrali ? 1. increasing the performance up to 20% to 50% 2. size of the code will be reduced 3. reduce heap space or memory leak exceptions 4. project life span will increase. what are the benefits for our organization ? 1. using the updated technologies 2. we will get some more working days on Etrali 3. profit on worked days

JAVA 7

Page 17: Java7

17

sample code

JDK7Samples.rar

Page 18: Java7

18

references :

> http://www.oracle.com/technetwork/articles/java/fork-join-422606.html

> http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html

> http://docs.oracle.com/javase/7/docs> http://geeknizer.com/java-7-whats-new-performance-benchmark-

1-5-1-6-1-7/> http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html> http://rajakannappan.blogspot.in/2010/05/new-features-in-java-7-

dolphin.html

JAVA 7

Page 19: Java7

19

Queries

JAVA 7

ANY QUERIES ..?

Page 20: Java7

Thank you

Java 7

THANK YOU

Page 21: Java7

21

> Static typed programming languages are those in which variables need not be defined before they’re used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they’re employed. Java is an example of a static typed language; C and C++ are also static typed languages. Note that in C (and C++ also), variables can be cast into other types, but they don’t get converted; you just read them assuming they are another type.

> Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:

> /* C code */  > static int num, sum; // explicit declaration  > num = 5; // now use the variables  > sum = 10;  > sum = sum + num;  

JAVA 7

Page 22: Java7

22

> http://www.sitepoint.com/typing-versus-dynamic-typing/> Dynamic typed programming languages are those

languages in which variables must necessarily be defined before they are used. This implies that dynamic typed languages do not require the explicit declaration of the variables before they’re used. Python is an example of a dynamic typed programming language, and so is PHP. Consider the following example:

JAVA 7