Update on Java 8 Features

  • By Pallavi Kadam
  • November 5, 2019
  • JAVA Programming
Update on Java 8 Features

know the Latest Update on Java 8 Features Highlighted 2019

String Joiner Class was introduced in java 8.

  • The purpose of String Joinerclass was to build a sequence of characters separated by a delimiter and with a supplied prefix and ending with a supplied suffix which is optional.
  • StringJoiner is a final class and extends Object Class.


-Constructor of StringJoiner Class:

 1. public StringJoiner (CharSequence delimiter): –It builds a StringJoiner with absence of characters and also with no prefix or suffix, and a copy of the supplied delimiter. It throws NullPointerException if delimiter is null.

2. public StringJoiner (CharSequence delimiter, CharSequence prefix, CharSequence suffix): –It builds a StringJoinerabsenceof characters using mentioned prefix, delimiter and suffix in the arguments. It throws NullPointerException if prefix, delimiter, or suffix is null.

Methods of StringJoiner Class: – 

StringJoineradd(CharSequence newElement) Creates a copy of the given CharSequence value as the next element of the StringJoiner value.
Int length() It returns the count of no of chars in the String.
StringJoinermerge(StringJoiner other) It merges the existing String with another String specified in the argument.
StringJoinersetEmptyValue(CharSequence emptyValue) Sets the default value if no value is in object.


StringtoString() Returns the current value, containing of the prefix, the values added so far separated by the delimiter, and the suffix, the prefix + suffix or the emptyValue characters are returned.



import java.util.StringJoiner;

public class StringJoinerExample


public static void main(String[] args)


StringJoiner     joinNames=new StringJoiner(“,”);


//we can set default value if no value is in object





// how to add value




int length=joinNames.length();


StringJoiner joinNames2=new StringJoiner(“:”,”[“,”]”);


joinNames2.add(“Visual Coder”);                        //Merging two String

StringJoiner newString= joinNames.merge(joinNames2);




  • Java Lambda Expressions: –

Lambda expression is a new feature which is introduced in JDK1.8. A lambda expression is a shorthand representation of anonymous function. Anonymous function are those that doesn’t have a name and doesn’t belong to any class.


For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Java Lambda Expression Syntax

To create a lambda expression, we specify input parameters (if there are any) on the left side of the lambda operator ->, and place the expression or block of statements on the right side of lambda operator. For example, the lambda expression (x, y) -> x + y specifies that lambda expression takes two arguments x and y and returns the sum of these.

//Syntax of lambda expression(parameter_list)->{function_body}

Lambda expression vs method in Java

A method (or function) in Java has these main parts:
1. Name
2. Parameter list
3. Body
4. return type.

A lambda expression in Java has these main parts:
Lambda expression only has body and parameter list.
1. We don’t need to mention the nameof function as it is anonymous.
2. Parameter list
3. Function Body where implementation is done.
4. We don’t need to explicitly mentioned the return type of the function.

Use of Lambda Expression: –

Lambda expressions are used only for functional interface. A function interface contains one single abstract method which can be overridden by Lambda Expression.

Java Lambda expression Example

1)Using Anonymous Inner Class: –

interface Person {

public void eat ();


public class PersonLambda


public static void main (String [] args) {

int width=10;

Person d=new Person () {

public void eat () {System.out.println(“Drawing “+width);}


d.eat ();



2)Using Lambda Expression:

Person p= ()-> {




p.eat (); 

Ways of Using Lambda Expression: 

public class JavaLambda {

public static void main (String args []) {

JavaLambda jl = new JavaLambda ();


//with type declaration

Calci add = (int a, int b) -> a + b;

//with out type declaration

Calci sub = (a, b) -> a – b;

//with return statement along with curly braces

Calci multi = (int a, int b) ->{return a * b;};

//without return statement and without curly braces

Calci div = (int a, int b) -> a / b;

System.out.println(“10 + 5 = ” + jl. operate (10, 5, add));

System.out.println(“10 – 5 = ” + jl. operate (10, 5, sub));

System.out.println(“10 x 5 = ” + jl. operate (10, 5, multi));

System.out.println(“10 / 5 = ” + jl. operate (10, 5, div));


//without parenthesis

Demo d = msg ->

System.out.println(“Hello ” + msg);

//with parenthesis

Demo d1 = (msg) ->

System.out.println(“Hello ” + msg);




interface Calci {

int operation (int a, int b);



interface Demo {

void mesg (String message);



private int operate (int a, int b, Calci c) {

return c. operation (a, b);



Using For Each:-


import java.util.*;

public class Data


public static void main(String[] args) {


List<String> list=new ArrayList<String>();






/* for(String s:list)












3) Java 8 Date Time API Examples: –

LocalDate: –

LocalDate is afinal class that is use to represents Date with default format of YYYY-MM-DD. We can use now () method to get the current date. We can also customise input arguments for year, month and date to create LocalDate instance. Also, we can pass ZoneIdin now() for getting date in specific time zone.


public class LocalDateExample {


public static void main (String [] args)


LocalDate today = LocalDate. now ();

System.out.println(“Current Date=”+today);


LocalDate firstDay = LocalDate. Of (2014, Month. JANUARY, 1);

System.out.println(“Specific Date=”+firstDay);


LocalDate todayKolkata   =LocalDate. now (ZoneId. Of(“Asia/Kolkata”));

System.out.println(“Current Date in IST=”+todayKolkata);


LocalDate dateFromBase = LocalDate.ofEpochDay(365);

System.out.println(“365th day from base date= “+dateFromBase);


LocalDate hundredDay2014 = LocalDate.ofYearDay(2014, 100);

System.out.println(“100th day of 2014=”+hundredDay2014);




For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

LocalTime: –

LocalTime is a final class whose instance represents a time in the human readable format. Its default format is hh:mm: ss.zzz. This class displays time zone by  passing hour, minute and seconds as input arguments same as LocalDate class.


public class LocalTimeExample {

public static void main (String [] args) {

LocalTime time = LocalTime. now ();

System.out.println(“Current Time=”+time);

LocalTime specificTime = LocalTime. of (12,20,25,40);

System.out.println(“Specific Time of Day=”+specificTime);

LocalTime timeKolkata = LocalTime.now (ZoneId.of(“Asia/Kolkata”));

System.out.println(“Current Time in IST=”+timeKolkata);

LocalTime specificSecondTime = LocalTime.ofSecondOfDay(10000);

System.out.println(“10000th second time= “+specificSecondTime);




public class LocalDateTimeExample


public static void main(String[] args) {

LocalDateTime today = LocalDateTime.now();

System.out.println(“Current DateTime=”+today);

today = LocalDateTime.of(LocalDate.now(), LocalTime.now());

System.out.println(“Current DateTime=”+today);

LocalDateTime specificDate = LocalDateTime.of(2014, Month.JANUARY, 1,

10, 10, 30);

System.out.println(“Specific Date=”+specificDate);

LocalDateTimetodayKolkata=LocalDateTime.now (ZoneId.of(“Asia/Kolkata”));

System.out.println(“Current Date in IST=”+todayKolkata);

LocalDateTime dateFromBase = LocalDateTime.ofEpochSecond(10000, 0,ZoneOffset.UTC);

System.out.println(“10000th second time from 01/01/1970= “+dateFromBase);




public class JavaDuration


public static void main(String args[]){



LocalTime time1 = LocalTime.now();

Duration twoHours = Duration.ofHours(2);

LocalTime time2 = time1.plus(twoHours);

Duration duration = Duration.between(time1, time2);

System.out.println(“Duration: ” + duration);


LocalDate date1 = LocalDate.now();

System.out.println(“Current date: ” + date1);


//add 1 month to the current date

LocalDate date2 = date1.plus(1, ChronoUnit.MONTHS);

System.out.println(“Next month: ” + date2);


Period period = Period.between(date2, date1);

System.out.println(“Period: ” + period);




Class Test


public static void main(String a[])


DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;

String formattedDate = formatter.format(LocalDate.now());


String strDate = “2015-08-04”;

LocalDate aLD = LocalDate.parse(strDate);

DateTimeFormatter dTF = DateTimeFormatter.ofPattern(“dd MMM uuuu”);

System.out.println(aLD + ” formats as ” + dTF.format(aLD));




Wherever an object needs to be evaluated and a boolean value needs to be returned( or a boolean-valued Predicate exists – in mathematical terms) the Predicate functional interface can be used.


package java.util.function;

import java.util.Objects;


public interface Predicate<T> {

boolean test(T t);

//rest of the code goes here


Where –

  • boolean test(T t) is the abstract method
  • T is the type of input to the predicate
  • boolean test(T t) displays true if the input argument matches the test condition otherwise it returns false.


//Print Even and odd no of arraylist

public class Test


public static void main(String[] args)


Scanner scanner=new Scanner(System.in);

List<Integer> al=new ArrayList<Integer>();

System.out.println(“Enter the values”);

for(int i=0;i<5;i++)





Predicate<Integer> p = i -> i%2=0;



for(Integer i:al)








5.Default and static methods in interface:-

Default methods: 

If a class implements any interface and later if interface get modified by adding the method then in this case class has to implement the newly added method and for that we need to change the existing code of class.

Solution : You can declare default method in interface.

efault methods has the facilities  to add new functionality to the interfaces and upgrade it accordingly.

Whenever you are declaring a default method in the interface,you need to explicility declared it using default keyword and by default all the methods in interface are public,so there is no need of declaring it public.

Static methods: You can define static methods in interfaces. You can keep static methods specific to an interface in the same interface rather than in a separate class.These are also by default public.


public interface TimeClient {

// …

static ZoneId getZoneId (String zoneString) {

try {

return ZoneId.of(zoneString);


catch (DateTimeException e) {

System.err.println(“Invalid time zone: ” + zoneString +

“; using default time zone instead.”);

return ZoneId.systemDefault();



default ZonedDateTime getZonedDateTime(String zoneString) {

return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));}}

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

6.Method Reference:-

Method reference is a replacement of a lambda expression to call a method.


interface IsReferable {

public void referenceDemo();


public class ReferenceDemo

{   public static void commonMethod() {

System.out.println(“This method is already defined.”);


public void implement1() {

// Anonymous class.

IsReferable demoOne = new IsReferable() {


public void referenceDemo() {






public static void main(String a[])


// Lambda implementaion.

IsReferable demo = () -> ReferenceDemo.commonMethod();


// Method reference.

IsReferable demoTwo = ReferenceDemo::commonMethod;




Pallavi Kadam Software Trainer
SevenMentor Pvt Ltd.


Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *