Wednesday, 21 February 2018

Java 9 - Avoid Null Pointer Exception with requireNonNullElse and requireNonNullElseGet methods

(Download One Minute Java Mobile app for latest Java updates 
App Link : One Minute Java )

Overview
Java 7 and Java 8 provided some better ways to handle null pointer exceptions by providing variants of requireNonNull().
But Java 9 came up with 2 methods requireNonNullElse() and requireNonNullElseGet() using which you can provide a default value and can avoid throwing Null Pointer exception.
All the variants of requireNonNull are static methods of java.util.Objects class.
Lets iterate over all these methods in below section.

Java 7 introduced requireNonNull()
requireNonNull method in Java 7 has 2 variants requireNonNull(T obj) and requireNonNull(T obj,String message).
lets have a look at example so that we can understand it better.

Example

  public void city(String latitude, String longitude) {

 this.latitude = Objects.requireNonNull(latitude);
 this.longitude = Objects.requireNonNull(longitude, "longitude must not be null");

 }


Output 1: when latitude is passed null

  Exception in thread "main" java.lang.NullPointerException
    at at java.util.Objects.requireNonNull(Unknown Source)


Output 2: when longitutde is passed null

 Exception in thread "main" java.lang.NullPointerException: longitude must not be null
    at at java.util.Objects.requireNonNull(Unknown Source)


Advantages
As we can see in output 2 a more redable customized message can be conveyed using this method.
requireNonNull also makes code more redable and less cluttered.

Java 8 addition
Java 8 added new variant requireNonNull(T obj,Supplier &ltString&gt messageSupplier).
This is the optimised version as it helps to defer the creation of message string till it is actually needed.
Creating a string at runtime is costly affair and you can avoid it using supplier, below is what Java doc says :
This method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.

How to use Supplier

  String name = null;

 Objects.requireNonNull(name, new Supplier&ltString&gt() {
   @Override
   public String get() {
   return "Name cannot be null";
   }
 });


Java 9 addition
Java 9 further added 2 more methods requireNonNullElse​(T obj,T defaultObj) and requireNonNullElseGet​(T obj, Supplier&lt ? extends T&gt supplier).
These methods return first argument if it is non-null and otherwise returns the default non-null second argument.
Main advantage of this is, you can avoid throwing null pointer exeception.

Example

  Employee emp = null;

  Employee emp2 = Objects.requireNonNullElse(emp, new Employee());


Explaination
Since emp is null in above example another object of same type is returned, thus avoding NullPointerException.
Second variant of this method accepts a Supplier for returning a default value instead of new object of same type.
As discussed earlier Supplier has added advantage of differing execution until it is needed.


Monday, 19 February 2018

Java 8 - Join Strings Easily


(Download One Minute Java Mobile app for latest updates)

StringJoiner
StringJoiner class of package java.util.StringJoiner is very helpful for joining delimiter seprated string.
It is used to build a characters sequence separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

Example


 If we want to create a string of names seperated by ':' and enclosed in '[' ']' braces than we can write   code as below

 StringJoiner sj = new StringJoiner(":", "[", "]"); ----> 1
 sj.add("George");
 sj.add("Sally");
 sj.add("Fred");
 String requiredString = sj.toString();

 If we print 'requiredString' the output will be as below.

 "[George:Sally:Fred]"

 Note : In line 1 of above example delimiter ':' is compulsory parameter and the prefix '[' and sufix ']'   are optional parameters


Static join() of String
Static method join() from String class is very handy for joining strings in collections

Example


 Set stringSet = new HashSet();
 stringSet.add("Sachin");
 stringSet.add("Rahul");
 stringSet.add("Saurav");
 String newString = String.join(",", stringSet);


 If we print 'newString' the output will be as below.
 "Saurav,Rahul,Sachin"


Strong, Soft, Weak and Phantom References in Java

(Download One Minute Java Mobile app for latest updates)

Strong References
Strong references are the normal references which you create by new keyword
These references are not cleared until the reference is marked as null or the scope in which reference is created is executed completely

Soft References
These are the references which are marked for garbage collection, but garbage collector may not clear them till there is sufficient memory available
Garbage collector ensures that all soft references are cleared before throwing OutOfMemoryError.
This mean, soft references may remain in memory until JVM has exhausted all its heap memory and when there is a dire need of memory it will clear all soft references

Example

  Employee emp = new Employee();

 //Create Soft reference for emp object
 SoftReference&ltEmployee&gt softRefForEmp = new SoftReference&ltEmployee&gt(emp);

 //mark emp object for Garbage Collection
 emp = null;

 //if the soft reference is not yet cleared by garbage collector you can still get back the object in following way

 emp = softRefForEmp.get();

 //if the soft reference is cleared by garbage collector the above method will return null

Usage of Soft References
Direct instances of this class may be used to implement simple caches; this class or derived subclasses may also be used in larger data structures to implement more sophisticated caches. As long as the referent of a soft reference is strongly reachable, that is, is actually in use, the soft reference will not be cleared. Thus a sophisticated cache can, for example, prevent its most recently used entries from being discarded by keeping strong referents to those entries, leaving the remaining entries to be discarded at the discretion of the garbage collector.

Weak References
These references are garbage collected when there is no strong or soft references to the referent
Weak References are garbage collected even though plenty of memory is available
Example of weak reference could be exactly similar as above so I am not repeating it here. Only change is you have to use WeakReference class

Usage of Weak References
Weak references are most often used to implement canonicalizing mappings.
Canonical mappings are used in the scenarios where our dataset is huge and we have only limited memory, in such cases we can use weak references so that GC can remove used data immediately to load fresh set of data

Phantom References
When there is no strong, soft and weak references to the referent Phantom references can be garbage collected
Phantom references are the objects which are finalized but not yet cleared by garbage collector.
Memory management can be a issue while using phantom references this is because as per java docs:

  • Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable

As we have seen in the above example object of the referent can be retrived in cases of SoftReferences and WeakReferences but in case of phantom references get() method always return null

Usage of Phantom References
Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

Difference Between Yield() and Join()

(Download One Minute Java Mobile app for latest updates)

What is Yeild() in java?

When a thread executes a java.lang.Thread.yield(), the executing thread is suspended and the CPU is given to some other runnable thread. This thread will be in runnable state until the CPU becomes available again.

In other words, the executing thread is put back into the ready queue i.e in runnable state of the processor and waits for its next turn.

Yield method causes the currently executing thread object to temporarily pause and allow other threads to execute. However there is no guarantee that current thread will pause immediately.
Point to note here is, Yield is a static method of Thread class.

Yeild Example


  public class YieldExample
 {
    public static void main(String[] args)
    {
      Thread t1 = new Thread1();
      Thread t2 = new Thread2();
      t1.start();
      t2.start();
   }
  }

 class Thread1 extends Thread
 {
    public void run()
    {
      for (int i = 0; i < 3; i++)
      {
       System.out.println( i + " Iteration of " + "Thread1 is yielding control to Thread2 " );
        Thread.yield();
        System.out.println( i + " Iteration of " + "Thread1 is continuing after Thread2 ");
       }
    }
  }

 class Thread2 extends Thread
 {
    public void run()
    {
      for (int i = 0; i < 3; i++)
      {
       System.out.println( i + " Iteration of " + "Thread2 is yielding control to Thread1 " );
        Thread.yield();
        System.out.println( i + " Iteration of " + "Thread2 is continuing after Thread1 ");
       }
    }
  }


Output


  0 Iteration of Thread1 is yielding control to Thread2
  0 Iteration of Thread2 is yielding control to Thread1
  0 Iteration of Thread1 is continuing after Thread2
  0 Iteration of Thread2 is continuing after Thread1
  1 Iteration of Thread2 is yielding control to Thread1
  1 Iteration of Thread1 is yielding control to Thread2
  1 Iteration of Thread2 is continuing after Thread1
  1 Iteration of Thread1 is continuing after Thread2
  2 Iteration of Thread2 is yielding control to Thread1
  2 Iteration of Thread2 is continuing after Thread1
  2 Iteration of Thread1 is yielding control to Thread2
  2 Iteration of Thread1 is continuing after Thread2


What is Join() in java?

java.lang.Thread class has the join() method which allows one thread to stop execution until another thread completes its execution. In other words, the currently running threads stop executing until the thread it joins with, completes its task.

Variants of join method

Join() : Current thread will wait until the joined thread is dead.
Join(long milliseconds) : Current thread will wait until the joined thread is dead or for specified milliseconds whichever is earlier.
Join (long millis, int nanos) : Current thread will wait until the joined thread is dead or for specified milliseconds + nanos whichever is earlier.

Join Example


 public class JoinExample implements Runnable
 {
  Thread t;
  JoinExample(String threadName)
  {
   t = Thread.currentThread();
   t.setName(threadName);
  }
  public void run()
  {
   if(t.isAlive())
   {
    System.out.println(t.getName() + " is running...");
    System.out.println(t.getName() + " is complete...");
   }
  }
  public static void main(String args[]) throws Exception
  {
   System.out.println("Main Thread started...");
   Thread t = new Thread(new JoinExample("DemoThread"));
   t.start();
   System.out.println("Main Thread paused...");
   // current thread i.e main thread waits for DemoThread to complete
   t.join();
   System.out.print("Main Thread started again...");
  }
 }

Output


  Main Thread started...
  Main Thread paused...
  DemoThread is running...
  DemoThread is complete...
  Main Thread started again...


Comparison

Yeild() Join()
Current thread gives chance to other threads that have equal priority in the Thread-pool. Current thread will pause execution till another thread complete its excution
Current thread goes into runnable state Current thread goes into wait state till other thread is dead
yield() is a static method of thread class Join is not a static method of thread class

Difference between Java IO & NIO packages

(Download One Minute Java Mobile app for latest updates)

Overview
Java IO package provides API’s for input and output operations and Java NIO (Networking API) introduced in Java 7 provides an alternate way to do the same.

Comparison
Java IO Java NIO
Blocking I/O
Java IO's streams are blocking. Means when a thread invokes a read() or write(), that thread will wait until there is some data to read at source, or the data is fully written to destination
Non Blocking I/O
Java NIO's is non-blocking. Means a thread can issue a read request from a channel, and if nothing is available thread returns and can do some other task till the data is available for reading . It does not wait for the data to be read or write before returning.
Stream-oriented
Java IO API’s uses Streams for reading or writing data. Here a Java program uses stream to transport data from/to source/destincation.
Buffer-oriented
While writing data Java NIO API’s write it to buffer, and Channel reads data from buffer to write into destination.
While reading channel reads data from source and put it in buffer, and Java program using NIO package reads data from this buffer.
Selectors
Selectors are not available in Java IO
Selectors
Selectors are available in NIO package. Selectors are nothing but object that can monitor multiple channels at a time. To monitor a channel you need to first register it with selector and after that selector will observe the registered channels for 4 different operations i.e Connect, Accept, Read and Write.

Java 8 Default Method Interface

(Download One Minute Java Mobile App for Latest Updates)

Introduction

Prior to Java SE 8, interfaces were expected to have abstract methods only and the classes implementing the interfaces had to override all those abstract methods. But Java SE 8 has made it possible to hold method definitions in an interface using Default methods.
Default Methods can be used to introduce new functionalities into interfaces without breaking the existing classes which implement those interfaces. The Default methods provide their own definitions, so there is no compulsion on implementing classes to override default methods.

Syntax


  public interface firstDefaultTest
 {
  default void test()
  {
   System.out.println("This is a default method with default keyword");
  }
 }

Things to know

  • A default method cannot override methods from java.lang.Object
  • An interface can also have a static default method from Java 8
  • There are chances that 2 different interfaces may have default method with same name and signature and a class might implement both the interfaces, which causes ambiguity.
    • One way is to override the default method in implementing class
    • Other way is to override default method and invoke default method of the required interface with super keyword ex: interfaceName.super.methodName()
  • Default methods help to remove base implementation classes. The default implementations can be provided in the interface and the implementating classes can choose which one to override.

Debugging collections with checkedCollection()

Download One Minute Java mobile app for latest updates

Overview
checkedCollection() is a static method in Collections class
Java Docs says checkedCollection() returns a dynamically typesafe view of the specified collection. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException.
To understand what the previous statement mean, lets first have look at the problem which checkedCollection() helps us to solve

Problem Statement
While writing huge code it might happen we insert incorrect type of elemnt in parameterised collection. Later on somewhere down the code we try to perform some operation on the collection and at that point we get a ClassCastException.
In big code it really takes a lot of time to identify the exact point where incorrect element was inserted. This is because the classCastException indicates the line where the operation is failed and not the line where the element is inserted
Lets look at this problem with below example

Example

  public class CheckedCollectionExample
 {
    public static void main(String[] args)
    {
     1 List &ltString&gt cities = new ArrayList<>();
     2 Collections.addAll(cities, "Pune", "Nashik", "Dhule");

     3 List listInterface = cities;

     4 List codes = listInterface;
     5 codes.add(42);

     6 System.out.println(String.join(",", cities));

   }
  }


Error Message

  Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.CharSequence
    at java.lang.String.join(Unknown Source)
    at CheckedCollectionExample.main(CheckedCollectionExample.java:6)


Error Message Analysis
The above error message points at the line of operation and not at the line of element insertion, this can be a real pain in big code

How checkedCollection() can help
Lets see below example

  public class CheckedCollectionExample
 {
    public static void main(String[] args)
    {
     1 List &ltString&gt cities = Collections.checkedList(new ArrayList<>(), String.class);
     2 Collections.addAll(cities, "Pune", "Nashik", "Dhule");

     3 List listInterface = cities;

     4 List codes = listInterface;
     5 codes.add(42);

     6 System.out.println(String.join(",", cities));

   }
  }


Error Message

  Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Integer element into collection with element type class java.lang.String
    at java.util.Collections$CheckedCollection.typeCheck(Unknown Source)
    at java.util.Collections$CheckedCollection.add(Unknown Source)
    at CheckedCollectionExample.main(CheckedCollectionExample.java:5)


Error Message Analysis
As you can see above error message clearly mention the cause as well as points to the corect line where incorrect element is inserted
So next time whenever you come across classCastException in your code you can utilise checkedCollection() method