• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java 8: a New Beginning
 

Java 8: a New Beginning

on

  • 3,399 views

Slides from my ACCU 2013 session introducing some of the Java 8 features.

Slides from my ACCU 2013 session introducing some of the Java 8 features.

Statistics

Views

Total Views
3,399
Views on SlideShare
3,213
Embed Views
186

Actions

Likes
2
Downloads
90
Comments
0

6 Embeds 186

http://lanyrd.com 80
http://eventifier.co 63
https://twitter.com 37
http://www.linkedin.com 4
https://web.tweetdeck.com 1
https://www.google.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java 8: a New Beginning Java 8: a New Beginning Presentation Transcript

    • 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.ukCopyright © 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 EndCopyright © 2013 Russel Winder 3
    • Interstitial Advertisement ?Copyright © 2013 Russel Winder 4
    • A BeginningCopyright © 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, its 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 TuringCopyright © 2013 Russel Winder 12
    • Another BeginningCopyright © 2013 Russel Winder 13
    • Whats 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
    • Whats (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
    • ClosuresCopyright © 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) = bCopyright © 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 isnt all there is…Copyright © 2013 Russel Winder 44
    • A MiddleCopyright © 2013 Russel Winder 45
    • It all about where the iteration is.Copyright © 2013 Russel Winder 46
    • Explicit iteration vs. Implicit iterationCopyright © 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).sumCopyright © 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).sumCopyright © 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 FunctionsCopyright © 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).sumCopyright © 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, its known exactly. Its . Obviously.Copyright © 2013 Russel Winder 84
    • Its simples Александр Орлов 2009Copyright © 2013 Russel Winder 85
    • Approximating  ● What is its 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 2Copyright © 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 reduceCopyright © 2013 Russel Winder 90
    • Code!Copyright © 2013 Russel Winder 91
    • An EndCopyright © 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
    • Its all about how your data evolves. Its 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. Its the change to the Java library that is. Its all about those default methods*. * Aka defender methods, virtual extension methods.Copyright © 2013 Russel Winder 99
    • Interstitial Advertisement ?Copyright © 2013 Russel Winder 100
    • The EndCopyright © 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