Advanced Programming in Java Java 8 New Features Mehdi Einali 1 agenda Java versions Default method Functional interface Double colon Functional programing Lambda expression Optional Streams 2 Java versions 3 Java versions-1 Version Release Date 1.0 1996 The first stable version 1.1 1997 • 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.3 2000 • Java performance engine Hotspot added • RMI added 2002 • • • • 1.4 Main Features Regular Expression support Exception Handling high-level api added Non-Blocking IO(NIO 1) added Java Web Start 4 Java versions-2 Version 1.5(J2SE 5) Release Date 2004-2009 22 update 2006-2013 1.6(J2SE 6) More than 50 updates Last one 113 2011-now J2SE 7 Many update Last one 101 Main Features • • • • • • Generic Types Annotation Autoboxing Enumorations Varargs Foreach • Performance improvements • Security Improvements • JDBC 4 • • • • String in switch NIO 2 Diamond in generic types Concurrency high level API 5 Java versions-3 Version Java SE 8 Java SE 9 Java SE 10 Release Date 2014-now Last update 92 Main Features • Lambda expression or closure (Functional Programming) • JavaScript embedded emulator • Annotation on Java Type • default method in interface • Better support for multi-gigabyte heaps • Java module system (OSGI like built-in Scheduled system) for March 2017 • Reactive Streams support • Better support for parallel systems Scheduled • Object without Identity for March 2018 • Support 64-bit addressable arrays 6 Default method in interfaces 7 Old interface 8 Default keyword 9 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. 10 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. 11 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 12 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. 13 Functional Programming 14 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 15 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 16 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. 17 compare 18 Functional interface 19 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 20 sample 21 22 Double colon 23 Double colon static method Java 8 enables you to pass references of methods or constructors via the :: keyword 24 Double colon object method 25 Double colon constructor 26 Lambda expression 27 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;} 28 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!") 29 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 30 31 optional 32 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. 33 sample 34 Streams 35 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. 36 sample 37 38 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. 39 sample 40 end 41
© Copyright 2025 Paperzz