Java 8

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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