Generics, Lambdas, Reflections

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

Road Map Introduction to object oriented programming. Classes
Java Generics.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
Abstract Classes and Interfaces
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Types in programming languages1 What are types, and why do we need them?
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Classes, Interfaces and Packages
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Default Methods Anonymous Classes Generic Methods Lambdas Design by Contract JUnit.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
CMSC 202 ArrayList Aug 9, 2007.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Java Programming Language
Java Generics.
Chapter 20 Generic Classes and Methods
Methods Attributes Method Modifiers ‘static’
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
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Generics.
Chapter 9 Inheritance and Polymorphism
CSC 113 Tutorial QUIZ I.
Chapter 19 Generics Dr. Clincy - Lecture.
CMSC 202 Interfaces.
Java Programming Language
Lecture 26: Advanced List Implementation
CMSC 202 ArrayList Aug 9, 2007.
Generic programming in Java
CISC124 Assignment 4 on Inheritance due today at 7pm.
CMSC 202 ArrayList Aug 9, 2007.
Java Inheritance.
Type Safety, Generics, Lambdas, Class Object
CMSC 202 Interfaces.
Class.
Review: libraries and packages
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 19 Generics.
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Interfaces.
Generics, Lambdas and Reflection
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Generics, Lambdas, Reflections Lecture 6 Advanced Java Generics, Lambdas, Reflections

Type Safety in Java Type safety: Java is a statically typed language. Every variable must have a type at compile time Type safety: Making sure the object referenced by a variable is of a type compatible with the type of the variable Object type can be one of three options: The type of the object is equal to the type of its variable Person person = new Person(); The type of the object extends the type of its variable Person person = new Man(); //where Person is a Class The type of the object implements the type of its variable Person person = new Man(); //where Person is an Interface In Java, the compiler verifies type safety for us, and throws errors during compilation as needed

Type safety But sometimes the language constructs weakens the type information, to enable more generic code. Example:

Type safety Example: The get() and add()methods of List work with Object (a super class of every class). The type of objects in the List is not known at compile time to enable the reusability of List. We cannot know if all the types in the List are equal.

Generics Similar to C++’s templates, but simpler. Like in C++ Java's generics can be defined on methods, classes and interfaces. Code example: 1 2 3 4 5 6 public class Pair<X, Y> { private X x, private Y y; public Pair(X x, Y y){ this.x = x; this.y = y; } public X getX(){ return x;} }

Generics and Primitives Generics in Java do not allow the use of primitives! ArrayList<int> intArrayList; //compilation error! ArrayList<Integer> integerArrayList; //works fine! Solution: autoboxing and unboxing A convenient way to auto transform primitive data type to it’s corresponding java wrapper classes and vice versa Helpful in reducing code size – no need to convert primitive type to object type explicitly!

Java Wrapper Classes Each primitive type in Java has a corresponding wrapper class type Wrapper classes are objects that wrap the primitive type Wrapper Classes vs Primitive Types: Primitive types cannot be null while wrapper classes can be null Wrapper classes can be used in generics, primitive types cannot

Autoboxing and Unboxing Autoboxing is done on primitive value in these cases: Passed as a parameter to a method that expects an object of the corresponding wrapper class Example: a method with Integer argument can be called by passing int, Java compiler will do the conversion of int to Integer. Assigned to a variable of the corresponding wrapper class Example, assigning a long variable to a Long object Unboxing is done on wrapper classes in these cases: Passed as a parameter to a method that expects a value of the corresponding primitive type Example: a method with int argument can be called by passing Integer, Java compiler will do the conversion of Integer to int. Assigned to a variable of the corresponding primitive type Example, assigning a Long object to a long primitive

Autoboxing and Unboxing - Examples In this autoboxing example, the added values are of primitive type int, while the list contains objects of type Integer: In this unboxing example, the values retrieved from the container are of an Integer object, while the summed up result of a primitive type of int: 1 2 3 List<Integer> li = new ArrayList<>(); for (int i = 1; i < 50; i += 2) li.add(i); 1 2 3 4 5 6 7 public static int sumEven(List<Integer> li) { int sum = 0; for (Integer i: li) if (i % 2 == 0) sum += i; return sum; } How is this done? While compiling the compiler converts: Autoboxing example: line 3 to li.add(Integer.valueOf(i)); Unboxing example: line 5 to sum += i.intValue();

Generic Interfaces – Java 5 Generic interface are exactly the same as generic classes Syntax Example: public interface Person<T, E> { public Person(T age, E weight); public T getAge(); public E getWeight(); }

Generic Method

Some comments A generic type argument can have a lower bound type defined using the extend word. Comparable is an interface which defines a single method int compareTo(T arg) All java's wrapper types implements this interface. 

Generic Methods – Java 8+ public class Person { private int age; private int weight; public Person(int age, int weight) { this.age = age; this.weight = weight; } public int getAge() { return age; public int getWeight() { return weight; Given an array of Person objects what is the maximal object? Is it the heaviest Person? Maybe the oldest Person? It depends on how you want to compare the Person!

Generic Methods Implementation public static <T> T max(T[] array, Comparator<T> comparator) { if (array.length == 0) throw new IllegalArgumentException("empty array"); int maxIndex=0; for (int i=1; i<array.length; i++) if (comparator.compare(array[maxIndex], array[i]) < 0) maxIndex = i; return array[maxIndex]; } Note the <T> in front of the method signature which parametrize the method.

Using Generic Methods public static class PersonComparatorByAge implements Comparator<Person> { public int compare(Person o1, Person o2) { return o1.getAge() - o2.getAge(); } public static class PersonComparatorByWeight implements Comparator<Person> { return o1.getWeight() - o2.getWeight(); public static void main(String[] args) { Person[] Persons = {new Person(7,50), new Person(9,200), new Person(3,100)}; System.out.println(max(Persons, new PersonComparatorByWeight ()).getWeight()); // 200 System.out.println(max(Persons, new PersonComparatorByAge()).getAge()); // 9

Generic Limitations Not being able to work on primitive types. Generics are "erased" at runtime: At complie time the compiler knows that List<Integer> is not a List<String>. At run time, both of them looks like List<Object>. This limitation can be seen in the following code: This won’t compile  as the compiler knows that at runtime the defined methods has the same signature.

Generic Limitations More on “type erasure”: You can do: List<String> list; ((List)list).add(new Object()); An error will be thrown only when you get the value of a String. Arrays of generic types cannot be instantiated directly i.e., the following will not compile: But can be worked around this way:  pairs is an array of Pair without any additional generic type information.

Wildcards Pair<Object,Object> is not a super-class of Pair<Integer,Integer>. Pair<?,?> is a super-class of Pair<Integer,Integer>. Bad example: This method accepts only Collection<Object>

Wildcards (more at: https://docs. oracle Good example: This method accepts any Collection However The compiler does not allow to add() because the inner type is unknown at compile time.

Default methods in interface A default method is a method which implemented directly in an interface. It provides default implementation for classes implementing that interface. Example: isEmpty() method in Stack.

Default methods in interface: motivation

Default Methods – Java 8+ Allows us to implement methods directly in the interface! We add default keyword before the access modifier of the method we wish to implement. This implementation will be the default implementation for all classes implementing this interface, and did not override it. Default methods tutorial: https://blog.idrsolutions.com/2015/01/java-8-default-methods-explained-5-minutes/

Default Method - Example interface Stack<E> { void push(E something); E pop(); int size(); boolean isEmpty(); } interface Stack<E> { void push(E something); E pop(); int size(); default boolean isEmpty(){ return (size())==0); } Implementation of isEmpty()wil act as the default implementation for all implementing classes, as long as the class does not overwrite the implementation.

Abstract classes vs. interfaces Another solution: declare an abstract AbstractStack class which implements the Stack interface and the method isEmpty(). BUT: you can only derive from one parent class in Java. This will enforce classes to derive from this class, which can be very constraining. Since in Java a class can implement a number of interfaces - the default method does not enforce such constraints.

Anonymous classes Anonymous classes enable you to make the code shorter. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Anonymous classes (motivation) We had this example before: What if a class is not Comparable or has more than one way to compare objects?

Anonymous classes (motivation)

Anonymous classes (motivation)

Anonymous classes (now code..)

Anonymous classes – motivation for Lambdas If the implementation of your anonymous class is very simple. The code becomes cumbersome (a lot of “wrapping” for a little bit of code), and sometimes unclear. A common example is as an interface that contains only one method. In this case we’re trying to pass functionality as an argument. Just as in our Comparator example. Lambda expressions enable you to do this compactly.

Examples The general syntax is (args...) -> { body } If you just want to return something you can write: (args...) -> ... (int a, int b) -> {  return a + b; }   () -> System.out.println("Hello World"); (String s) -> { System.out.println(s); } () -> 42 () -> { return 3.1415 }; Guide: http://viralpatel.net/blogs/lambda-expressions-java-tutorial/

Anonymous classes – motivation for Lambdas Lambdas example:

Persons Example – Using Lambda public static void main(String[] args) { Person[] Persons = {new Person(7,50), new Person(9,200), new Person(3,100)}; System.out.println(max(Persons, (Person o1, Person o2) -> { return o1.getAge() - o2.getAge(); }).getAge()); return o1.getWeight() - o2.getWeight(); }).getWeight()); } Anonymous Classes: public static void main(String[] args) { Person[] Persons = {new Person(7,50), new Person(9,200), new Person(3,100)}; System.out.println(max(Persons, new Comparator<Person>(){ public int compare(Person o1, Person o2) { return o1.getAge() - o2.getAge(); } }).getAge()); // prints 9 return o1.getWeight() - o2.getWeight(); }).getWeight()); // prints 200 }

Reflection – class object There is a special class called Class. Instances of Class represent classes and interfaces in a running Java application. Class has no public constructor. Instead, Class objects are constructed automatically by the JVM.

Reflection – class object The static class property: for a class A, A.class returns an object of the type Class. The method getClass(): for an object a of type A, a.getClass() returns an object of the type Class. getName() - corresponding class name isAssignableFrom(Class) -  the calling Class represents a superclass or superinterface of the class represented by the given Class object (see example)

 

Another example

Output: