SlideShare une entreprise Scribd logo
1  sur  102
Télécharger pour lire hors ligne
Java 8
                                 A New Beginning

                                     Russel Winder
                                   email: russel@winder.org.uk
                                  xmpp: russel@winder.org.uk
                                     twitter: @russel_winder
                                  Web: http://www.russel.org.uk




Copyright © 2013 Russel Winder                                    1
Aims, Goals and Objectives
     ●   Investigate why Java 8 is the greatest revolution in
         Java since 1995.
     ●   Investigate why some idioms from Java 0–7 lead to
         bad code in a post-Java 7 world.
     ●   Show what some of the idioms of Java 8 and beyond
         are likely to be.




Copyright © 2013 Russel Winder                                  2
Structure


                                 A Beginning

                                  A Middle

                                   An End


Copyright © 2013 Russel Winder                 3
Interstitial Advertisement




                                                     ?

Copyright © 2013 Russel Winder                           4
A Beginning




Copyright © 2013 Russel Winder                 5
Many people are still using Java 1.4.




Copyright © 2013 Russel Winder                         6
They see no reason to move to any
                            later version of Java:
                     no real benefit, for too much pain.




Copyright © 2013 Russel Winder                             7
Java 5 is a barrier too far for them.




Copyright © 2013 Russel Winder                      8
The Java 7 → Java 8 change
            is an even more disruptive change.




Copyright © 2013 Russel Winder                   9
This possibly means Java
                            is now a legacy language?




Copyright © 2013 Russel Winder                          10
No, it's an opportunity to stop
                 programming using 1970s techniques
                      and start using 1930s ones.




Copyright © 2013 Russel Winder                        11
Alonzo Church
                                 Stephen Kleene
                                    J B Rosser
                                   Alan Turing




Copyright © 2013 Russel Winder                    12
Another Beginning




Copyright © 2013 Russel Winder                       13
What's New in Java 8
     ●   Simon Ritter gave a talk about 55 things that are
         new in Java 8 at DevoxxUK 2013:
         http://www.devoxx.com/display/UK13/55+New+features+in+Java+SE+8
     ●   The video will appear on Parleys once encoded.




Copyright © 2013 Russel Winder                                             14
What's (Quite) Interesting in Java 8?
     ●   G1 garbage collector.   ●   Lambda expressions.
     ●   Nashorn.                ●   Enhanced collections.
     ●   JavaFX.




Copyright © 2013 Russel Winder                               15
G1 garbage collector is now the standard:

                                 No more PermGen.




Copyright © 2013 Russel Winder                           16
Nashorn comes as standard:

                   Server-side JavaScript without Rhino.




Copyright © 2013 Russel Winder                             17
JavaFX in the distribution and the classpath:

          You probably want to use GroovyFX though.




Copyright © 2013 Russel Winder                           18
Lambda expressions, and associated
                      collections enhancements.




Copyright © 2013 Russel Winder                          19
Closures




Copyright © 2013 Russel Winder              20
A closure is a function with an associated
        environment containing values for all the free
                  variables in the function.




Copyright © 2013 Russel Winder                           21
A Function



                                 Integer f(final Integer x) {
                                   return x * y ;
                                 }




                                                   Free variable.



Copyright © 2013 Russel Winder                                      22
A closure should be referentially transparent.

      Whenever function f is evaluated on the same value,
           a say, then the same value is returned.

                                 f(a) = b




Copyright © 2013 Russel Winder                              23
Java has had things (sort of) like this
                      since (almost) the beginning…




Copyright © 2013 Russel Winder                               24
A closure can be realized as an instance of
                 a class with a single method and
                      single assignment fields.




Copyright © 2013 Russel Winder                            25
public class ClosureClass {
                               private final Integer multiplier;
                               public ClosureClass(final Integer m) {
                                  multiplier = m;
                               }
                               public Integer call(final Integer i) {
                                  return multiplier * i;
                               }
                             }


                                                No free variables in call.

Copyright © 2013 Russel Winder                                               26
final ClosureClass multiplyBy5 = new ClosureClass(5);




                                 multiplyBy5.call(4)



Copyright © 2013 Russel Winder                                  27
Alternatively, using anonymous classes…




Copyright © 2013 Russel Winder                         28
public interface ClosureInterface<T> {
      T call(T t);
    }


                        final ClosureInterface<Integer> multiplyBy5 =
                           new ClosureInterface<Integer>() {
                                 public Integer call(final Integer i) {
                                   return 5 * i;
                                 }
                              };



                                                     multiplyBy5.call(4)

Copyright © 2013 Russel Winder                                             29
Java has accreted a reputation for being verbose.




Copyright © 2013 Russel Winder                          30
Unlike Groovy, Scala, Kotlin, Ceylon, etc.




Copyright © 2013 Russel Winder                             31
Or Python, D, C++ (!), etc.




Copyright © 2013 Russel Winder                                 32
final multiplyBy5 = {i -> 5 * i}

                                         final multiplyBy5 = {5 * it}



                                 multiplyBy5(4)




                                        multiplyBy5.call(4)

Copyright © 2013 Russel Winder                                          33
def multiplyBy5(i: Int): Int = 5 * i




                                           multiplyBy5(4)




Copyright © 2013 Russel Winder                                     34
How to do all this stuff in Java whilst
                    keeping backward compatibility?




Copyright © 2013 Russel Winder                               35
Copyright © 2013 Russel Winder   36
Do not use anonymous classes.




Copyright © 2013 Russel Winder                       37
Copyright © 2013 Russel Winder   38
We have method handles…




Copyright © 2013 Russel Winder                      39
…and invokedynamic.




Copyright © 2013 Russel Winder                         40
public interface ClosureInterface<T> {
      T call(T t);
    }




      final ClosureInterface<Integer> multiplyBy5 = i -> 5 * i;




                                  multiplyBy5.call(4)




Copyright © 2013 Russel Winder                                    41
Lambda Expressions
     ●   Functional interfaces – previously known as
         single abstract method (SAM) types.
     ●   Call site type inference.
     ●   No classes: JVM byte code synthesis, method
         handles and invokedynamic.




Copyright © 2013 Russel Winder                         42
Nothing much revolutionary here,
                        just a bit of syntactic sugar…




Copyright © 2013 Russel Winder                           43
…true (sort of), but that isn't all there is…




Copyright © 2013 Russel Winder                             44
A Middle




Copyright © 2013 Russel Winder              45
It all about where the iteration is.




Copyright © 2013 Russel Winder                           46
Explicit iteration

                                        vs.

                                 Implicit iteration




Copyright © 2013 Russel Winder                        47
Work with a (trivial) example:




Copyright © 2013 Russel Winder                           48
Calculate the sum of the squares of the
           numbers between 0 and 100 that are
                       divisible by 7.




Copyright © 2013 Russel Winder                      49
int sum = 0;
                                 for (int i = 0; i < 100; ++i) {
                                    if (i % 7 == 0) {
                                       sum += i * i;
                                    }
                                 }




Copyright © 2013 Russel Winder                                     50
(for (i <- 0 until 100; if i % 7 == 0) yield i * i).sum




Copyright © 2013 Russel Winder                                       51
sum({for (i in 0..100) if (i % 7 == 0) i * i})




Copyright © 2013 Russel Winder                                     52
sum(i * i for i in range(100) if i % 7 == 0)




Copyright © 2013 Russel Winder                                   53
(0 until 100).filter(i => i %7 == 0).map(i => i * i).sum




Copyright © 2013 Russel Winder                                     54
0.rangeTo(100).iterator().filter{i -> i % 7 == 0}.map{i -> i * i}.reduce{a, b -> a + b}




Copyright © 2013 Russel Winder                                                            55
sum((0..100).filter((Integer i) => i % 7 == 0).map((Integer i) => i * i))




Copyright © 2013 Russel Winder                                         56
(0..100).findAll{i -> i % 7 == 0}.collect{i -> i * i}.sum()




Copyright © 2013 Russel Winder                                        57
(0..100).findAll{it % 7 == 0}.collect{it * it}.sum()




Copyright © 2013 Russel Winder                                     58
Streams.intRange(0, 100).filter(i -> i % 7 == 0).map(i -> i * i).sum()




Copyright © 2013 Russel Winder                                           59
Higher Order Functions




Copyright © 2013 Russel Winder                            60
Functions that take functions as parameters
         and/or return functions as result.




Copyright © 2013 Russel Winder                    61
Some may be thinking:

                                 Why do I give a f###?




Copyright © 2013 Russel Winder                           62
After all nothing good has happened
                     in Java since Java 1.4.2.




Copyright © 2013 Russel Winder                       63
Copyright © 2013 Russel Winder   64
Because all computers are
                             now parallel computers.




Copyright © 2013 Russel Winder                          65
Copyright © 2013 Russel Winder   66
We have to move to an attitude where we
             assume our software is not uniprocessor.




Copyright © 2013 Russel Winder                          67
We have to actually do object-oriented
               and functional programming.




Copyright © 2013 Russel Winder                      68
Instead of just saying we write code using
                   Java, C++, etc. and so must be
               doing object-oriented programming.




Copyright © 2013 Russel Winder                            69
From earlier…




Streams.intRange(0, 100).filter(i -> i % 7 == 0).map(i -> i * i).sum()




Copyright © 2013 Russel Winder                                           70
Streams.intRange(0, 100).parallel().filter(i -> i % 7 == 0).map(i -> i * i).sum()




Copyright © 2013 Russel Winder                                                      71
Data parallelism with a single method call.




Copyright © 2013 Russel Winder                           72
GParsPool.withPool {
     (0..100).findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum()
   }




Copyright © 2013 Russel Winder                                              73
GParsPool.withPool {
             (0..100).parallel.filter{it % 7 == 0}.map{it * it}.sum()
           }




Copyright © 2013 Russel Winder                                          74
def value = (0..100)
     ParallelEnhancer.enhanceInstance(value)
     value = value.parallel.filter{it % 7 == 0}.map{it * it}.sum()




Copyright © 2013 Russel Winder                                       75
def value = (0..100)
     ParallelEnhancer.enhanceInstance(value)
     value = value.findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum()




Copyright © 2013 Russel Winder                                                   76
GPars and Groovy give you Java 8 style
                   approach and parallelism today
                      for Java with JDK6 or JDK7.




Copyright © 2013 Russel Winder                            77
Guava, TotallyLazy, and FunctionalJava can be used
      today to practice the functional approach using Java.




Copyright © 2013 Russel Winder                                78
Using Scala is an option for
                       doing functional programming*.




Copyright © 2013 Russel Winder
                                    *And just ignore Java altogether.   79
(0 until 100).par.filter(i => i %7 == 0).map(i => i * i).sum




Copyright © 2013 Russel Winder                                         80
Or just install the JDK8 Lambda release.




Copyright © 2013 Russel Winder                                 81

Copyright © 2013 Russel Winder       82
Copyright © 2013 Russel Winder   83
What is the Value of           ?

                                 Easy, it's known exactly.

                                          It's .

                                        Obviously.



Copyright © 2013 Russel Winder                                84
It's simples
                                          Александр Орлов   2009




Copyright © 2013 Russel Winder                                     85
Approximating 
     ●   What is it's value represented as a floating point
         number?
          ●    We can only obtain an approximation.
          ●    A plethora of possible algorithms to choose from, a
               popular one is to employ the following integral
               equation.


                                              1  1
                                             =∫0       dx
                                           4     1x 2




Copyright © 2013 Russel Winder                                       86
One Possible Algorithm
     ●   Use quadrature to estimate the value of the integral
         – which is the area under the curve.
                                   4 n          1
                                 = ∑i=1
                                   n           i−0.5 2
   Embarrassingly                          1      
   parallel.                                      n


                                                With n = 3 not much to do,
                                                but potentially lots of error.
                                                Use n = 107 or n = 109?



Copyright © 2013 Russel Winder                                                   87
Because addition is commutative and
                   associative, expression can be
               decomposed into sums of partial sums.




Copyright © 2013 Russel Winder                         88
a+b+c+d+e+f

                                      =

                         (a+b)+(c+d)+(e+f)




Copyright © 2013 Russel Winder                 89
Scatter – Gather




                                 map         reduce
Copyright © 2013 Russel Winder                        90
Code!



Copyright © 2013 Russel Winder           91
An End




Copyright © 2013 Russel Winder            92
Java is about to get the functional
                       programming approach.




Copyright © 2013 Russel Winder                          93
Scala, Groovy, Kotlin, Ceylon,
                                Python, D, C++, etc.
                            already have object-oriented
                                   and functional*.




                                                    * Well Scala has.
Copyright © 2013 Russel Winder                                          94
It's all about how your data evolves.

                  It's not about the flow of control.




Copyright © 2013 Russel Winder                          95
Closures the next “big thing” in Java?




Copyright © 2013 Russel Winder                         96
Yes*.



                                  *But will everyone ignore it?
Copyright © 2013 Russel Winder                                    97
No.



Copyright © 2013 Russel Winder         98
It is not the lambda expressions in Java 8 that is
                  the disruptive revolution.

              It's the change to the Java library that is.

                  It's all about those default methods*.



                                 * Aka defender methods, virtual extension methods.
Copyright © 2013 Russel Winder                                                    99
Interstitial Advertisement




                                                     ?

Copyright © 2013 Russel Winder                           100
The End




Copyright © 2013 Russel Winder             101
Java 8
                                 A New Beginning

                                    Russel Winder
                                  email: russel@winder.org.uk
                                 xmpp: russel@winder.org.uk
                                    twitter: @russel_winder
                                 Web: http://www.russel.org.uk/




Copyright © 2013 Russel Winder                                    102

Contenu connexe

Similaire à Java 8: a New Beginning

Java is Dead, Long Live Ceylon, Kotlin, etc
Java is Dead,  Long Live Ceylon, Kotlin, etcJava is Dead,  Long Live Ceylon, Kotlin, etc
Java is Dead, Long Live Ceylon, Kotlin, etcRussel Winder
 
GroovyFX: or how to program JavaFX easily
GroovyFX: or how to program JavaFX easily GroovyFX: or how to program JavaFX easily
GroovyFX: or how to program JavaFX easily Russel Winder
 
Java is dead, long live Scala, Kotlin, Ceylon, etc.
Java is dead, long live Scala, Kotlin, Ceylon, etc.Java is dead, long live Scala, Kotlin, Ceylon, etc.
Java is dead, long live Scala, Kotlin, Ceylon, etc.Russel Winder
 
Switch to Python 3…now…immediately
Switch to Python 3…now…immediatelySwitch to Python 3…now…immediately
Switch to Python 3…now…immediatelyRussel Winder
 
Java is dead, long live Scala Kotlin Ceylon etc.
Java is dead, long live Scala Kotlin Ceylon etc.Java is dead, long live Scala Kotlin Ceylon etc.
Java is dead, long live Scala Kotlin Ceylon etc.Russel Winder
 
Why Groovy When Java 8 or Scala, or…
Why Groovy When Java 8 or Scala, or…Why Groovy When Java 8 or Scala, or…
Why Groovy When Java 8 or Scala, or…Russel Winder
 
Testing: Python, Java, Groovy, etc.
Testing: Python, Java, Groovy, etc.Testing: Python, Java, Groovy, etc.
Testing: Python, Java, Groovy, etc.Russel Winder
 
The Case for Kotlin and Ceylon
The Case for Kotlin and CeylonThe Case for Kotlin and Ceylon
The Case for Kotlin and CeylonRussel Winder
 
GPars: Parallelism the Right Way
GPars: Parallelism the Right WayGPars: Parallelism the Right Way
GPars: Parallelism the Right WayRussel Winder
 

Similaire à Java 8: a New Beginning (10)

Java is Dead, Long Live Ceylon, Kotlin, etc
Java is Dead,  Long Live Ceylon, Kotlin, etcJava is Dead,  Long Live Ceylon, Kotlin, etc
Java is Dead, Long Live Ceylon, Kotlin, etc
 
GroovyFX: or how to program JavaFX easily
GroovyFX: or how to program JavaFX easily GroovyFX: or how to program JavaFX easily
GroovyFX: or how to program JavaFX easily
 
Java is dead, long live Scala, Kotlin, Ceylon, etc.
Java is dead, long live Scala, Kotlin, Ceylon, etc.Java is dead, long live Scala, Kotlin, Ceylon, etc.
Java is dead, long live Scala, Kotlin, Ceylon, etc.
 
Switch to Python 3…now…immediately
Switch to Python 3…now…immediatelySwitch to Python 3…now…immediately
Switch to Python 3…now…immediately
 
Java is dead, long live Scala Kotlin Ceylon etc.
Java is dead, long live Scala Kotlin Ceylon etc.Java is dead, long live Scala Kotlin Ceylon etc.
Java is dead, long live Scala Kotlin Ceylon etc.
 
Why Groovy When Java 8 or Scala, or…
Why Groovy When Java 8 or Scala, or…Why Groovy When Java 8 or Scala, or…
Why Groovy When Java 8 or Scala, or…
 
GPars Workshop
GPars WorkshopGPars Workshop
GPars Workshop
 
Testing: Python, Java, Groovy, etc.
Testing: Python, Java, Groovy, etc.Testing: Python, Java, Groovy, etc.
Testing: Python, Java, Groovy, etc.
 
The Case for Kotlin and Ceylon
The Case for Kotlin and CeylonThe Case for Kotlin and Ceylon
The Case for Kotlin and Ceylon
 
GPars: Parallelism the Right Way
GPars: Parallelism the Right WayGPars: Parallelism the Right Way
GPars: Parallelism the Right Way
 

Plus de Russel Winder

On Concurrency and Parallelism in the JVMverse
On Concurrency and Parallelism in the JVMverseOn Concurrency and Parallelism in the JVMverse
On Concurrency and Parallelism in the JVMverseRussel Winder
 
On the Architectures of Microservices: the next layer
On the Architectures of Microservices: the next layerOn the Architectures of Microservices: the next layer
On the Architectures of Microservices: the next layerRussel Winder
 
Fast Python? Don't Bother
Fast Python? Don't BotherFast Python? Don't Bother
Fast Python? Don't BotherRussel Winder
 
Making Python computations fast
Making Python computations fastMaking Python computations fast
Making Python computations fastRussel Winder
 
Tales from the Workshops
Tales from the WorkshopsTales from the Workshops
Tales from the WorkshopsRussel Winder
 
Making Computations Execute Very Quickly
Making Computations Execute Very QuicklyMaking Computations Execute Very Quickly
Making Computations Execute Very QuicklyRussel Winder
 
Spocktacular testing
Spocktacular testingSpocktacular testing
Spocktacular testingRussel Winder
 
Spocktacular Testing
Spocktacular TestingSpocktacular Testing
Spocktacular TestingRussel Winder
 
Is Groovy static or dynamic
Is Groovy static or dynamicIs Groovy static or dynamic
Is Groovy static or dynamicRussel Winder
 
Dataflow: the concurrency/parallelism architecture you need
Dataflow: the concurrency/parallelism architecture you needDataflow: the concurrency/parallelism architecture you need
Dataflow: the concurrency/parallelism architecture you needRussel Winder
 
Are Go and D threats to Python
Are Go and D threats to PythonAre Go and D threats to Python
Are Go and D threats to PythonRussel Winder
 
Is Groovy as fast as Java
Is Groovy as fast as JavaIs Groovy as fast as Java
Is Groovy as fast as JavaRussel Winder
 
Who needs C++ when you have D and Go
Who needs C++ when you have D and GoWho needs C++ when you have D and Go
Who needs C++ when you have D and GoRussel Winder
 
Why Go is an important programming language
Why Go is an important programming languageWhy Go is an important programming language
Why Go is an important programming languageRussel Winder
 
GPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for JavaGPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for JavaRussel Winder
 
Given Groovy Who Needs Java
Given Groovy Who Needs JavaGiven Groovy Who Needs Java
Given Groovy Who Needs JavaRussel Winder
 
It's All About Processes Communicating
It's All About Processes CommunicatingIt's All About Processes Communicating
It's All About Processes CommunicatingRussel Winder
 

Plus de Russel Winder (20)

On Concurrency and Parallelism in the JVMverse
On Concurrency and Parallelism in the JVMverseOn Concurrency and Parallelism in the JVMverse
On Concurrency and Parallelism in the JVMverse
 
On the Architectures of Microservices: the next layer
On the Architectures of Microservices: the next layerOn the Architectures of Microservices: the next layer
On the Architectures of Microservices: the next layer
 
Fast Python? Don't Bother
Fast Python? Don't BotherFast Python? Don't Bother
Fast Python? Don't Bother
 
Making Python computations fast
Making Python computations fastMaking Python computations fast
Making Python computations fast
 
Tales from the Workshops
Tales from the WorkshopsTales from the Workshops
Tales from the Workshops
 
Making Computations Execute Very Quickly
Making Computations Execute Very QuicklyMaking Computations Execute Very Quickly
Making Computations Execute Very Quickly
 
GPars Remoting
GPars RemotingGPars Remoting
GPars Remoting
 
GPars 2014
GPars 2014GPars 2014
GPars 2014
 
Spocktacular testing
Spocktacular testingSpocktacular testing
Spocktacular testing
 
Spocktacular Testing
Spocktacular TestingSpocktacular Testing
Spocktacular Testing
 
Is Groovy static or dynamic
Is Groovy static or dynamicIs Groovy static or dynamic
Is Groovy static or dynamic
 
Dataflow: the concurrency/parallelism architecture you need
Dataflow: the concurrency/parallelism architecture you needDataflow: the concurrency/parallelism architecture you need
Dataflow: the concurrency/parallelism architecture you need
 
Are Go and D threats to Python
Are Go and D threats to PythonAre Go and D threats to Python
Are Go and D threats to Python
 
Is Groovy as fast as Java
Is Groovy as fast as JavaIs Groovy as fast as Java
Is Groovy as fast as Java
 
Who needs C++ when you have D and Go
Who needs C++ when you have D and GoWho needs C++ when you have D and Go
Who needs C++ when you have D and Go
 
Why Go is an important programming language
Why Go is an important programming languageWhy Go is an important programming language
Why Go is an important programming language
 
GPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for JavaGPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for Java
 
Given Groovy Who Needs Java
Given Groovy Who Needs JavaGiven Groovy Who Needs Java
Given Groovy Who Needs Java
 
It's All About Processes Communicating
It's All About Processes CommunicatingIt's All About Processes Communicating
It's All About Processes Communicating
 
GPars
GParsGPars
GPars
 

Dernier

Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 

Java 8: a New Beginning

  • 1. Java 8 A New Beginning Russel Winder email: russel@winder.org.uk xmpp: russel@winder.org.uk twitter: @russel_winder Web: http://www.russel.org.uk Copyright © 2013 Russel Winder 1
  • 2. Aims, Goals and Objectives ● Investigate why Java 8 is the greatest revolution in Java since 1995. ● Investigate why some idioms from Java 0–7 lead to bad code in a post-Java 7 world. ● Show what some of the idioms of Java 8 and beyond are likely to be. Copyright © 2013 Russel Winder 2
  • 3. Structure A Beginning A Middle An End Copyright © 2013 Russel Winder 3
  • 4. Interstitial Advertisement ? Copyright © 2013 Russel Winder 4
  • 5. A Beginning Copyright © 2013 Russel Winder 5
  • 6. Many people are still using Java 1.4. Copyright © 2013 Russel Winder 6
  • 7. They see no reason to move to any later version of Java: no real benefit, for too much pain. Copyright © 2013 Russel Winder 7
  • 8. Java 5 is a barrier too far for them. Copyright © 2013 Russel Winder 8
  • 9. The Java 7 → Java 8 change is an even more disruptive change. Copyright © 2013 Russel Winder 9
  • 10. This possibly means Java is now a legacy language? Copyright © 2013 Russel Winder 10
  • 11. No, it's an opportunity to stop programming using 1970s techniques and start using 1930s ones. Copyright © 2013 Russel Winder 11
  • 12. Alonzo Church Stephen Kleene J B Rosser Alan Turing Copyright © 2013 Russel Winder 12
  • 13. Another Beginning Copyright © 2013 Russel Winder 13
  • 14. What's New in Java 8 ● Simon Ritter gave a talk about 55 things that are new in Java 8 at DevoxxUK 2013: http://www.devoxx.com/display/UK13/55+New+features+in+Java+SE+8 ● The video will appear on Parleys once encoded. Copyright © 2013 Russel Winder 14
  • 15. What's (Quite) Interesting in Java 8? ● G1 garbage collector. ● Lambda expressions. ● Nashorn. ● Enhanced collections. ● JavaFX. Copyright © 2013 Russel Winder 15
  • 16. G1 garbage collector is now the standard: No more PermGen. Copyright © 2013 Russel Winder 16
  • 17. Nashorn comes as standard: Server-side JavaScript without Rhino. Copyright © 2013 Russel Winder 17
  • 18. JavaFX in the distribution and the classpath: You probably want to use GroovyFX though. Copyright © 2013 Russel Winder 18
  • 19. Lambda expressions, and associated collections enhancements. Copyright © 2013 Russel Winder 19
  • 20. Closures Copyright © 2013 Russel Winder 20
  • 21. A closure is a function with an associated environment containing values for all the free variables in the function. Copyright © 2013 Russel Winder 21
  • 22. A Function Integer f(final Integer x) { return x * y ; } Free variable. Copyright © 2013 Russel Winder 22
  • 23. A closure should be referentially transparent. Whenever function f is evaluated on the same value, a say, then the same value is returned. f(a) = b Copyright © 2013 Russel Winder 23
  • 24. Java has had things (sort of) like this since (almost) the beginning… Copyright © 2013 Russel Winder 24
  • 25. A closure can be realized as an instance of a class with a single method and single assignment fields. Copyright © 2013 Russel Winder 25
  • 26. public class ClosureClass { private final Integer multiplier; public ClosureClass(final Integer m) { multiplier = m; } public Integer call(final Integer i) { return multiplier * i; } } No free variables in call. Copyright © 2013 Russel Winder 26
  • 27. final ClosureClass multiplyBy5 = new ClosureClass(5); multiplyBy5.call(4) Copyright © 2013 Russel Winder 27
  • 28. Alternatively, using anonymous classes… Copyright © 2013 Russel Winder 28
  • 29. public interface ClosureInterface<T> { T call(T t); } final ClosureInterface<Integer> multiplyBy5 = new ClosureInterface<Integer>() { public Integer call(final Integer i) { return 5 * i; } }; multiplyBy5.call(4) Copyright © 2013 Russel Winder 29
  • 30. Java has accreted a reputation for being verbose. Copyright © 2013 Russel Winder 30
  • 31. Unlike Groovy, Scala, Kotlin, Ceylon, etc. Copyright © 2013 Russel Winder 31
  • 32. Or Python, D, C++ (!), etc. Copyright © 2013 Russel Winder 32
  • 33. final multiplyBy5 = {i -> 5 * i} final multiplyBy5 = {5 * it} multiplyBy5(4) multiplyBy5.call(4) Copyright © 2013 Russel Winder 33
  • 34. def multiplyBy5(i: Int): Int = 5 * i multiplyBy5(4) Copyright © 2013 Russel Winder 34
  • 35. How to do all this stuff in Java whilst keeping backward compatibility? Copyright © 2013 Russel Winder 35
  • 36. Copyright © 2013 Russel Winder 36
  • 37. Do not use anonymous classes. Copyright © 2013 Russel Winder 37
  • 38. Copyright © 2013 Russel Winder 38
  • 39. We have method handles… Copyright © 2013 Russel Winder 39
  • 40. …and invokedynamic. Copyright © 2013 Russel Winder 40
  • 41. public interface ClosureInterface<T> { T call(T t); } final ClosureInterface<Integer> multiplyBy5 = i -> 5 * i; multiplyBy5.call(4) Copyright © 2013 Russel Winder 41
  • 42. Lambda Expressions ● Functional interfaces – previously known as single abstract method (SAM) types. ● Call site type inference. ● No classes: JVM byte code synthesis, method handles and invokedynamic. Copyright © 2013 Russel Winder 42
  • 43. Nothing much revolutionary here, just a bit of syntactic sugar… Copyright © 2013 Russel Winder 43
  • 44. …true (sort of), but that isn't all there is… Copyright © 2013 Russel Winder 44
  • 45. A Middle Copyright © 2013 Russel Winder 45
  • 46. It all about where the iteration is. Copyright © 2013 Russel Winder 46
  • 47. Explicit iteration vs. Implicit iteration Copyright © 2013 Russel Winder 47
  • 48. Work with a (trivial) example: Copyright © 2013 Russel Winder 48
  • 49. Calculate the sum of the squares of the numbers between 0 and 100 that are divisible by 7. Copyright © 2013 Russel Winder 49
  • 50. int sum = 0; for (int i = 0; i < 100; ++i) { if (i % 7 == 0) { sum += i * i; } } Copyright © 2013 Russel Winder 50
  • 51. (for (i <- 0 until 100; if i % 7 == 0) yield i * i).sum Copyright © 2013 Russel Winder 51
  • 52. sum({for (i in 0..100) if (i % 7 == 0) i * i}) Copyright © 2013 Russel Winder 52
  • 53. sum(i * i for i in range(100) if i % 7 == 0) Copyright © 2013 Russel Winder 53
  • 54. (0 until 100).filter(i => i %7 == 0).map(i => i * i).sum Copyright © 2013 Russel Winder 54
  • 55. 0.rangeTo(100).iterator().filter{i -> i % 7 == 0}.map{i -> i * i}.reduce{a, b -> a + b} Copyright © 2013 Russel Winder 55
  • 56. sum((0..100).filter((Integer i) => i % 7 == 0).map((Integer i) => i * i)) Copyright © 2013 Russel Winder 56
  • 57. (0..100).findAll{i -> i % 7 == 0}.collect{i -> i * i}.sum() Copyright © 2013 Russel Winder 57
  • 58. (0..100).findAll{it % 7 == 0}.collect{it * it}.sum() Copyright © 2013 Russel Winder 58
  • 59. Streams.intRange(0, 100).filter(i -> i % 7 == 0).map(i -> i * i).sum() Copyright © 2013 Russel Winder 59
  • 60. Higher Order Functions Copyright © 2013 Russel Winder 60
  • 61. Functions that take functions as parameters and/or return functions as result. Copyright © 2013 Russel Winder 61
  • 62. Some may be thinking: Why do I give a f###? Copyright © 2013 Russel Winder 62
  • 63. After all nothing good has happened in Java since Java 1.4.2. Copyright © 2013 Russel Winder 63
  • 64. Copyright © 2013 Russel Winder 64
  • 65. Because all computers are now parallel computers. Copyright © 2013 Russel Winder 65
  • 66. Copyright © 2013 Russel Winder 66
  • 67. We have to move to an attitude where we assume our software is not uniprocessor. Copyright © 2013 Russel Winder 67
  • 68. We have to actually do object-oriented and functional programming. Copyright © 2013 Russel Winder 68
  • 69. Instead of just saying we write code using Java, C++, etc. and so must be doing object-oriented programming. Copyright © 2013 Russel Winder 69
  • 70. From earlier… Streams.intRange(0, 100).filter(i -> i % 7 == 0).map(i -> i * i).sum() Copyright © 2013 Russel Winder 70
  • 71. Streams.intRange(0, 100).parallel().filter(i -> i % 7 == 0).map(i -> i * i).sum() Copyright © 2013 Russel Winder 71
  • 72. Data parallelism with a single method call. Copyright © 2013 Russel Winder 72
  • 73. GParsPool.withPool { (0..100).findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum() } Copyright © 2013 Russel Winder 73
  • 74. GParsPool.withPool { (0..100).parallel.filter{it % 7 == 0}.map{it * it}.sum() } Copyright © 2013 Russel Winder 74
  • 75. def value = (0..100) ParallelEnhancer.enhanceInstance(value) value = value.parallel.filter{it % 7 == 0}.map{it * it}.sum() Copyright © 2013 Russel Winder 75
  • 76. def value = (0..100) ParallelEnhancer.enhanceInstance(value) value = value.findAllParallel{it % 7 == 0}.collectParallel{it * it}.sum() Copyright © 2013 Russel Winder 76
  • 77. GPars and Groovy give you Java 8 style approach and parallelism today for Java with JDK6 or JDK7. Copyright © 2013 Russel Winder 77
  • 78. Guava, TotallyLazy, and FunctionalJava can be used today to practice the functional approach using Java. Copyright © 2013 Russel Winder 78
  • 79. Using Scala is an option for doing functional programming*. Copyright © 2013 Russel Winder *And just ignore Java altogether. 79
  • 80. (0 until 100).par.filter(i => i %7 == 0).map(i => i * i).sum Copyright © 2013 Russel Winder 80
  • 81. Or just install the JDK8 Lambda release. Copyright © 2013 Russel Winder 81
  • 82.  Copyright © 2013 Russel Winder 82
  • 83. Copyright © 2013 Russel Winder 83
  • 84. What is the Value of ? Easy, it's known exactly. It's . Obviously. Copyright © 2013 Russel Winder 84
  • 85. It's simples Александр Орлов 2009 Copyright © 2013 Russel Winder 85
  • 86. Approximating  ● What is it's value represented as a floating point number? ● We can only obtain an approximation. ● A plethora of possible algorithms to choose from, a popular one is to employ the following integral equation.  1 1 =∫0 dx 4 1x 2 Copyright © 2013 Russel Winder 86
  • 87. One Possible Algorithm ● Use quadrature to estimate the value of the integral – which is the area under the curve. 4 n 1 = ∑i=1 n i−0.5 2 Embarrassingly 1  parallel. n With n = 3 not much to do, but potentially lots of error. Use n = 107 or n = 109? Copyright © 2013 Russel Winder 87
  • 88. Because addition is commutative and associative, expression can be decomposed into sums of partial sums. Copyright © 2013 Russel Winder 88
  • 89. a+b+c+d+e+f = (a+b)+(c+d)+(e+f) Copyright © 2013 Russel Winder 89
  • 90. Scatter – Gather map reduce Copyright © 2013 Russel Winder 90
  • 91. Code! Copyright © 2013 Russel Winder 91
  • 92. An End Copyright © 2013 Russel Winder 92
  • 93. Java is about to get the functional programming approach. Copyright © 2013 Russel Winder 93
  • 94. Scala, Groovy, Kotlin, Ceylon, Python, D, C++, etc. already have object-oriented and functional*. * Well Scala has. Copyright © 2013 Russel Winder 94
  • 95. It's all about how your data evolves. It's not about the flow of control. Copyright © 2013 Russel Winder 95
  • 96. Closures the next “big thing” in Java? Copyright © 2013 Russel Winder 96
  • 97. Yes*. *But will everyone ignore it? Copyright © 2013 Russel Winder 97
  • 98. No. Copyright © 2013 Russel Winder 98
  • 99. It is not the lambda expressions in Java 8 that is the disruptive revolution. It's the change to the Java library that is. It's all about those default methods*. * Aka defender methods, virtual extension methods. Copyright © 2013 Russel Winder 99
  • 100. Interstitial Advertisement ? Copyright © 2013 Russel Winder 100
  • 101. The End Copyright © 2013 Russel Winder 101
  • 102. Java 8 A New Beginning Russel Winder email: russel@winder.org.uk xmpp: russel@winder.org.uk twitter: @russel_winder Web: http://www.russel.org.uk/ Copyright © 2013 Russel Winder 102