Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generics, Lambdas, Reflections

Similar presentations


Presentation on theme: "Generics, Lambdas, Reflections"— Presentation transcript:

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

2 Type safety Java is a statically typed language. Type safety:
Every variable has 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’s type is equal to var’s type or object’s type extends var’s type or object’s type implements var’s interface type Generally, the compiler verifies type safety for us.

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

4 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.

5 Generics Similar to C++’s templates, but simpler.
Like in C++ Java's generics can be defined on methods, classes and interfaces.

6

7 Some comments Integer is an Object type (i.e., a non primitive type) that boxes the primitive int type, There is such wrapper type for each primitive type in java. Unlike C++ templates, generics can only receive Object types (non primitive types) as arguments. It is impossible to create Pair<int, int> and we must use the wrapper types. Java automatically perform boxing and unboxing, but this is a costly operation. Most containers use generics e.g., ArrayList<T>, HashMap<K,V>, etc.

8 Generic Method

9 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. 

10 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.

11 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.

12 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>

13 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.

14 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.

15 Default methods in interface

16 Default methods in interface

17 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.

18 Anonymous classes 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.

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

20 Anonymous classes (motivation)

21 Anonymous classes (motivation)

22 Anonymous classes (now code..)

23 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 wer’re trying to pass functionality as an argument. Just as in our Comparator example. Lambda expressions enable you to do this compactly.

24 Anonymous classes – motivation for Lambdas
Lambdas example: The general syntax is (args...) -> { body } If you just want to return something you can write: (args...) -> ...

25 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.

26 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)

27

28 Another example

29

30 Output:


Download ppt "Generics, Lambdas, Reflections"

Similar presentations


Ads by Google