Date post: | 08-Aug-2015 |
Category: |
Technology |
Upload: | rajmahendra-hegde |
View: | 233 times |
Download: | 0 times |
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
AGENDA
Lambda Streams Date & Time Nashorn Java FX
JAVA LAMBDA !Java with functional style.
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
ANONYMOUS INNER CLASS
LAMBDAFIDE ANONYMOUS INNER CLASS
LAMBDA EXPRESSION
( ) -> ( ) -> { } a -> a x -> { int y=4; return x * y; }
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)
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 …
@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
LAMBDA TO LOCAL VARIABLE
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
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
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
METHOD REFERENCE TYPES
Static Method Printer::print
Instance Method myObject::print
Constructor Page::new
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
NO MORE NULLPOINTEREXCEPTION !
Wrapper class to guard from Null Exception java.util.Optional<T> It contain a data or a empty
STREAMS
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.
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
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
DATE & TIME API
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
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
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
LOCALDATE
Year-Month-Day Only for Date manipulation Stores or create one particular Date
LOCALTIME
Hours-Minutes-Seconds-nano
LOCALDATETIME
Combination of LocalDate & LocalTime All methods of both class available
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
DURATION
Time based amount of time
PERIOD
Date based amount of time
NASHORN
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.
CALLING JAVASCRIPT FROM JAVA
CALLING JAVA FROM JAVASCRIPT
JAVAFX• http://
www.slideshare.net/rajmahendra/javafx-2-rich-desktop-platform
• http://www.slideshare.net/Codemotion/codemotion-whatsnewforjavafxinjdk8
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 …
Q & A
Rajmahendra Hegdehttp://meetup.com/jughyderabadTwitter: @rajonjava GitHub: rajmahendra LinkedIn: rajmahendra SllideShare: rajmahendra
Rajmahendra Hegdehttp://meetup.com/jughyderabadTwitter: @rajonjava GitHub: rajmahendra LinkedIn: rajmahendra SllideShare: rajmahendra