0% found this document useful (0 votes)
20 views3 pages

Java 8

The document discusses new features introduced in Java 8 including default methods in interfaces, functional interfaces, lambda expressions, and the Optional class. It provides code examples for each feature: default methods allow interfaces to include default implementations; functional interfaces can have one abstract method and any number of default methods and must be annotated with @FunctionalInterface; lambda expressions provide a concise way to write anonymous functions for functional interfaces; and the Optional class handles potential null values and allows checking if a value is present without throwing NullPointerExceptions.

Uploaded by

Sammy Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Java 8

The document discusses new features introduced in Java 8 including default methods in interfaces, functional interfaces, lambda expressions, and the Optional class. It provides code examples for each feature: default methods allow interfaces to include default implementations; functional interfaces can have one abstract method and any number of default methods and must be annotated with @FunctionalInterface; lambda expressions provide a concise way to write anonymous functions for functional interfaces; and the Optional class handles potential null values and allows checking if a value is present without throwing NullPointerExceptions.

Uploaded by

Sammy Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

There are many new features added in java 8.

-----------------------------------------------------
1.default keyword: default keyword was introduced in java 8. using default keyword
we can create complete methods in an interface.
--
package com.nt.defaultkeyword;

public interface Sammy {


public void m2();
default void m1() {
System.out.println("Hii This is default method");
}
default void m3() {
System.out.println("Hii this is second default method");
}

}
---
package com.nt.defaultkeyword;

public class Abc implements Sammy {

@Override
public void m2() {
System.out.println("Hii This is abstarct method");
}

}
------
package com.nt.defaultkeyword;

public class Main {


public static void main(String[] args) {
Abc a = new Abc();
a.m1();
a.m2();
a.m3();
}

}
------------------------------------------------
2.functional interface:functional interface was introduced in java 8.functional
interface can cosists of exactly one incomplete method but it can have any number
of complete method in it.
---functional interface always annotated with @FunctionalInterface annotation.
----ex:

package com.nt.defaultkeyword;
@FunctionalInterface
public interface Sammy {
public void m2();
public void m3();//this line gives error because it contains only one incomplete
method

default void m1() {


System.out.println("Hii This is default method");
}
default void m3() {
System.out.println("Hii this is second default method");
}

}
---
package com.nt.defaultkeyword;

public class Abc implements Sammy {

@Override
public void m2() {
System.out.println("Hii This is abstarct method");
}

}
------
package com.nt.defaultkeyword;

public class Main {


public static void main(String[] args) {
Abc a = new Abc();
a.m1();
a.m2();
a.m3();
}

}
--------------------------------------------------------
3.lambda expression: lambda expression was introduce in java 8.
using lambda expression we can reduce the number of lines of java code.
lambda expression can be applied only on functional interface.
--------
imp)-- lambda expression is an anonymous function,
which has no name,no modifiers,no return type.
----------------------------------------------------------------------

package com.nt.lamda;
@FunctionalInterface
public interface Abc {
public void test(int x);
default void m1() {
System.out.println("Default");
}
}
--------------
package com.nt.lamda;

public class Main {


public static void main(String[] args) {
Abc a=(x)->
{
System.out.println("HII");
};
a.test(12);
a.m1();
}

}
-----------------------------------
ex 2:
package com.nt.lamda;
@FunctionalInterface
public interface Abc {
public int test(int a,int b);
default void m1() {
System.out.println("Default");
}
}
-------
package com.nt.lamda;

public class Main {


public static void main(String[] args) {
Abc a1=(a,b)->
a+b;

System.out.println(a1.test(12,23));
a1.m1();
}

}
------------------------------------------
4. Optional class: optional class was introduce in java 8.
optional class is used to handle the NullPointerException.
optional class is used to find the value is present or not .

---isPresent():--ispresent() method return true if value is present in object


othrewise it will return false.
---get():-get() method return the value the if value is present in object otherwise
it will throw NoSuchElementException.
----ex:

package com.opt;

import java.util.Optional;

public class optionalExample {


public static void main(String[] args) {
String str=null;
Optional<String> opt=Optional.ofNullable(str);
System.out.println(opt.isPresent());

System.out.println(opt.orElse("No value present in object"));


System.out.println(opt.get());
}
}
--------------------------------------

You might also like