Tuesday, June 21, 2011

Release Memory, Valid URL, Valid Email, Valid Phone

1) How we release a memory in android ?
Answer :-
Suppose if we use in our activity
                     Button btnYes;
                     LinearLayout llMain;
                     Gallery gallery
                     ImageButton btnSearch
                     ImageView backImageView;
 

@Override
 protected void onPause() {
       releaseViewMemory(btnYes);
       releaseViewMemory(llMain);
       releaseViewMemory(gallery);
                                   releaseViewMemory(btnSearch);         
                                   releaseImageViewMemory(backImageView);
                                   super.onPause();
                             }

public static void releaseViewMemory(View view) {
    if (view != null) {
        Drawable draw = view.getBackground();
        if (draw != null) {
           draw.setCallback(null);
           draw = null;
        }
   }
}

public static void releaseImageViewMemory(ImageView imgView) {
     if (imgView != null) {
         Drawable draw = imgView.getBackground();
         if (draw != null) {
            draw.setCallback(null);
            draw = null;
         }
         draw = imgView.getDrawable();
         if (draw != null) {
            draw.setCallback(null);
            draw = null;
         }
     }
}

2) Write a method for valid url.
Answer :-
 

public static boolean isValidURL(String url) {
    boolean isValid = false;
    String expression = "^(http|https|ftp)\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\\-\\._\\?\\,\\'/\\\\+&%\\$#\\=~])*$";
   CharSequence inputStr = url;
   Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
   Matcher matcher = pattern.matcher(inputStr);
   if (matcher.matches()) {
      isValid = true;
   }
   return isValid;
}

3) Write a method for valid email.
Answer :-
 
public static boolean isValidEmail(String email) {
    boolean isValid = false;
    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}quot;;
    CharSequence inputStr = email;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}



3) Write a method for valid phone.
Answer :-
 
public static boolean isValidphone(String phone) {
    boolean isValid = false;
    String expression = "^[+][0-9]{10,13}$";
    CharSequence inputstr = phone;
    if (phone.length() == 0)
        return false;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputstr);
    if (matcher.matches()) {
        if (phone.length() == 10)
            isValid = true;
        }
        return isValid;
   }


Thursday, June 2, 2011

Core Java Questions Part-2

1) What is Collection ?
Answer : Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List. 


è SetA Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Ø  SortedSet a Set that maintains its elements in ascending order.

è ListA List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.
Ø  ArrayList - Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.
Ø  LinkedList
Ø  Vector - The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index.

è QueueA collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.

è MapAn object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value.
o   SortedMap — A Map that maintains its mappings in ascending key order.

2) What is the difference between List, Set and Map? 
Answer : A Set is a collection that has no duplicate elements. A List is a collection that has an order associated with its elements. A map is a way of storing key/value pairs. The way of storing a Map is similar to two-column table.

3) What is the difference between Vector and ArrayList ?
Answer : Vector is synchronized, ArrayList is not. Vector is having a constructor to specify the incremental capacity. But ArrayList don't have. By default Vector grows by 100% but ArrayList grows by 50% only.

4) What is the difference between Hashtable and HashMap ?
Answer :
 Hashtable is synchronized . but HashMap is not synchronized. Hashtable does not allow null values , but HashMap allows null values.

5) What is the difference between array and ArrayList ?
Answer :
 Array is collection of same data type. Array size is fixed, It cannot be expanded. But ArrayList is a growable collection of objects. ArrayList is a part of Collections Framework and can work with only objects.

6) What is any an anonymous class?
Answer :
 An anonymous class is a local class with no name.

7) What is the difference between synchronized block and synchronized method ?
Answer : Synchronized blocks place locks for the specified block where as synchronized methods place locks for the entire method.