What's New in Java SE 8
A Quick Look at the Platform Enhancements
1
Objectives
When we are done, you should be able to use:
Functional interfaces
Default methods
Lambdas and lambda expressions
java.time package
2© DevelopIntelligence http://www.DevelopIntelligence.com
Functional Interfaces
Interface containing only one method
Can annotate with java.lang.FunctionalInterface
If only one method, the annotation is not needed
Primarily used as targets for lambdas
Several have been pre-defined in
java.util.function
Predicate – used for tests; takes an argument, compares it to
the given Predicate
Function – takes an argument, returns response
Consumer – takes an argument, returns nothing
3© DevelopIntelligence http://www.DevelopIntelligence.com
Default Methods
Specialized methods found in interfaces
Marked with default keyword
Allows us to add methods to an interface long after it has
been released without breaking the interface
4© DevelopIntelligence http://www.DevelopIntelligence.com
@FunctionalInterface
public interface Predicate<T> {
public boolean test();
default Predicate<T> and(Predicate<? super T> other){
//do stuff
}
}
Lambda Expressions
Mechanism to pass functionality as an argument
Way to simplify reading of code
Anonymous inner classes tend to be bulky
Multiple functionality options decrease DRY properties and/or
cause explosion in number of methods
5© DevelopIntelligence http://www.DevelopIntelligence.com
Example: Customer
public class Customer {
private String firstName, surName;
private String street, city, state, postalCode;
public Customer (String firstName, String surName,
String postalCode){
this.firstName = firstName;
this.surName = surName;
this.postalCode = postalCode;
}
<all appropriate accessor, mutators and constructors
written here>
6© DevelopIntelligence http://www.DevelopIntelligence.com
Example: MailingList
public List<Customer> findBySurName(String surName){
List<Customer> tempList = new ArrayList<>();
for(Customer c : customerList){
if(c.getSurName().equals(surName)){
tempList.add(c);
}
}
return tempList;
}
We would need something like this for every search method
7© DevelopIntelligence http://www.DevelopIntelligence.com
Writing Lambdas
Consists of three parts
Comma-separated list of parameters enclosed in parentheses
Arrow →
Body of expression
If one expression, it is evaluated and result is returned
If it is a block, return statement can be explicitly defined
Sometimes contains ::
Can only be used when the Java compiler can determine
the data type
Called “Target Type”
8© DevelopIntelligence http://www.DevelopIntelligence.com
Lambda Expression: Part 1
Called method replaces all the various options
public List<Customer> findCustomer
(Predicate<Customer> tester){
List<Customer> customer = new ArrayList<>();
for(Customer c: customerList) {
if(tester.test(c))
customer.add(c);
}
return customer;
}
9© DevelopIntelligence http://www.DevelopIntelligence.com
Lambda Expression: Part 2
Calling method has the lambda expression
modifiedList = ml.findCustomer(c->
c.getSurName().equals("Smith") &&
c.getPostalCode().equals("12346"));
for(Customer c: modifiedList){
System.out.printf("Customer List: %s, %sn",
c.getSurName(), c.getFirstName());
});
10© DevelopIntelligence http://www.DevelopIntelligence.com
Charset Improvements
Goal
Decrease size of installed charsets
Reduce maintenance cost
Improve performance of encoding/decoding
Improved performance of String(byte[], …) and
string.getBytes()
11© DevelopIntelligence http://www.DevelopIntelligence.com
New Date-Time Package
java.time package is the base
Divided into two types
• Human – based on days, months, hours
• Machine (or continuous) – based on Unix Time
Immutable and thus thread safe
Methods perform same action in each class
Works with ISO-8601 calendar system and non-ISO calendar
systems
Designed to be more natural to use and less clumsy than
Calendar
12© DevelopIntelligence http://www.DevelopIntelligence.com
Date-Time Example
13© DevelopIntelligence http://www.DevelopIntelligence.com
import java.time.*;
public class DateTime {
public static void main(String[] args) {
LocalTime local = LocalTime.now();
System.out.println("The time is now " + local);
LocalTime hours = local.plusHours(5);
System.out.println("In 5 hours is will be " + hours);
int seconds = local.toSecondOfDay();
System.out.println("The total number of seconds for
today is " + seconds);
}
}
Date-Time Example (cont.)
The previous example has the following results:
The time is now 20:07:43.289
In 5 hours it will be 01:07:43.289
The total number of seconds since midnight is 72463
14© DevelopIntelligence http://www.DevelopIntelligence.com
java.time Package
Has two enums
DayOfWeek
Month
Continuous classes
Instant
Duration
All others are considered 'human'
15© DevelopIntelligence http://www.DevelopIntelligence.com
SelectorProvider
Abstract class whose subclasses give us access to the
various channel streams
Now have one specific to Solaris using its event port mechanism
-Djava.nio.channels.spi.SelectorProvider=
sun.nio.ch.EventPortSelectorProvider
16© DevelopIntelligence http://www.DevelopIntelligence.com
Closing
Thank you!

DevelopIntelligence
http://www.Develop
Intelligence.com
Contact
Does your team need to build its Java skills and knowledge beyond today's
webinar’s offerings? We have some of the best Java instructors in the industry.
Contact us today to discuss available learning solutions.
Develop Intelligence
(877) 629-5631
info@developintelligence.com
Twitter: @DevIntelligence
Stay in the know about developer news, tools, SDKs, technical presentations,
events and future webinars, connect with AMD Central here:
AMD Developer Central
Twitter: @AMDDevCentral
Web: http://developer.amd.com/
Developer Forums: devgurus.amd.com/welcome

Webinar: Whats New in Java 8 with Develop Intelligence

  • 1.
    What's New inJava SE 8 A Quick Look at the Platform Enhancements 1
  • 2.
    Objectives When we aredone, you should be able to use: Functional interfaces Default methods Lambdas and lambda expressions java.time package 2© DevelopIntelligence http://www.DevelopIntelligence.com
  • 3.
    Functional Interfaces Interface containingonly one method Can annotate with java.lang.FunctionalInterface If only one method, the annotation is not needed Primarily used as targets for lambdas Several have been pre-defined in java.util.function Predicate – used for tests; takes an argument, compares it to the given Predicate Function – takes an argument, returns response Consumer – takes an argument, returns nothing 3© DevelopIntelligence http://www.DevelopIntelligence.com
  • 4.
    Default Methods Specialized methodsfound in interfaces Marked with default keyword Allows us to add methods to an interface long after it has been released without breaking the interface 4© DevelopIntelligence http://www.DevelopIntelligence.com @FunctionalInterface public interface Predicate<T> { public boolean test(); default Predicate<T> and(Predicate<? super T> other){ //do stuff } }
  • 5.
    Lambda Expressions Mechanism topass functionality as an argument Way to simplify reading of code Anonymous inner classes tend to be bulky Multiple functionality options decrease DRY properties and/or cause explosion in number of methods 5© DevelopIntelligence http://www.DevelopIntelligence.com
  • 6.
    Example: Customer public classCustomer { private String firstName, surName; private String street, city, state, postalCode; public Customer (String firstName, String surName, String postalCode){ this.firstName = firstName; this.surName = surName; this.postalCode = postalCode; } <all appropriate accessor, mutators and constructors written here> 6© DevelopIntelligence http://www.DevelopIntelligence.com
  • 7.
    Example: MailingList public List<Customer>findBySurName(String surName){ List<Customer> tempList = new ArrayList<>(); for(Customer c : customerList){ if(c.getSurName().equals(surName)){ tempList.add(c); } } return tempList; } We would need something like this for every search method 7© DevelopIntelligence http://www.DevelopIntelligence.com
  • 8.
    Writing Lambdas Consists ofthree parts Comma-separated list of parameters enclosed in parentheses Arrow → Body of expression If one expression, it is evaluated and result is returned If it is a block, return statement can be explicitly defined Sometimes contains :: Can only be used when the Java compiler can determine the data type Called “Target Type” 8© DevelopIntelligence http://www.DevelopIntelligence.com
  • 9.
    Lambda Expression: Part1 Called method replaces all the various options public List<Customer> findCustomer (Predicate<Customer> tester){ List<Customer> customer = new ArrayList<>(); for(Customer c: customerList) { if(tester.test(c)) customer.add(c); } return customer; } 9© DevelopIntelligence http://www.DevelopIntelligence.com
  • 10.
    Lambda Expression: Part2 Calling method has the lambda expression modifiedList = ml.findCustomer(c-> c.getSurName().equals("Smith") && c.getPostalCode().equals("12346")); for(Customer c: modifiedList){ System.out.printf("Customer List: %s, %sn", c.getSurName(), c.getFirstName()); }); 10© DevelopIntelligence http://www.DevelopIntelligence.com
  • 11.
    Charset Improvements Goal Decrease sizeof installed charsets Reduce maintenance cost Improve performance of encoding/decoding Improved performance of String(byte[], …) and string.getBytes() 11© DevelopIntelligence http://www.DevelopIntelligence.com
  • 12.
    New Date-Time Package java.timepackage is the base Divided into two types • Human – based on days, months, hours • Machine (or continuous) – based on Unix Time Immutable and thus thread safe Methods perform same action in each class Works with ISO-8601 calendar system and non-ISO calendar systems Designed to be more natural to use and less clumsy than Calendar 12© DevelopIntelligence http://www.DevelopIntelligence.com
  • 13.
    Date-Time Example 13© DevelopIntelligencehttp://www.DevelopIntelligence.com import java.time.*; public class DateTime { public static void main(String[] args) { LocalTime local = LocalTime.now(); System.out.println("The time is now " + local); LocalTime hours = local.plusHours(5); System.out.println("In 5 hours is will be " + hours); int seconds = local.toSecondOfDay(); System.out.println("The total number of seconds for today is " + seconds); } }
  • 14.
    Date-Time Example (cont.) Theprevious example has the following results: The time is now 20:07:43.289 In 5 hours it will be 01:07:43.289 The total number of seconds since midnight is 72463 14© DevelopIntelligence http://www.DevelopIntelligence.com
  • 15.
    java.time Package Has twoenums DayOfWeek Month Continuous classes Instant Duration All others are considered 'human' 15© DevelopIntelligence http://www.DevelopIntelligence.com
  • 16.
    SelectorProvider Abstract class whosesubclasses give us access to the various channel streams Now have one specific to Solaris using its event port mechanism -Djava.nio.channels.spi.SelectorProvider= sun.nio.ch.EventPortSelectorProvider 16© DevelopIntelligence http://www.DevelopIntelligence.com
  • 17.
  • 18.
    Contact Does your teamneed to build its Java skills and knowledge beyond today's webinar’s offerings? We have some of the best Java instructors in the industry. Contact us today to discuss available learning solutions. Develop Intelligence (877) 629-5631 info@developintelligence.com Twitter: @DevIntelligence Stay in the know about developer news, tools, SDKs, technical presentations, events and future webinars, connect with AMD Central here: AMD Developer Central Twitter: @AMDDevCentral Web: http://developer.amd.com/ Developer Forums: devgurus.amd.com/welcome