Functional Programming with Java

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

The Web Warrior Guide to Web Design Technologies
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Chapter 17 Java SE 8 Lambdas and Streams
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Lecturer PEN PHIROM Tel : Web site: Lecturer PEN PHIROM Tel :
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Parts of JAVA 1www.gowreeswar.com. Features of JAVA 2www.gowreeswar.com.
Programming in Java CSCI-2220 Object Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Java8 Released: March 18, Lambda Expressions.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Lecture 5:Interfaces and Abstract Classes
The Object-Oriented Thought Process Chapter 03
Modern Programming Tools And Techniques-I
C# for C++ Programmers 1.
EECE 310: Software Engineering
Object-Oriented Programming: Classes and Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Lecture 30 Revision Lecture 1
Chapter 3: Using Methods, Classes, and Objects
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Generics, Lambdas, Reflections
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Object-Oriented Programming: Classes and Objects
Nested class.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Object Based Programming
Notable Java 8 New Features – Not including Lambda S Marvasti
Chapter 9 Object-Oriented Programming: Inheritance
Chapter 6 Methods: A Deeper Look
Java Programming Language
Conditional Statements
Lecture 22 Inheritance Richard Gesick.
MSIS 655 Advanced Business Applications Programming
Interfaces.
Advanced Programming in Java
Java – Inheritance.
Functional interface.
Java Inheritance.
A few of the more significant changes and additions. James Brucker
Abstract Classes and Interfaces
Computer Science 312 What’s New in Java 8? 1.
Object Oriented Programming in java
Java Programming Language
Final and Abstract Classes
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Review for Midterm 3.
„Lambda expressions, Optional”
Presentation transcript:

Functional Programming with Java Advanced Programming in Java Functional Programming with Java Mehdi Einali

agenda Java versions Default method Functional interface Double colon Functional programing Lambda expression Optional Streams

Java versions

Java versions-1 Version Release Date Main Features 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 1.4 2002 Regular Expression support Exception Handling high-level api added Non-Blocking IO(NIO 1) added Java Web Start

Java versions-2 1.6(J2SE 6) Version Release Date Main Features 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

Java versions-3 Java SE 9 Version Release Date Main 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 10 Scheduled for  March 2018 Object without Identity Support 64-bit addressable arrays 

Default method in interfaces

Old interface

Default keyword

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.

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.

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

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. 

Functional Programming

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

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

collection iteration External iteration Disadvantages: 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.

compare

Functional interface

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

sample

Double colon

Double colon static method Java 8 enables you to pass references of methods or constructors via the :: keyword

Double colon object method

Double colon constructor

Lambda expression

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;}

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!")

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

optional

optional Optionals are not functional interfaces, instead it's a nifty utility to prevent NullPointerException. 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.

sample

Streams

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.

sample

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.

sample

end