Wednesday, March 2, 2016

Difference between wait and sleep methods

1) wait() method must be called from synchronized context i.e. from synchronized method or block in Java. If you call wait method without synchronization, it will throw IllegalMonitorStateException in Java.

2) wait() method operates on Object and defined in Object class while sleep operates on current Thread and defined in java.lang.Thread class.

3) wait() method releases the lock of object on which it has called, it does release other locks if it holds any while sleep method of Thread class does not release any lock at all.

4) wait() is a non static method while sleep() is static method in Java.

Tuesday, March 1, 2016

3 ways to convert array to ArrayList

Yes. We can do it this from three ways.

1. Arrays.asList(...)
      e.g.,
             String[] array = {"value1", "value2", "value3", "value4"};
             List<String> arrayList = Arrays.asList(array);
 
2. Collections.addAll(...)
       e.g., Collections.addAll(exampleList, "Value1", "Value2" , "Value3");

3. In spring framework---> CollectionUtils.arrayToList(...)
      e.g.,
            String [] array = {"value1", "value2", "value3", "value4"};           
            List<String> arrayList = CollectionUtils.arrayToList(array);

Saturday, February 27, 2016

What happens if a class doesn't implement all the methods defined in the interface?


A class must implement all methods of the interface, unless the class is declared as abstract. There are only two choices:
  • 1.Implement every method defined by the interface.
  • 2.Declare the class as an abstract class, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects. 

The only case the class do not need to implement all methods in the interface is when any class in its inheritance tree has already provided concrete (i.e., non-abstract) method implementations then the subclass is under no obligation to re-implement those methods. The supclass may not implement the interface at all and just method signature is matched. For example,
interface MyInterface {  
  void m() throws NullPointerException;
} 

class SuperClass {
//NOTE : SuperClass class doesn't implements MyInterface interface 
  public void m() {
    System.out.println("Inside SuperClass m()");
  }
}

class SubClass extends SuperClass implements MyInterface {
}

public class Program {  
  public static void main(String args[]) {
    SubClass s = new SubClass();
    s.m();
  }
}


SimpleDateFormat and DateFormat

SimpleDateFormat is a class used to format and parsing dates in a locale sensitive manner. It is used to format date into text and parse text into date.
SimpleDateFormat is extends DateFormat class which is an abstract class for date/time formatting subclasses and provides many class methods for obtaining date/time matters based on any given locale. package com.lea.oops;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Date date = new Date();
       
        String str = date.toString();
        System.out.println(str);
       
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        System.out.println(dateFormat.format(date));
    }

}

Abstraction in Java

What is abstraction? Abstraction is the concept of segregate the implementation of interface. It will focus only on essential details instead of showing the full implementation details. Abstraction can be applied through abstract class and interface.
Abstract class is something which is incomplete and provide common information for the subclasses. You cannot create instance for abstract class. If you want to use it you need to make it complete or concrete by extending it.
When do you use abstraction? When you know something needs to be there but not sure how exactly it should like.
Abstraction key points
  • Use abstraction if you know something needs to be in class but implementation of that varies. 
  • You cannot create instance of abstract class using new operator. 
  •  Abstract class can have constructor 
  •  A class automatically become abstract class when any of its method declared as abstract. 
  •  Abstract method does not have method body 
  •  Variable cannot be made abstract. Its only for class and methods 
  •  If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be concrete class.

Friday, February 26, 2016

Why abstract class can have constructor in Java?

The main reason is, when any class extend abstract class, constructor of sub class will invoke constructor of super class either implicitly or explicitly.

Write a program to find the first non repeated character in a String?

package com.lea.oops;

import java.util.HashMap;
import java.util.Scanner;

public class FirstNonRepeatedCharacter {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
       
        char c = firstNonRepeatedCharacter(s);
       
        System.out.println(c);
       
    }

    public static Character firstNonRepeatedCharacter(String str){
       
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
       
        int i, length;
        Character c;
       
        length = str.length();
       
        for (int j = 0; j < length; j++) {
            c = str.charAt(j);
           
            if (hashMap.containsKey(c)) {
                hashMap.put(c, hashMap.get(c)+1);
            }else{
                hashMap.put(c, 1);
            }
        }
       
        for (int j = 0; j < length; j++) {
            c = str.charAt(j);
            if (hashMap.get(c) == 1) {
                return c;
            }
        }
       
        return null;
    }
}