Java 8: Part One
Java 8: Part One
Part One:
Date and Time Classes
Interfaces
Default Methods
Lambdas
DATE AND TIME
The java.time Package
• Existing time packages were inadequate.
• Not thread-safe
• Poor design- e.g., months start at 1, days start at 0
• The new java.time package addresses deficiencies.
The java.time Package
• Designed to be clear and intuitive
• Threadsafe
• Immutable
• Most classes are immutable!
• Create new instances with of, from, or with
• No constructors, no setters
Immutable!
• Objects in the java.time package are immutable.
• You cannot change them by invoking a method.
• Methods will return a new object that you can then save in a variable.
• This is just like Strings:
name.toUpperCase();
// returns JESSICA but the value is not used so it is
lost; name still stores Jessica
name = name.toUpperCase();
// name now stores JESSICA
Method Naming Conventions
https://docs.oracle.com/javase/tutorial/datetime/overview/naming.html
Local Dates
• Local dates and times have no associated time zone information
• Example: April 7, 2016
• This is different from zoned dates and times:
• Example: April 7, 2016, 18:00:00 PST
• Think about this like looking at the calendar on the wall.
• It is recommended to use local dates unless you really need to
represent an absolute instance of time.
The LocalDate Class
• A LocalDate object has a year, month, and day of the month.
• Static methods now and of create LocalDate objects.
• Review the single button GUI that changes the color of a rectangle.
Anonymous Classes
• Pre-Java 8, we could use an anonymous class to simplify things a little
bit.
• With an anonymous class, you can declare and instantiate a class at
the same time.
• You define the class as an expression.
• The expression is part of another statement.
• For a functional interface, all we really care about is that one method-
a function- rather than an object.
• We want to pass a function around, rather than an object.
• Argument List/Parameters
• ( ) are optional if there is only one argument
• Types are optional if they can be inferred
• Body
• Can be a single expression
• The expression is evaluated and returned (the “return” keyword is optional)
• Can be a statement block
• The body is must be surrounded with { }
• Return statement must be explicit
Simple Lambda Examples
String s1 -> {
String s2 = s1.toUpperCase();
System.out.println(s2);
return s2;
}
Lambdas and Functional Interfaces
• You can use a lambda any place an object of a functional interface is
expected.
• Example: a method takes a Comparator? You can send a lambda.
• Example: a method takes an ActionListener? You can send a lambda.
• Example: a method takes an EventHandler? You can send a lambda.
• Send in a lambda to replace the one abstract method of the functional
interface.
• Example:
• the Comparator<MyClass> method is compare(MyClass m1, MyClass m2)
• the lambda is (MyClass m1, MyClass m2) -> { code that goes inside compare }
Using Lambdas to Replace Functional
Interfaces
• Think about the method you care about inside the functional
interface.
• The one abstract method
• That’s what you’re replacing with the lambda.
• What are the parameters to that method?
• These are the lambda parameters
• What code would normally go inside your method?
• This the lambda body expression
Practice
• Replace the comparator code with lambdas.
• Replace the GUI listener code with lambdas.
Variable Scope
• A lambda expression can capture the value of a variable in the enclosing
scope as long as it is effectively final.
• What does this mean?
• Lambdas can safely use and update instance data variables.
• For local variables or formal parameters, the variable must be effectively final.
• Effectively final means you do not have to declare it as final, but you
cannot change its value.
• Using this inside of a lambda refers to the this parameter of the
method that creates the lambda.