Java Technologies. Lecture IV. Valdas Rapševičius

Size: px
Start display at page:

Download "Java Technologies. Lecture IV. Valdas Rapševičius"

Transcription

1 Preparation of the material was supported by the project Increasing Internationality in Study Programs of the Department of Computer Science II, project number VP1 2.2 ŠMM-07-K , funded by The European Social Fund Agency and the Government of Lithuania. Java Technologies Lecture IV Valdas Rapševičius Vilnius University Faculty of Mathematics and Informatics

2 Session Outline Java 8 feature Lambda variations towards java.util.function interfaces Typed Generic Other Lambda scope Effectively final Method references Valdas Rapševičius. Java Technologies 2

3 Functional Programming Structure computer programs by evaluation of mathematical functions and avoids state and mutable data Emphasizes functions that produce results and depend only on their inputs and not on the program Programming is done with expressions In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Principles Avoiding Mutable State Functions as First-Class Values Higher-Order Functions Declarative Style Valdas Rapševičius. Java Technologies 3

4 Lambda expressions A new and important feature included in Java SE 8 Lambda comes from the use of the Greek lambda symbol λ to represent functions in lambda calculus A clear and concise way to represent one method interface using an expression Valdas Rapševičius. Java Technologies 4

5 Anonymous Class Definition static <T> void Arrays.sort(T[] array, Comparator<T> comp); public interface Comparator<T> { int compare(t arg1, T arg2); Implementation Class class ByNameLengthComparator implements Comparator<Person> { public int compare(person p1, Person p2) { return p1.getname().length() - p2.getname().length(); Arrays.sort(arr, new ByNameLengthComparator()); Annonymous class Arrays.sort(arr, new Comparator<Person>() { public int compare(person p1, Person p2) { return p1.getage() - p2.getage(); ); Valdas Rapševičius. Java Technologies 5

6 Lambda Expression 01 Monstrous anonymous class new Comparator<Person>() public int compare(person p1, Person p2) { return p1.getage() - p2.getage(); Replace with simple (lambda) function (Person p1, Person p2) -> { return p1.getage() - p2.getage(); Valdas Rapševičius. Java Technologies 6

7 Lambda Example 01 Arrays.sort(arr, (Person p1, Person p2) -> { return p2.getage() - p1.getage(); ); Arrays.sort(arr, (Person p1, Person p2) -> { return p2.getname().length() - p1.getname().length(); ); Arrays.sort(arr, (Person p1, Person p2) -> { ); int diff = p1.getname().length() - p2.getname().length(); if (diff == 0) { diff = p1.getage() - p2.getage() return diff; Valdas Rapševičius. Java Technologies 7

8 Lambda Expression 02 Monstrous anonymous class new Comparator<Person>() public int compare(person p1, Person p2) { return p1.getage() - p2.getage(); Replace with simple (lambda) function (p1,p2) -> { return p1.getage() - p2.getage(); Valdas Rapševičius. Java Technologies 8

9 Lambda Example 02 Arrays.sort(arr, (p1, p2) -> { return p2.getage() - p1.getage(); ); Arrays.sort(arr, (p1, p2) -> { return p2.getname().length() - p1.getname().length(); ); Arrays.sort(arr, (p1, p2) -> { ); int diff = p1.getname().length() - p2.getname().length(); if (diff == 0) { diff = p1.getage() - p2.getage() return diff; Valdas Rapševičius. Java Technologies 9

10 Lambda Expression 03 Monstrous anonymous class new Comparator<Person>() public int compare(person p1, Person p2) { return p1.getage() - p2.getage(); Replace with simple (lambda) function (p1,p2) -> p1.getage() - p2.getage() Valdas Rapševičius. Java Technologies 10

11 Lambda Example 03 Arrays.sort(arr, (p1, p2) -> p2.getage() - p1.getage() ); Arrays.sort(arr, (p1, p2) -> p2.getname().length() - p1.getname().length()); Arrays.sort(arr, (p1, p2) -> { ); int diff = p1.getname().length() - p2.getname().length(); if (diff == 0) { diff = p1.getage() - p2.getage() return diff; Valdas Rapševičius. Java Technologies 11

12 More Lambda Examples 03 // Pre Java 8 button.addactionlistener(new ActionListener() public void actionperformed(actionevent e) { button.setbackground(color.green); ); button.addactionlistener(new ActionListener() public void actionperformed(actionevent e) { JOptionPane.showMessageDialog(button, e.getactioncommand()); ); button.addactionlistener(new ActionListener() public void actionperformed(actionevent e) { System.out.println(this.getClass().getCanonicalName()); ); // Java 8! button.addactionlistener(e -> button.setbackground(color.red)); button.addactionlistener(e -> JOptionPane.showMessageDialog(button, e.getactioncommand())); button.addactionlistener(e -> System.out.println(this.getClass().getCanonicalName())); Valdas Rapševičius. Java Technologies 12

13 @FunctionalInterface Any single method of the functional interface can be used with lambdas One-method interfaces Functional interfaces Single Abstract Method (SAM) interfaces Declaration is NOT mandatory but recommended (just Compilation error if you add more than one abstract method Declares designers intent for lambdas Declaration of Object methods are public interface SimpleFuncInterface { public void dowork(); Valdas Rapševičius. Java Technologies 13

14 @FunctionalInterface Example Declare your intent for the interface MyFunction { double calc(double x); Declare a method with parameter private static double sumof(myfunction f, double a, double b, double step) { double sum = 0d; for (double i = a; i < b; i += step) { sum += f.calc(i); return sum; Usage double s1 = sumof(x -> x * x, 0d, 10d, 0.1d); double s2 = sumof(x -> 10 / x, -10d, 10d, 1.0d); double s3 = sumof(x -> { return 2 * x;, 1d, 100d, 2.0d); double s4 = sumof(x -> Math.PI * x, 0d, 10d, 0.1d); double s5 = sumof(x -> Math.cos(x), 0d, 10d, 0.1d); Valdas Rapševičius. Java Technologies 14

15 java.util.function Interfaces Provides a backbone for lambdas All-purpose Reusable Types Typed interfaces Takes parameters and/or return values as primitive types Generic interfaces Takes parameters and/or return values as generic objects Valdas Rapševičius. Java Technologies 15

16 Typed java.util.function Interfaces Typed unary operators: one in, one out Represents an operation on a single type-valued operand that produces a same type-valued result. DoubleUnaryOperator, IntUnaryOperator, LongUnaryOperator Typed binary operators: two in, one out Represents an operation upon two type-valued operands and producing a type-valued result. DoubleBinaryOperator, IntBinaryOperator, LongBinaryOperator Typed predicates: one in, boolean out Represents a predicate (boolean-valued function) of one type-valued argument. DoublePredicate, IntPredicate, LongPredicate Typed consumers: one in, void out Represents an operation that accepts a single type-valued argument and returns no result. DoubleConsumer, IntConsumer, LongConsumer Valdas Rapševičius. Java Technologies 16

17 Typed Interface Example Method declaration private static double sumof(doubleunaryoperator f, double a, double b, double step) { double sum = 0d; for (double i = a; i < b; i += step) { sum += f.calc(i); return sum; Usage (the same as in previous example slide!) double s1 = sumof(x -> x * x, 0d, 10d, 0.1d); double s2 = sumof(x -> 10 / x, -10d, 10d, 1.0d); double s3 = sumof(x -> { return 2 * x;, 1d, 100d, 2.0d); double s4 = sumof(x -> Math.PI * x, 0d, 10d, 0.1d); double s5 = sumof(x -> Math.cos(x), 0d, 10d, 0.1d); Valdas Rapševičius. Java Technologies 17

18 Typed Functional Interfaces in API Valdas Rapševičius. Java Technologies 18

19 Generic java.util.function Interfaces Predicate<T>: T in, boolean out Represents a predicate (boolean-valued function) of one argument. This is a functional interface whose functional method is test(object). Function<T,R>: T in, R out Represents a function that accepts one argument and produces a result. This is a functional interface whose functional method is apply(object). Consumer<T>: T in, nothing (void) out Represents an operation that accepts a single input argument and returns no result. This is a functional interface whose functional method is accept(object). Supplier<T>: nothing in, T out Represents a supplier of results. There is no requirement that a new or distinct result be returned each time the supplier is invoked. This is a functional interface whose functional method is get(). BinaryOperator<T>: two T s in, T out Represents an operation upon two operands of the same type, producing a result of the same type as the operands. This is a functional interface whose functional method is BiFunction.apply(Object, Object) Valdas Rapševičius. Java Technologies 19

20 Generic Interface Example Declaration private static <T> T findfirst(collection<t> collection, Predicate<T> pred) { for (T t: collection) { if (pred.test(t)) { return t; return null; Usage Person p1 = findfirstperson(persons, p -> p.getage() == 23); Person p2 = findfirst(persons, p -> p.getname().equalsignorecase("john")); Person p3 = findfirst(persons, p.getage() < 40 && p.getname().equals("john")); Valdas Rapševičius. Java Technologies 20

21 Other java.util.function Interfaces public interface BiConsumer<T,U> Represents an operation that accepts two input arguments and returns no result. This is the two-arity specialization of Consumer. public interface ToDoubleFunction<T> Represents a function that produces a double-valued result. public interface LongToDoubleFunction Represents a function that accepts a long-valued argument and produces a double-valued result. public interface ObjIntConsumer<T> Represents an operation that accepts an object-valued and a intvalued argument, and returns no result. (see java.util.functions for others, more info) Valdas Rapševičius. Java Technologies 21

22 Lambda Scope No new scope introduced! In contrast to anonymous class, lambda scope is on this class. // this variable Consumer<Person> c1 = (p) -> this.process(p); // Legal to access variables StringBuilder sb = new StringBuilder(); Consumer<Person> c2 = (p) -> sb.append(p.getname()); // Not legal to have same names Person p = new Person(); String s = ""; Consumer<Person> c3 = (p) -> { String s = ""; ); Valdas Rapševičius. Java Technologies 22

23 Effectively final To access outer variable it must be effectively final. // this variable Consumer<Person> c1 = (p) -> this.process(p); Consumer<Person> c2 = (p) -> this.name = p.getname(); // Legal to access effectively final variables StringBuilder sb = new StringBuilder(); Consumer<Person> c3 = (p) -> sb.append(p.getname()); // Legal to modify instance variables private boolean x = true; Consumer<Person> c4 = (p) -> x = p.ismanager(); // Not legal to modify local variable String s = ""; Consumer<Person> c3 = (p) -> { s = p.getname(); ; Valdas Rapševičius. Java Technologies 23

24 Method References For methods whose signature matches the signature of the method of a functional (SAM) interface you can use Class::method (or similar), rather than an explicit lambda MyClass::staticMethod Math::cos // x -> Math.cos(x) myvariable::instancemethod s::touppercase // () -> s.touppercase() MyClass::instanceMethod String::toUpperCase // s -> s.touppercase() MyClass::new Person::new // () -> new Person() Valdas Rapševičius. Java Technologies 24

25 Method References Examples Static class method Function<Double, Double> cos = Math::cos; // (x) -> Math.cos(x) Variable instance method String s = "Alma Matter Vilnensis"; Supplier<String> upper01 = s::touppercase; // (x) -> x.touppercase() Parameter instance method Function<String, String> upper02 = String::toUpperCase; // (x) -> x.touppercase() Constructor Supplier<String> str01 = String::new; // () -> new String() Function<String, String> str02 = String::new; // (x) -> new String(x) Valdas Rapševičius. Java Technologies 25

26 Session Conclusions Lambda expressions are much more concise than anonymous classes p -> p.getgender() == Person.Sex.MALE && p.getage() >= 18 to declare interfaces for lambda expressions java.util.function package provides a lot of useful reusable interfaces for your needs Learn these interfaces BEFORE creating yours Study the interface if using lambda with Java API Lambda do not introduce new scope Can access effectively final variable and modify its properties Use method references instead of simple lambdas Valdas Rapševičius. Java Technologies 26

Josh Bloch Charlie Garrod Darya Melicher

Josh Bloch Charlie Garrod Darya Melicher Principles of Software Construction: Objects, Design, and Concurrency Lambdas and streams Josh Bloch Charlie Garrod Darya Melicher 1 Administrivia Homework 5 Best Frameworks available tonight Or early

More information

Free your Lambdas Java SE 8

Free your Lambdas Java SE 8 Free your Lambdas Java SE 8 Agenda Tutorial session: we will start at the very beginning! and explore how to build functional interfaces to design new APIs This is about lambdas and functional interfaces

More information

Java Technologies. Lecture V. Valdas Rapševičius

Java Technologies. Lecture V. Valdas Rapševičius Preparation of the material was supported by the project Increasing Internationality in Study Programs of the Department of Computer Science II, project number VP1 2.2 ŠMM-07-K-02-070, funded by The European

More information

LAMBDA EXPRESSIONS AND STREAMS API

LAMBDA EXPRESSIONS AND STREAMS API Java 8 LAMBDA EXPRESSIONS AND STREAMS API An Introduction Methods As Data 2 @FunctionalInterface public interface Runnable { public abstract void run(); public interface ActionListener extends EventListener

More information

LAMBDA EXPRESSIONS. Summer 2018

LAMBDA EXPRESSIONS. Summer 2018 LAMBDA EXPRESSIONS Summer 2018 LAMBDA EXPRESSIONS USES Introduced in Java SE 8, lambda expressions are a way to create single-method classes in your code in a much less cumbersome manner than anonymous

More information

Lambdas in Java 8. Start programming in a more functional style

Lambdas in Java 8. Start programming in a more functional style Lambdas in Java 8 Start programming in a more functional style Background Who am I? Tobias Coetzee I m a Technical Lead at BBD I present the Java Expert Level Certifications at BBD (EJB, JPA, etc.) I m

More information

Charlie Garrod Michael Hilton

Charlie Garrod Michael Hilton Principles of So3ware Construc9on: Objects, Design, and Concurrency Part 6: Et cetera Java lambdas and streams Charlie Garrod Michael Hilton School of Computer Science 1 Administrivia Homework 5 Best Frameworks

More information

New Features in Java 8

New Features in Java 8 New Features in Java 8 Lambda expressions Functional interfaces Streaming support for Collections Lambda expressions Are a block of java code with parameters Can be assigned to variables Can be executed

More information

Fontys Hogeschool voor Techniek en Logistiek. March 13, 2018

Fontys Hogeschool voor Techniek en Logistiek. March 13, 2018 Java 8 s and Java 8 Fontys Hogeschool voor Techniek en Logistiek March 13, 2018 and? /FHTenL s and Java 8 March 13, 2018 1/34 talk The other anonymous and? Java 8 and? /FHTenL s and Java 8 March 13, 2018

More information

Peter Sestoft. Java Precisely. Third Edition. The MIT Press Cambridge, Massachusetts London, England

Peter Sestoft. Java Precisely. Third Edition. The MIT Press Cambridge, Massachusetts London, England Peter Sestoft Java Precisely Third Edition The MIT Press Cambridge, Massachusetts London, England Contents Preface Notational Conventions xi xii 1 Running Java: Compilation, Loading, and Execution 2 2

More information

Advanced Programming Methods. Lecture 4 - Functional Programming in Java

Advanced Programming Methods. Lecture 4 - Functional Programming in Java Advanced Programming Methods Lecture 4 - Functional Programming in Java Important Announcement: At Seminar 6 (7-13 November 2017) you will have a closed-book test (based on your laboratory work). Overview

More information

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming Ernest Ryu UCLA Mathematics Last edited: December 8, 2017 Introductory example When you write an ActionListener for a GUI, you

More information

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Copyright 2012, Oracle and/or its affiliates. All rights reserved. 1 Jump-Starting Lambda Stuart Marks @stuartmarks Mike Duigou @mjduigou Oracle JDK Core Libraries Team 2 What is Lambda? Essentially an anonymous function allows one to treat code as data provides parameterization

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring

More information

Lambdas & Streams In JDK 8: Beyond The Basics

Lambdas & Streams In JDK 8: Beyond The Basics Lambdas & Streams In JDK 8: Beyond The Basics Simon Ritter Deputy CTO, Azul Systems @speakjava azul.com Copyright Azul Systems 2015 1 A clever man learns from his mistakes......a wise man learns from other

More information

Using Lambdas to Write Mixins in Java 8

Using Lambdas to Write Mixins in Java 8 Using Lambdas to Write Mixins in Java 8 Dr Heinz M. Kabutz heinz@javaspecialists.eu Last updated 2014-11-12 2014 Heinz Kabutz All Rights Reserved Copyright Notice l 2014 Heinz Kabutz, All Rights Reserved

More information

Quick start. Robert Bachmann & Dominik Dorn. JSUG Meeting #63

Quick start. Robert Bachmann & Dominik Dorn. JSUG Meeting #63 1.. Java 8 Quick start Robert Bachmann & Dominik Dorn JSUG Meeting #63 Outline: What s new in Java 8 2 Interface additions and lambda syntax (r) Library additions (r) Nashorn (d) Type annotations (d) VM

More information

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology Computer Science II OO Programming Classes Scott C Johnson Rochester Institute of Technology Outline Object-Oriented (OO) Programming Review Initial Implementation Constructors Other Standard Behaviors

More information

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Copyright 2014, Oracle and/or its affiliates. All rights reserved. 1 Introduction to Lambda Stuart W. Marks Principal Member of Technical Staff Oracle JDK Core Libraries Team Twitter: @stuartmarks What is a Lambda? A lambda is a function. A function is a computation that

More information

C30b: Inner Class, Anonymous Class, and Lambda Expression

C30b: Inner Class, Anonymous Class, and Lambda Expression CISC 3115 TY3 C30b: Inner Class, Anonymous Class, and Lambda Expression Hui Chen Department of Computer & Information Science CUNY Brooklyn College 12/6/2018 CUNY Brooklyn College 1 Outline Discussed Concept

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 15/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 30 Java 8! Lambdas and streams in Java 8 1 Java 8:

More information

Java SE 8: Lambda Expressions And The Stream API

Java SE 8: Lambda Expressions And The Stream API Java SE 8: Lambda Expressions And The Stream API Simon Ritter Head of Java Technology Evangelism Java Product Management Java Day Tokyo 2015 April 8, 2015 Safe Harbor Statement The following is intended

More information

Binghamton University. CS-140 Fall Functional Java

Binghamton University. CS-140 Fall Functional Java Functional Java 1 First Class Data We have learned how to manipulate data with programs We can pass data to methods via arguments We can return data from methods via return types We can encapsulate data

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Introduction to Functional Programming in Java 8

Introduction to Functional Programming in Java 8 1 Introduction to Functional Programming in Java 8 Java 8 is the current version of Java that was released in March, 2014. While there are many new features in Java 8, the core addition is functional programming

More information

INHERITANCE. Spring 2019

INHERITANCE. Spring 2019 INHERITANCE Spring 2019 INHERITANCE BASICS Inheritance is a technique that allows one class to be derived from another A derived class inherits all of the data and methods from the original class Suppose

More information

Java 8 new features Juan Hernández

Java 8 new features Juan Hernández Java 8 new features Juan Hernández Dec 1st 2015 1 / 73 Introduction In this session we will do an introduction to the new features introduced by Java 8 for functional programming: λ-expressions and the

More information

COMP6700/2140 Code as Data

COMP6700/2140 Code as Data COMP6700/2140 Code as Data Alexei B Khorev Research School of Computer Science, ANU March 2017 Alexei B Khorev (RSCS, ANU) COMP6700/2140 Code as Data March 2017 1 / 19 Topics 1 What does treating code

More information

Introducing Scala-like function types into Java-TX

Introducing Scala-like function types into Java-TX Introducing Scala-like function types into Java-TX ManLang 2017 Martin Plümicke Andreas Stadelmeier www.dhbw-stuttgart.de/horb Overview 1 Type of lambda expressions in Java-8 2 Introducing real function

More information

Overview of Java 8 Functional Interfaces

Overview of Java 8 Functional Interfaces Overview of Java 8 Functional Interfaces Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University

More information

Lecture 10 Declarations and Scope

Lecture 10 Declarations and Scope Lecture 10 Declarations and Scope Declarations and Scope We have seen numerous qualifiers when defining methods and variables public private static final (we'll talk about protected when formally addressing

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Lambda Expressions and Java 8 Streams. Jan Trienes, adapted by Th. Dorssers, Pieter van den Hombergh. Contents of this talk.

Lambda Expressions and Java 8 Streams. Jan Trienes, adapted by Th. Dorssers, Pieter van den Hombergh. Contents of this talk. Java 8 s and Java 8 van den Hombergh Fontys Hogeschool voor Techniek en Logistiek February 23, 2017 and /FHTenL s and Java 8 February 23, 2017 1/28 talk Expression and Internal/External Iteration Java

More information

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs. Java SE11 Development Java is the most widely-used development language in the world today. It allows programmers to create objects that can interact with other objects to solve a problem. Explore Java

More information

Lambda Notes for CS 2102

Lambda Notes for CS 2102 Lambda Notes for CS 2102 Remember filter and map from CS1101/1102: ;; filter: (X- >Boolean) ListOfX - > ListOfX the function argument (X- > Boolean) is a predicate. Filter applies the predicate to each

More information

<Insert Picture Here> Project Lambda: To Multicore and Beyond

<Insert Picture Here> Project Lambda: To Multicore and Beyond Project Lambda: To Multicore and Beyond Brian Goetz Java Language Architect, Oracle Corporation The following is intended to outline our general product direction. It is intended

More information

Collections After Eight. Maurice Naftalin Morningside Light

Collections After Eight. Maurice Naftalin Morningside Light Collections After Eight Maurice Naftalin Morningside Light Ltd. @mauricenaftalin Maurice Naftalin Developer, designer, architect, teacher, learner, writer Co-author www.lambdafaq.org Current Projects Why

More information

Ausblick auf Java 8. Martin Plümicke. 25. Mai Baden-Wuerttemberg Cooperative State University Stuttgart/Horb

Ausblick auf Java 8. Martin Plümicke. 25. Mai Baden-Wuerttemberg Cooperative State University Stuttgart/Horb Ausblick auf Java 8 Martin Plümicke Baden-Wuerttemberg Cooperative State University Stuttgart/Horb 25. Mai 2012 Overview Introduction Introduction Closures Java s motivation λ expressions Functional interfaces

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-15: Recursion,

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 35 November 28, 2018 Swing II: Inner Classes and Layout Chapter 30 Announcements Game Project Complete Code Due: December 10 th NO LATE SUBMISSIONS

More information

http://xkcd.com/378/ Quicksort in Rust solution (in class) CS 152: Programming Language Paradigms Returning to Java Prof. Tom Austin San José State University Returning home to Java It's the last day of

More information

Conversions and Overloading : Overloading

Conversions and Overloading : Overloading Conversions and Overloading : First. Java allows certain implicit conversations of a value of one type to a value of another type. Implicit conversations involve only the primitive types. For example,

More information

ENCAPSULATION AND POLYMORPHISM

ENCAPSULATION AND POLYMORPHISM MODULE 3 ENCAPSULATION AND POLYMORPHISM Objectives > After completing this lesson, you should be able to do the following: Use encapsulation in Java class design Model business problems using Java classes

More information

Declarations and Access Control SCJP tips

Declarations and Access Control  SCJP tips Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for

More information

Interfaces. An interface forms a contract between the object and the outside world.

Interfaces. An interface forms a contract between the object and the outside world. Interfaces An interface forms a contract between the object and the outside world. For example, the buttons on the television set are the interface between you and the electrical wiring on the other side

More information

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator. .00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

More information

Java gets a closure. Tomasz Kowalczewski

Java gets a closure. Tomasz Kowalczewski Java gets a closure Tomasz Kowalczewski Agenda Lambdas and closures Java syntax Language interaction Implementation Interface evolution Library changes Lambda expression First class function that closes

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Notable Enhancements in Java 8. Functional Programming with Java. Lambda Expressions in Java. How to Define a Lambda Expression? Lambda expressions

Notable Enhancements in Java 8. Functional Programming with Java. Lambda Expressions in Java. How to Define a Lambda Expression? Lambda expressions Notable Enhancements in Java 8 Lambda expressions Allow you to do functional programming in Java Functional Programming with Java Static and default methods in interfaces 1 2 Lambda Expressions in Java

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

Event Handling Java 7

Event Handling Java 7 Event Handling Java 7 Waterford Institute of Technology September 25, 2014 John Fitzgerald Waterford Institute of Technology, Event Handling Java 7 1/24 Inheritance Inheritance v Interface Inheritance

More information

Advanced Object Oriented Programming EECS2030Z

Advanced Object Oriented Programming EECS2030Z Advanced Object Oriented Programming EECS2030Z 1 Academic Support Programs: Bethune having trouble with your FSC and LSE courses? consider using the Academic Support Programs at Bethune College PASS free,

More information

Object-Oriented Concepts

Object-Oriented Concepts JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing

More information

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited. final A final variable is a variable which can be initialized once and cannot be changed later. The compiler makes sure that you can do it only once. A final variable is often declared with static keyword

More information

Advanced Java Programming

Advanced Java Programming Advanced Java Programming Programming Technologies 2015/2016 spring Kollár, Lajos Kocsis, Gergely (English version) Advanced Java Programming Java 5 Generics (Enums) Java 7 Strings in switch try-with-resources

More information

Lambda Expressions In JDK8: Going Beyond The Basics

Lambda Expressions In JDK8: Going Beyond The Basics Lambda Expressions In JDK8: Going Beyond The Basics Simon Ri?er Head of Java Technology Evangelism Oracle Corp Twi?er: @speakjava Copyright 2014, Oracle and/or its affiliates. All rights reserved. Safe Harbor

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2 CS321 Languages and Compiler Design I Winter 2012 Lecture 2 1 A (RE-)INTRODUCTION TO JAVA FOR C++/C PROGRAMMERS Why Java? Developed by Sun Microsystems (now Oracle) beginning in 1995. Conceived as a better,

More information

Lambda Calculus see notes on Lambda Calculus

Lambda Calculus see notes on Lambda Calculus Lambda Calculus see notes on Lambda Calculus Shakil M. Khan adapted from Gunnar Gotshalks recap so far: Lisp data structures basic Lisp programming bound/free variables, scope of variables Lisp symbols,

More information

Part 3. Why do we need both of them? The object-oriented programming paradigm (OOP) Two kinds of object. Important Special Kinds of Member Function

Part 3. Why do we need both of them? The object-oriented programming paradigm (OOP) Two kinds of object. Important Special Kinds of Member Function Part 3 The object-oriented programming paradigm (OOP) Two kinds of object Value objects The object contains the member data items Allocated automatically just like primitive (built-in) data items Suitable

More information

15CS45 : OBJECT ORIENTED CONCEPTS

15CS45 : OBJECT ORIENTED CONCEPTS 15CS45 : OBJECT ORIENTED CONCEPTS QUESTION BANK: What do you know about Java? What are the supported platforms by Java Programming Language? List any five features of Java? Why is Java Architectural Neutral?

More information

Utilities (Part 2) Implementing static features

Utilities (Part 2) Implementing static features Utilities (Part 2) Implementing static features 1 Goals for Today learn about preventing class instantiation learn about methods static methods method header method signature method return type method

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

More information

Java Workshop Lambda Expressions

Java Workshop Lambda Expressions Java Workshop Lambda Expressions AP Java Workshop 2015 Hanno Hüther and Martin Stein Agenda 1. Origin and syntax 2. History and motivation 3. Exercise 1: Refactoring to lambdas 4. Method references 5.

More information

C11: Garbage Collection and Constructors

C11: Garbage Collection and Constructors CISC 3120 C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017 CUNY Brooklyn College 1 Outline Recap Project progress and lessons

More information

Outline. Command Pattern. Examples. Goal & Applications Motivation (undo / redo) Structure & participants Sequence diagram

Outline. Command Pattern. Examples. Goal & Applications Motivation (undo / redo) Structure & participants Sequence diagram Outline Command Pattern Goal & Applications Motivation (undo / redo) Structure & participants Sequence diagram Examples javax.swing.action java.util.timer java.util.concurrent.executorservice Callable

More information

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has

More information

Interfaces (1/2) An interface forms a contract between the object and the outside world.

Interfaces (1/2) An interface forms a contract between the object and the outside world. Interfaces (1/2) An interface forms a contract between the object and the outside world. For example, the buttons on remote controls for some machine. As you can see, an interface is a reference type,

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

Lecture 16: Static Semantics Overview 1

Lecture 16: Static Semantics Overview 1 Lecture 16: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 5 Anatomy of a Class Outline Problem: How do I build and use a class? Need to understand constructors A few more tools to add to our toolbox Formatting

More information

Binghamton University. CS-140 Fall Functional Java

Binghamton University. CS-140 Fall Functional Java Functional Java 1 First Class Data We have learned how to manipulate data with programs We can pass data to methods via arguments We can return data from methods via return types We can encapsulate data

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 35 November 29, 2017 Swing II: Building GUIs Inner Classes Chapter 29 Announcements Game Project Complete Code Due: December 11 th NO LATE SUBMISSIONS

More information

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing

More information

EINDHOVEN UNIVERSITY OF TECHNOLOGY

EINDHOVEN UNIVERSITY OF TECHNOLOGY EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics & Computer Science Exam Programming Methods, 2IP15, Wednesday 17 April 2013, 09:00 12:00 TU/e THIS IS THE EXAMINER S COPY WITH (POSSIBLY INCOMPLETE)

More information

Lambda Calculus LC-1

Lambda Calculus LC-1 Lambda Calculus LC-1 λ- Calculus History-1 Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of λ-calculus is equivalent in power

More information

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 34 April 11, 2016 Swing II: Inner Classes and Layout Announcements (Review) Final exam: May 2, 3-5PM If you have two finals at the same time, you can

More information

UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

More information

Lecture Topics. Administrivia

Lecture Topics. Administrivia ECE498SL Lec. Notes L8PA Lecture Topics overloading pitfalls of overloading & conversions matching an overloaded call miscellany new & delete variable declarations extensibility: philosophy vs. reality

More information

Constructors for classes

Constructors for classes Constructors for Comp Sci 1570 Introduction to C++ Outline 1 2 3 4 5 6 7 C++ supports several basic ways to initialize i n t nvalue ; // d e c l a r e but not d e f i n e nvalue = 5 ; // a s s i g n i

More information

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and Type-Checking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture:

More information

User-defined Functions. Conditional Expressions in Scheme

User-defined Functions. Conditional Expressions in Scheme User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

More information

COMP 110/L Lecture 6. Kyle Dewey

COMP 110/L Lecture 6. Kyle Dewey COMP 110/L Lecture 6 Kyle Dewey Outline Methods Variable scope Call-by-value Testing with JUnit Variable Scope Question Does this compile? public class Test { public static void main(string[] args) { int

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos. Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Monad Background (3A) Young Won Lim 11/20/17

Monad Background (3A) Young Won Lim 11/20/17 Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Evolving Java. Brian Goetz Java Language Architect, Oracle. Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Evolving Java. Brian Goetz Java Language Architect, Oracle. Copyright 2013, Oracle and/or its affiliates. All rights reserved. Evolving Java Brian Goetz Java Language Architect, Oracle 1 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

More information

Fundamental Java Methods

Fundamental Java Methods Object-oriented Programming Fundamental Java Methods Fundamental Java Methods These methods are frequently needed in Java classes. You can find a discussion of each one in Java textbooks, such as Big Java.

More information

Class 9: Static Methods and Data Members

Class 9: Static Methods and Data Members Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward Goals This the session in which we explain what static means. You

More information

INSTRUCTIONS TO CANDIDATES

INSTRUCTIONS TO CANDIDATES NATIONAL UNIVERSITY OF SINGAPORE SCHOOL OF COMPUTING FINAL ASSESSMENT FOR Semester 1 AY2017/2018 CS2030 Programming Methodology II November 2017 Time Allowed 2 Hours INSTRUCTIONS TO CANDIDATES 1. This

More information