+ All Categories
Home > Technology > What’s new in java 8

What’s new in java 8

Date post: 08-Aug-2015
Category:
Upload: rajmahendra-hegde
View: 233 times
Download: 0 times
Share this document with a friend
40
WHAT’S NEW IN JAVA 8
Transcript
Page 1: What’s new in java 8

WHAT’S NEW IN JAVA 8

Page 2: What’s new in java 8

ABOUT ME Rajmahendra Hegde (Raj)• Working for ValueLabs as Scrum Master/Project

Lead

• 14 years Java development

• JUG Hyderabad Lead

• NetBeans Dream Team Member

• JCP Individual and Expert Group Member for 3 JSRs

• Comitter / Contributor: JBake, Forge, NetBeans, eWidgetFX, JavaFX, Gradle, Agorava, JRebirth, ScalaFX

Twitter: @rajonjava GitHub: rajmahendra LinkedIn: rajmahendra

Page 3: What’s new in java 8

AGENDA

Lambda Streams Date & Time Nashorn Java FX

Page 4: What’s new in java 8

JAVA LAMBDA !Java with functional style.

Page 5: What’s new in java 8

JAVA LAMBDA

Beginning of Functional flavor into Java platform

Lambda provides anonymous function to Java Replaces the old use of Anonymous inner

classes Write more compact code Parallel programming easier

Page 6: What’s new in java 8

ANONYMOUS INNER CLASS

Page 7: What’s new in java 8

LAMBDAFIDE ANONYMOUS INNER CLASS

Page 8: What’s new in java 8

LAMBDA EXPRESSION

( ) -> ( ) -> { } a -> a x -> { int y=4; return x * y; }

Page 9: What’s new in java 8

LAMBDA & FUNCTIONAL INTERFACE

Functional interface are interface with only one method.

Java space is filled of such Interfaces java.lang.Runnable.run(); java.util.Comparator.compare(T a, Tb);

java.awt.event.ActionListener.actionPerformed( ActionEvent e)

Page 10: What’s new in java 8

JAVA8 LAMBDA PACKAGE

Additionally Java8 also provides FunctionalInterface in package java.util.function to use with Lambdas. This package contains more than 40 commonly used FI Predicate<T>.test(T t) - Represents a predicate

(boolean-valued function) of one argument Consumer<T>.accept(T t) - Represents an operation

that accepts a single input argument and returns no result

Function<T, R>.R accept(T t) - Represents a function that accepts one argument and produces a result

Supplier<T>. T get() -Represents a supplier of results …

Page 11: What’s new in java 8

@FUNCTIONALINTERFACE

FunctionalInterface = Interface with only one method

Annotation is optional. Its only to make sure the interface is a FunctionalInterface

Any interface with only one method can be considered as FunctionalInterface

Page 12: What’s new in java 8

LAMBDA TO LOCAL VARIABLE

Page 13: What’s new in java 8

VARIABLE CAPTURE

Lambda can interact with the variable outside of its body

Using the variables outside of the Lambda expression is called Variable Capture

Effectively final

Page 14: What’s new in java 8

LAMBDA VS ANONYMOUS INNER CLASS

Lambda != Anonymous Inner Class Inner class can have state Inner class can have multiple methods this points to the object instance in a inner

class but points to the enclosed object in Lambda

Lambda != Anonymous Inner Class

Page 15: What’s new in java 8

METHOD REFERENCE

Use method reference when you use Lambda expression

Reduces boilerplate code NOTE: return type of lambda expression must

match with the signature of the referred method

Page 16: What’s new in java 8

METHOD REFERENCE TYPES

Static Method Printer::print

Instance Method myObject::print

Constructor Page::new

Page 17: What’s new in java 8

DEFAULT METHOD - INTERFACE

Provides a way to add new methods to interface without breaking the old implementation

default keyword is introduced to do this magic

Page 18: What’s new in java 8

NO MORE NULLPOINTEREXCEPTION !

Wrapper class to guard from Null Exception java.util.Optional<T> It contain a data or a empty

Page 19: What’s new in java 8

STREAMS

Page 20: What’s new in java 8

STREAMS

Streams are functional programming design pattern

Helps in processing sequential elements as sequentially or parallel

In java we use in many different places Retrieve database query as list objects Iterate over xml or json elements Read multiple lines Collections, map etc.

Page 21: What’s new in java 8

STREAMS IN JAVA

To create a stream you use a Source collection

Add a filter option to the stream Intermediate operation pipeline

And ends with Terminal operation which starts the stream process

Page 22: What’s new in java 8

STREAM LIFECYCLE

Creation – Stream is created with the collection object from source

Configure – get configured with the list of pipeline operators

Execution – Streams Terminal operation triggers the stream to starts by pulling objects into the pipeline

Cleanup

Page 23: What’s new in java 8

DATE & TIME API

Page 24: What’s new in java 8

OLD DATE/TIME API

java.util.Date java.sql.Date java.util.Calendar java.util.GregorianCalendar java.util.TimeZone java.text.DateFormat java.text.SimpleDateFormat

Page 25: What’s new in java 8

ISSUES WITH OLD API

All are just utility class (in java.util package or in java.text)

Date class contains both Date and Time Different Date class for SQL! Date doesn't have timezone Month is zero based, year is 1900 based. Not thread safe by default Arithmetic operations are hard to use

Page 26: What’s new in java 8

DATE AND TIME API

New dedicated date time package java.time Dedicated class for Date and time LocalDate

and LocalTime A composite class for both LocalDateTime Numerous way to create Date time objects Support for Truncation, TimeZones, Periods,

Durations and Chronologies More fluent, clean and simple API Immutable classes

Page 27: What’s new in java 8

LOCALDATE

Year-Month-Day Only for Date manipulation Stores or create one particular Date

Page 28: What’s new in java 8

LOCALTIME

Hours-Minutes-Seconds-nano

Page 29: What’s new in java 8

LOCALDATETIME

Combination of LocalDate & LocalTime All methods of both class available

Page 30: What’s new in java 8

TIMEZONE

Numerous timezones are available Some time complex to manipulate Some time it may change frequently TimeZone Classes

ZoneId – Europe/London etc. ZoneOffset – offset from UTC time ZoneRules – defines timezone rules ZonedDateTime – TimeZone aware Datetime

class

Page 31: What’s new in java 8

DURATION

Time based amount of time

Page 32: What’s new in java 8

PERIOD

Date based amount of time

Page 33: What’s new in java 8

NASHORN

Page 34: What’s new in java 8

NASHORN

Nashorn is a JavaScript Engine developed on Java

Light-weight, high-performance integrated into JRE

ECMAScript-262 Edition 5.1 language specification compliance

Java 8 shipped with a command-line tool called jjs

JDK 6 already shipped with Mozilla’s Rhino JavaScript Engine.

Page 35: What’s new in java 8

CALLING JAVASCRIPT FROM JAVA

Page 36: What’s new in java 8

CALLING JAVA FROM JAVASCRIPT

Page 38: What’s new in java 8

JAVA 9! September 22nd 2016 Timeline

2015-12-10  Feature Complete 2016-02-04  All Tests Run 2016-02-25  Ramp down Start 2016-04-21  Zero Bug Bounce 2016-06-16  Ramp down Phase 2 2016-07-21  Final Release Candidate 2016-09-22  General Availability

Project Jigsaw REPL …

Page 39: What’s new in java 8

Q & A

Rajmahendra Hegdehttp://meetup.com/jughyderabadTwitter: @rajonjava GitHub: rajmahendra LinkedIn: rajmahendra SllideShare: rajmahendra

Page 40: What’s new in java 8

Rajmahendra Hegdehttp://meetup.com/jughyderabadTwitter: @rajonjava GitHub: rajmahendra LinkedIn: rajmahendra SllideShare: rajmahendra


Recommended