Sunday, 26 June 2016

Difference between Singleton Pattern vs Static Class in Java

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes), so you can pass around the singleton as if it were "just another" implementation.

Static class:
Static class can not be a top level class and can not implement the interface.
It provides better performance than Singleton pattern, because static methods are bonded on compile time.
Static class always provides the eagerly loaded however Singleton classes can be lazy loaded if it’s a heavy object.

Singleton objects:
It stored on heap while static class stored in stack.
It can have constructor while static class cannot.
It can dispose but not static class.
It can clone but not with static class (logically wrong to clone the Singleton objects).

Wednesday, 15 June 2016

Can overridden methods differ in return type in Java?

Before Java 5.0, Overriding method must match the both parameters and return type exactly.

In Java 5.0, it introduces a new facility called covariant return type. In OOPS, a covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass.

We can override a method with the same signature but returns a subclass of the object returned.

An overriding method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.

class ParentClass {
     Object methodOver() {
           return newObject();
     }
}

class Child extends ParentClass {
     @Override
     String methodOver() {
           return "5";
     }
}

Case 1: If the return type is primitive data type or void.
If the return type is void or primitive then the data type of parent class method and overriding method should be same.

Example: if return type is int, float, string then it should be same.

Case 2: If the return type is derived data type.
Due to covariance return type in JDK 5.0+ versions.



Wednesday, 8 June 2016

Print Even or Odd without using conditional statement in Java



public classEvenOddWoCondition {

     /**
      * Return even odd result.
      * @param number
      * @return even or odd.
      */
     private static String getEvenOddRest(int number) {
           String[] array  = {"even","odd"};

           int index = number%2;

           returnarray[index];
     }

     public static voidmain(String[] args) {
           int number = 10;
           String result = getEvenOddRest(number);
           System.out.println(result);
     }
}

Calling run method directly of thread (created by implementing Runnable interface) in Java

It will works as ordinary java method because the thread class run() method implementation.

voidjava.lang.Thread.run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns. Subclasses of Thread should override this method.

@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

Here target is our runnable class which will set while calling Thread class constructor while creating thread.

Example:

class MyThread implements Runnable {
     @Override
     public void run() {
           System.out.println("This is my thread !!");
     }
}

public class ThreadTest {
     public static voidmain(String[] args) {
           MyThread  myThread = new MyThread();
          
           Thread thread = new Thread(myThread);
          
           thread.run();
     }
}

Monday, 6 June 2016

Reverse merge the half linked list

Write the production level code to arrange the linked list as given example:

1->2->3->4->5->6, make the following changes 1->6->2->5->3->4.

class Node {
     int value; Node next;
     public Node(intvalue) {
           this.value = value;
     }
}

public class ArrangeSuffledList {
     private static void getReversedSuffledList(Node head) {
           if(head==null|| head.next==null) {
                printList(head);
                return;
           }
          
           Node end = head.next;
           int length = 1;
           while(end != null) {
                end = end.next;
                length++;
           }

           Node mid = head.next;
           for (inti = 0; i < length/2-1; i++) {
                mid = mid.next;
           }

           //printList(mid);
           Node head2 = new Node(mid.value);
           Node next = mid.next;

           for (inti = 0; i < length-length/2-1; i++) {
                Node tNext = next.next;
                next = new Node(next.value);
                Node temp = next;
                temp.next = head2;
                head2 = temp;
                next = tNext;       

           }

           printList(mergeList(head, head2, length));

     }

     private static Node mergeList(Node head1, Node head2, int length) {
           Node head = new Node(head1.value);
           head.next = new Node(head2.value);
           Node next = head.next;

           int count = 2;

           head1 = head1.next;
           head2 = head2.next;

           while(head2!=null) {
                Node temp = newNode(head1.value);
                next.next = temp;
                next = next.next;
                count++;
                if(count<length) {
                     temp = newNode(head2.value);
                     next.next = temp;
                     next = next.next;
                     count++;
                }

                head1 = head1.next;
                head2 = head2.next;
           }
           return head;
     }

     public static void main(String[] args) {
           Node head = new Node(1);
           head.next = new Node(2);
           head.next.next = new Node(3);
           head.next.next.next = new Node(4);
           head.next.next.next.next = new Node(5);
           head.next.next.next.next.next = new Node(6);
           //head.next.next.next.next.next.next = new Node(7);

           /* 1->2->3->4->5->6 :: 1->6->2->5->3->4 * */
           getReversedSuffledList(head);
           //printList(head);
     }

     private static void printList(Node head) {
           System.out.println("");
           Node next = null;
           if(head!=null) {
                System.out.print(head.value+" ");
                next = head.next;
           }

           while(next!=null) {
                System.out.print(next.value+" ");
                next = next.next;
           }
     }
}
Related Posts Plugin for WordPress, Blogger...