Download presentation
Presentation is loading. Please wait.
Published byNora Wilcox Modified over 9 years ago
1
Java 8 New Features Mehdi Einali Advanced Programming in Java 1
2
2 agenda Java versions Default method Functional interface Double colon Functional programing Lambda expression Optional Streams
3
3 Java versions
4
4 Java versions-1 VersionRelease DateMain Features 1.01996 The first stable version 1.11997 an extensive retooling of the AWT event model Inner classes added to language Reflection I18n and Unicode Support 1.2(j2se)1998 Swing Framework Collection Framework 1.32000 Java performance engine Hotspot added RMI added 1.42002 Regular Expression support Exception Handling high-level api added Non-Blocking IO(NIO 1) added Java Web Start
5
5 Java versions-2 VersionRelease DateMain Features 1.5( J2SE 5 ) 2004-2009 22 update Generic Types Annotation Autoboxing Enumorations Varargs Foreach 1.6( J2SE 6 ) 2006-2013 More than 50 updates Last one 113 Performance improvements Security Improvements JDBC 4 J2SE 7 2011-now Many update Last one 101 String in switch NIO 2 Diamond in generic types Concurrency high level API
6
6 Java versions-3 VersionRelease DateMain Features Java SE 8 2014-now Last update 92 Lambda expression or closure (Functional Programming) JavaScript embedded emulator Annotation on Java Type default method in interface Java SE 9 Scheduled for March 2017 Better support for multi-gigabyte heaps Java module system (OSGI like built-in system) Reactive Streams support Better support for parallel systems Java SE 10Scheduled for March 2018 Object without Identity Support 64-bit addressable arrays
7
7 Default method in interfaces
8
8 Old interface
9
9 Default keyword
10
10 Default method In ‘the strictest sense’, Default methods are a step backwards because they allow you to ‘pollute’ your interfaces with code they provide the most elegant way to allow backwards compatibility The implementation will be used as default if a concrete class does not provide implementation for that method.
11
11 Notes on default default method can only access its arguments as interfaces do not have any state. Remember interface don’t have object state and its fields are implicitly public static final You can write static method in interface and call it from default method Both on them don’t depend on method arguments and class variables rather than object state All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.
12
12 Notes on default When we extend an interface that contains a default method, we can perform following: Not override the default method and will inherit the default method. Override the default method similar to other methods we override in subclass. Redeclare default method as abstract, which force subclass to override it
13
13 Conflicts multiple interface Since classes in java can implement multiple interfaces, there could be a situation where 2 or more interfaces has a default method with the same signature hence causing conflicts as java will not know what methods to use at a time.
14
14 Functional Programming
15
15 Java programing paradigms Prior to JavaSE 8, Java supported three programming paradigms procedural programming object-oriented programming generic programming. Java SE 8 adds functional programming programming that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Stateless functions is building block of code
16
16 Why functional programing? Multi core and multi thread processor increase every day Functions can pass as building block of computations. Distributes on threads Collections is every important data structure in algorithms In memory storage paradigms are increasing Collection operations is more frequent process consuming codes
17
17 collection iteration External iteration Developer code do iterations What you want to accomplish collection oriented task and how Disadvantages: Its error prone (keep all intermediate variables consistent) Duplicate logic In be optimized in library level Internal iterations Just say what you want to accomplish collection oriented task.
18
18 compare
19
19 Functional interface
20
20 Functional interface An interface that contains exactly one abstract method (and may also contain default and static methods).also known as single abstract method (SAM) interfaces Functional interfaces are used extensively in functional programming, because they act as an object oriented model for a function. To ensure that your interface meet the requirements, you should add the optional @FunctionalInterface annotation. java.util.function package for basic functional interfaces
21
21 sample
22
22
23
23 Double colon
24
24 Double colon static method Java 8 enables you to pass references of methods or constructors via the :: keyword
25
25 Double colon object method
26
26 Double colon constructor
27
27 Lambda expression
28
28 Lambda expressions Functional programming is accomplished with lambda expressions. A lambda expression represents an anonymous method—a shorthand notation for implementing a functional interface, similar to an anonymous inner class A lambda consists of a parameter list followed by the arrow token (->) and a body (parameterList) -> {statements} (int x, int y) -> {return x + y;}
29
29 syntax When the body contains only one expression, the return keyword and curly braces may be omitted, as in: (x, y) -> x + y in this case, the expression’s value is implicitly returned When the parameter list contains only one parameter, the parentheses may be omitted, as in value -> System.out.printf("%d ", value) () -> System.out.println("Welcome to lambdas!")
30
30 scoping Accessing outer scope variables from lambda expressions is very similar to anonymous objects. You can access effectively final variables from the local outer scope as well as instance fields and static variables. effectively final means: Explicitly final Implicitly final: not explicit final keyword but acts as final variable
31
31
32
32 optional
33
33 optional Optionals are not functional interfaces, instead it's a nifty utility to preventNullPointerException. Optional is a simple container for a value which may be null or non-null. Think of a method which may return a non-null result but sometimes return nothing.Instead of returning null you return an Optional in Java 8.
34
34 sample
35
35 Streams
36
36 streams A java.util.Stream represents a sequence of elements on which one or more operations can be performed Stream operations are either intermediate or terminal. terminal operations return a result of a certain type intermediate operations return the stream itself so you can chain multiple method calls in a row Stream operations can either be executed sequential or parallel.
37
37 sample
38
38
39
39 Parallel stream Streams can be either sequential or parallel. Operations on sequential streams are performed on a single thread while operations on parallel streams are performed concurrent on multiple threads.
40
40 sample
41
41 end
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.