Monday, 2 July 2018

Java 8 & 9 Features & Singleton

java 8

define static and default methods in interfaces as absract classes.. but if A class extends B class and implements C class and lets say both B and C are having same method then java8 will take priority of class method. (remember this problem only java8 since interfaces can have methods now...)

----------
functional programming(programming styleusing declarative style using expressions instead of statements) lamba expressions interface A { void show(int i);} A obj; obj = (int i) -> s.o.p("hello" + i);

internal foreach loop for lists lamda expressions... list values = Arrays.asList(4,5,6,7); ( values.foreach(i => s.o.p(i); ) lamda expression .. earlier it was like for(int i : values) { } NOTE: foreach method ideally takes Consumer as the method parameter...

whenever we have functional interface we can use lambda expressions...

------
Stream API achieves concurrency and functional programming...

list values = Arrays.asList(4,5,6,7);

values.parrallelStream().foreach(System.out::println); (here we are saying println method belongs to System.out class hence :: this is called method reference)

method reference is calling method within the method like mp.print(str,(sp::convert));
------

Date api vast improvements.. new package java.time.LocalDate; LocalTime

-----------------------

Singleton we can use private constructor and have static instance created and use static method to return that object.. else use lazy initialisation for that static instance variable in that method but remember to have null check so only 1 object is created . then to avoid multi threading issues use synchronized at method level or best synchronized block. Then to avoid multiple objects being created due to deserialisation issues use Enums best as it will implement readresolve() method.... in Enum just have INSTANCE and call EnumA.INSTANCE raa..

--------------------------

Java9 ( JShell command where we can run directly java programs like simply type hello world... etc)
interfaces private methods
Modularity like packages raa..

--------------------------

No comments: