Monthly Archives: June 2014

09Jun/14

Why Learn Android Programming

Why Learn Android Programming?

The IT Job market is on the rise and one of the top job from the top jobs list include Mobile application development. The ramp of Mobile will impact all forms of technology and will continue with its pace. So along with it comes the need for talent to drive those processes and hence lot of organizations are looking forward to hire programmers in mobile development space. People from all over are switching from laptops and notebooks to smartphones for many things.

There are various smart phone operating systems(OS) available like Android, iOS, Blackberry, Windows Mobile, Symbian etc. Among all of them Android stands out as the winner with no other competitor even near to it in terms of its user base. Android has a huge market share and almost 300 million smart phones run Android OS. It looks set to completely dominate the high growth in developing markets and has set the stage to increase it further.

  • Among the developing markets, India ranks only behind to China in terms of the smart phone users which is just 10 % of the overall mobile users base. So there is a huge potential going forward for growth in India itself.
  • In India each subscriber average is almost 2 SIM cards so the current active mobile connections is 760 million and the number of registered mobile connection is 889 million.
  • Projections suggests that Active mobile connections in India should exceed 900 million by 2016 and the use of smart phones will increase very rapidly.
  • The average time by an Indian user on mobile apps is around 70 to 75 minutes per day.
  • The revenue generated by Mobile value added services is expected to grow and touch around Rs. 500 billion by 2015.

More importantly, According to IDC, Android enjoys 91% of market share in India and the second nearest is windows which is just 5.4%. So considering the value added services growth and the market share of Android it is pretty clear that is a huge potential for Android based application development.

The salary range in India for an entry level android application developers is in the range of 3 lacs to 4.5 lacs PA. The job locations for Android developers is across India. One should have good hands-on knowledge on Java before taking up Android programming.

Hope it is pretty clear that learning android programming will help you to have a bright career in the challenging and growth oriented IT sector.

Happy Learning!!

 

02Jun/14

Lambda Expressions – Part I

Lambda expressions

Introduction

Java as a programming language has evolved continuously over various versions adding new features like Swing, NIO, Annotations, Generics, Auto-boxing, Java FX and the latest Java 8 has introduced the much awaited very important feature Lambda expressions.

The typical usage of several interfaces like ActionListener, Runnable etc. were as anonymous classes inside the code where it is used. One single alternative to writing an anonymous class would have been writing a separate class for each interface thus increasing the number of classes. If you look closely at each of these interfaces there is only a single method inside each of these interfaces. Lambda expression provides a very clear and concise way to represent one method interface using an expression. Along with Java 8, an interface like ActionListener, Runnable that follows this pattern is called as a “Functional Interface“.

Let us Dig further

The following table depicts the composition of a Lambda expression:

LBComposition

 

Let us have a look at the examples using the above parts in the following picture:

LDexamples

Lambda expressions reduces the number of lines of code which are required in case of anonymous classes. Let us run through a quick example of the ActionListener using Anonymous class and make the code shorter using Lambda expression.

ActionListener

As you can see from the class above the five lines of code for adding an anonymous class is done in just one line of code. When the program and click the check button following output is seen on your console:

ActionListeneroutput

Note: In the above scenario lambda expression is also passed as a parameter.

Java 8, comes with a set of standard functional interfaces and is part of java.util.function package. Out of these standard functional interfaces let us understand the Predicate interface which is as below:

public interface Predicate<T>{
   public boolean test(T t);
}

The test method takes a generic class and returns a boolean result.

Consider one example in which you have to place an order for items whose quantity is less than or equal to 10. Prior to Java 8, one would hard code the comparison inside the method while iterating over the collection of items. Using Lambda expressions functional interface Predicate life becomes easier as seen below in the code example.

Consider the class Item.java

import java.util.List;
import java.util.ArrayList;
public class Item {
  
  private Item(Item.Builder builder){
   id = builder.id;
   qty = builder.qty;
   itemName = builder.itemName;
  }
  private Item(){
   super();
  }
  
  private long id;
  private String itemName;
  private int qty;
  
  public long getId(){
   return id;
  }
  
  public int getQty(){
   return qty;
  }
  public String getItemName(){
   return itemName;
  }
  
  public void setId(long id){
   this.id = id;
  }
  
  public void setQty(int qty){
   this.qty = qty;
  }
  public String setItemName(String itemName){
   return itemName;
  }
  public static List<Item> createItemList(){
   List<Item> items = new ArrayList<Item>();
   
   items.add(
   new Item.Builder()
            .id(1)
            .itemName("Screw")
            .qty(10)
            .build() );
   
   items.add(
   new Item.Builder()
            .id(2)
            .itemName("Bolt-12")
            .qty(9)
            .build() );
   
   items.add(
   new Item.Builder()
            .id(3)
            .itemName("Bolt-14")
            .qty(12)
            .build() );
    
    for(Item i : items){
     System.out.println(i.getItemName());
    }
   
   return items;
  }  
 
  public static class Builder{
  
   private long id = 0;
   private String itemName = "";
   private int qty = 0;
   
   public Item.Builder id(long id){
     this.id = id;
     return this;
   }
   
   public Item.Builder itemName (String itemName){
     this.itemName = itemName;
     return this;
   }
   public Item.Builder qty(int qty){
     this.qty = qty;
     return this;
   }
   public Item build(){
    return new Item(this);
   }
    }
 }

Now let us code the OrderManager class in which there is a method for ordering the items if they lie below or equal to the required quantity.

import java.util.*;
import java.util.function.Predicate;
public class OrderManager{
 public void placeOrder(List<Item> items, Predicate<Item> pred){
  for(Item item: items){
   if(pred.test(item)){
    System.out.println("Place order for item : " + item.getItemName());
   }
  }
 }
}

The class PredicateTest.java for passing the predicate to the reorder method.

import java.util.List;
import java.util.function.Predicate;
public class PredicateTest {
 public static void main(String[] args){
 
  List<Item> items = Item.createItemList();
  
  OrderManager mgr = new OrderManager();
  
  Predicate<Item> reorder = i -> i.getQty() <= 10;
  
  mgr.placeOrder(items, reorder);
 
 }
}

Finally, the output on the console:

Predicate

The output seen above shows that we have to place the order for item Screw and Bolt-12.

Following are some set of standard interfaces which also has a primitive version.

  • Consumer: An action to be performed with the object passed as argument
  • Function: Transform a T to a U
  • Supplier: Provide an instance of a T (such as a factory)
  • UnaryOperator: A unary operator from T -> T
  • BinaryOperator: A binary operator from (T, T) -> T

In this part it might have become clear about the Lambda expressions syntax, Lambda expressions will replace anonymous inner classes, and Predicate used for checking or testing on a list. In the next tutorial we will cover a bit further on Lambda expressions and its usage on collections.

Happy Learning!!!