Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

This section quickly runs through installing Java SE 8 and Eclipse. It also reviews Java-7 topics that are foundational to lambdas and streams in Java 8: anonymous inner classes and making your own methods and classes that support generic types.

See http://courses.coreservlets.com/Course-Materials/java.html for the complete tutorial series (Programming with Java 8) and associated code. That site also lets you download PDF files of each lecture for saving or printing, and includes exercises and exercise solutions for each of the topics.

5.
6
Why Java 8?
• Make code more flexible and reusable
– Lambda expressions
• Make code more convenient to write
– High-level methods in Stream interface
• Make code faster and more memory efficient
– Lazy evaluation and automatic parallelization for streams
• Adapt to the times
– Others will be using lambdas and streams, since they are standard part of Java now.
So, you have to learn it simply to be able to use and modify others’ code.
• Besides, once you get a taste of the benefits, you will want to use the new features
frequently

7.
8
Step 2: Install IDE that Supports Java 8
• Eclipse
– Luna or later
• http://www.eclipse.org/downloads/
– Info on Java-8-specific features in Eclipse
• https://www.eclipse.org/community/eclipse_newsletter/
2014/june/article1.php
• NetBeans
– NetBeans 8 and later
• https://netbeans.org/downloads/
– Info on Java-8-specific features in NetBeans
• https://netbeans.org/kb/docs/java/javase-jdk8.html
• IntelliJ IDEA
– IDEA 13.1 and later
• http://www.jetbrains.com/idea/
– Info on Java-8-specific features in IDEA
• https://www.jetbrains.com/idea/features/code_editor.html
All three are popular IDEs (Integrated Development Environments) for Java.
There is no agreement on which is better, but there is clear empirical
evidence that Eclipse is more widely used.
Graph from http://pypl.github.io/IDE.html. Note the log scale.

18.
19
Separate Classes: Pros and Cons
• Advantages
– Flexible: can pass arguments to class constructor
– More reusable: loosely coupled
• Disadvantage
– Need extra step to call methods in main app. How does handler call code in the
main application? It needs a reference to the main app to do so.
• E.g., you pass main app instance to constructor
• But, even then, the methods you call in the main app must be public

20.
21
Implementing Interface: Pros and Cons
• Advantages
– No extra steps needed to call methods in main app
• The code is part of the main app, so it can call any method or access any instance
variable, even private ones.
– Simple
• Widely used in real life for button handlers, when you know you will have only one
button, or for threading code when you need only one method to run in the
background.
• Disadvantage
– Inflexible: hard to have multiple different versions since you cannot pass arguments
to handler
• For example, what if you want to sort arrays two different ways? If you supply “this” as
second argument to Arrays.sort, it will refer to the same compare method both times.

23.
24
Named Inner Classes: Pros and Cons
• Advantages
– No extra steps needed to call methods in main app. Inner classes have full access to
all methods and instance variables of surrounding class, including private ones.
• However, if there is a name conflict, you have to use the unpopular
OuterClass.this.name syntax
– Flexible: can define constructor and pass arguments
• Disadvantage
– A bit harder to understand (arguably)

26.
27
Anonymous Inner Classes: Pros and Cons
• Advantages
– As with named inner classes, full access to code of surrounding class (even private
methods and variables)
– Slightly more concise than named inner classes
• But still bulky and verbose
• Disadvantage
– Harder to understand (arguably)
– Not reusable
• Cannot use the same anonymous class definition in more than one place

30.
31
Creating Your Own Generic Methods and Classes
• Intermediate capability
– Intermediate Java developers should also to be able to define classes or methods that
support generics
– In Java 7 and earlier, being able to do this was mostly reserved for advanced
developers, but it is done much more commonly in Java 8
• Because lambda functions and generic types work together for same goal: to make
code more reusable
public interface Map<K,V> { ... }
public static <T> T lastElement(List<T> elements) { ... }

31.
32
Generic Classes and Methods: Syntax Overview
• Using <TypeVariable>
– If you put variables in angle brackets in the class or method definition, it tells Java
that uses of those variables refer to types, not to values
– It is conventional to use short names in upper case, such as T, R (input type, result
type) or T1, T2 (type1, type2), or E (element type)
• Examples
public class ArrayList<E> ... {
...
}
public static <T> T randomElement(T[] array) {
...
}

33.
34
Generic Classes and Methods: Syntax Details
• Declaring methods that support generics
public static <T> T best(List<T> entries, ...) { ... }
• This says that the best method takes a List of T’s and returns a T. The <T> at the
beginning means T is not a real type, but a type that Java will figure out from the
method call.
• Java will figure out the type of T by looking at parameters to the
method call
List<Person> people = ...;
Person bestPerson = Utils.best(people, ...);
List<Car> cars = ...;
Car bestCar = Utils.best(cars, ...);

34.
35
Partial Example: randomElement
public class RandomUtils {
...
public static <T> T randomElement(T[] array) {
return(array[randomIndex(array)]);
}
} • In rest of method, T refers to a type.
• Java will figure out what type T is by looking
at the parameters of the method call.
• Even if there is an existing class actually called T,
it is irrelevant here.
This says that the method takes in an array of T’s and returns a T.
For example, if you pass in an array of Strings, you get out a String;
if you pass in an array of Employees, you get out an Employee.
No typecasts required in any of the cases.

38.
39
Generic Classes and Methods: Syntax Details
• Declaring classes or interfaces that support generics
public class SomeClass<T> { ... }
• Methods in the class can now refer to T both for arguments and
for return values
public T getSomeValue(int index) { ... }
• Java will figure out the type of T by your declaration
SomeClass<Person> blah = new SomeClass<>();

39.
40
Example: Generic Class (Simplified)
public class ArrayList<E> {
public E get(int index) { ... }
public boolean add(E element) { ... }
...
}
In rest of class, E does not refer
to an existing type. Instead, it
refers to whatever type was
defined when you created the list.
E.g., if you did
ArrayList<String> words = ...;
then E refers to String.
This says that get returns an E. So, if you created
ArrayList<Employee>, get returns an Employee.
No typecast required in the code that calls get.
This is a highly simplified version of the real java.util.ArrayList class.
That class implements multiple interfaces, and the generic support comes from the interfaces.
This says that add takes an E as a parameter.
So, if you created ArrayList<Circle>, add can take only a Circle.