There are many new features added in java 8.
-----------------------------------------------------
[Link] keyword: default keyword was introduced in java 8. using default keyword
we can create complete methods in an interface.
--
package [Link];
public interface Sammy {
public void m2();
default void m1() {
[Link]("Hii This is default method");
}
default void m3() {
[Link]("Hii this is second default method");
}
}
---
package [Link];
public class Abc implements Sammy {
@Override
public void m2() {
[Link]("Hii This is abstarct method");
}
}
------
package [Link];
public class Main {
public static void main(String[] args) {
Abc a = new Abc();
a.m1();
a.m2();
a.m3();
}
}
------------------------------------------------
[Link] interface:functional interface was introduced in java [Link]
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 [Link];
@FunctionalInterface
public interface Sammy {
public void m2();
public void m3();//this line gives error because it contains only one incomplete
method
default void m1() {
[Link]("Hii This is default method");
}
default void m3() {
[Link]("Hii this is second default method");
}
}
---
package [Link];
public class Abc implements Sammy {
@Override
public void m2() {
[Link]("Hii This is abstarct method");
}
}
------
package [Link];
public class Main {
public static void main(String[] args) {
Abc a = new Abc();
a.m1();
a.m2();
a.m3();
}
}
--------------------------------------------------------
[Link] 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 [Link];
@FunctionalInterface
public interface Abc {
public void test(int x);
default void m1() {
[Link]("Default");
}
}
--------------
package [Link];
public class Main {
public static void main(String[] args) {
Abc a=(x)->
{
[Link]("HII");
};
[Link](12);
a.m1();
}
}
-----------------------------------
ex 2:
package [Link];
@FunctionalInterface
public interface Abc {
public int test(int a,int b);
default void m1() {
[Link]("Default");
}
}
-------
package [Link];
public class Main {
public static void main(String[] args) {
Abc a1=(a,b)->
a+b;
[Link]([Link](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 [Link];
import [Link];
public class optionalExample {
public static void main(String[] args) {
String str=null;
Optional<String> opt=[Link](str);
[Link]([Link]());
[Link]([Link]("No value present in object"));
[Link]([Link]());
}
}
--------------------------------------