Thursday, 22 April 2021

Java: Function & BiFunction interfaces

 Function and BiFunction are the functional interfaces introduced from java 8. Simples examples are as below.



Wednesday, 21 April 2021

Java: forEachRemaining() method

The forEachRemaining() method is introduced in java 8 itself. This method makes iteration on iterator very easy. Basically, this method uses a lambda expression and reduces a lot of unnecessary code. Check below example,




Java: join() method

 The join() is used to hold the execution of the main thread for the time our thread on which we are calling join() method is completed. A simple example of this join() method is as below,



Java: clone() method

 The clone() method is used to create a copy of any class which implements the Clonable interface. If the class does not implement the Clonable interface and you still call clonable on that then it will throw ClassNotSupportedException. When we call this clone method then the copy of primitive types will be created but object references will be copied as they are. So basically original and cloned object will have the same object references. So modification in the cloned object will be reflected in the original object.

Shallow copy example,











If we need a deep copy of any object then we will need to implement the clone method manually,

Deep copy example,




Tuesday, 20 April 2021

Java: yield() method

 The yeild() method gives chance to other high-priority threads to execute. This is one of the static native method from the Thread class. 


When we call Thread.yield() in any of the running thread then the currently running thread will move to the ready state and other thread with higher priority will get a chance to execute. If another waiting thread doesn't have a higher priority than the current thread then, in that case, the current thread will continue its execution.


The Explanation from Thread class is,












Example of yield method,