Monday, 21 January 2013

JDK 1.7 : Useful Features

 Useful Features in JDK 1.7

Following are the features that are very useful JDK 1.7

     1.       Strings in switch Statements   : We can start using Strings in switch case statements:

String option;
switch(option)
{
        case “start”:
                        System.out.println(“Starting the process”);
                        …….
                        break;
        case “pause”:
                        System.out.println(“Pausing the process”);
                        break;
        case “stop”:
                        System.out.println(“Stopping the process”);
                        break;
        default:
                        System.out.println(“continue”);
                        break;
}

     2.       Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type CheckingWe would have been using multiple catch blocks doing the same thing. Below is an example where this can be used
Pre JDK 1.7
JDK 1.7
try
{
                ….
}catch(FileNotFoundException fe)
{
   log.error(“---EXCEPTION occurred with file handling-- ”+ fe.getMessage());
   throw new CustomException(fe);
}catch(IOException ie)
{
   log.error(“---EXCEPTION occurred with file handling-- ”+ ie.getMessage());
   throw new CustomException(Ie);
}catch(Exception e)
{
   log.error(“---EXCEPTION occurred with file handling-- ”+ e.getMessage());
   throw new CustomException(e);
}

try
{
}catch(FileNotFoundException |  IOException | Exception e)
{
    log.error(“---EXCEPTION occurred with file handling-- ”+ e.getMessage());
    throw new CustomException(e);
}



3.  The try-with-resources Statement  :  We can open Autoclosable  resources like Streams, JDBC connection, Statements, Resultset within the try.  The resources will be automatically closed when the try ends.


Pre JDK 1.7
JDK 1.7 (resources are auto-closed after the try statement)
BufferedReader breader = new BufferedReader(new FileReader(path));
FileOutputStream fs = new FileOutputStream(new File (…));
  try {
     …
       …
  } finally {
    if (breader != null)
         breader.close();
    if (os != null)
         os.close();
  }
try(BufferedReader breader = new BufferedReader(new FileReader(path));
     FileOutputStream fs = new FileOutputStream(new File (…))
  ){
      ………………….
  }catch(..){
     ….
  }



   4. Automatic Type inference : Easy Type declaration. This will not work when declaring Generics with wildcard.
Pre JDK 1.7
JDK 1.7 :  Easy declaration with “diamond Operator”
List<String> listNames = new ArrayList<String>();
Map<String, Integer> mapStrNumbers = new HashMap<String, Integer>();

List<String> listNames = new ArrayList<>();
Map<String, Integer> mapStrNumbers = new HashMap<>();


Wednesday, 9 January 2013

Quick Summary : Object Associations



Composition

final class Company{
  private Employee emp;
  public Company(EmpDetails details) {
    emp= new Employee(details);
  }
   void assign() {
      emp.work();
   } 
}

Aggregation

final class Company{
  private Employee engine;
  void addEmployee(Employee emp) {
    this.emp = emp;
  }
  void assign() {
    if (emp != null)
      emp.work();
  }
}

Dependency

final class Company{
  void assign(Employee emp) {
    if (emp != null)
      emp.work();
  }
}

Abstraction

public interface Employee{
 public void work();
 public void off();
 public void quit();
}

Realisation

public abstract class Engineer implements Employee
{
 public void work();
 public void off();
 public void quit();
}

Generalization

public class SWEngineer extends Engineer
{
 public void work()
 {
  System.out.println("SW Engineer working");
 }
 public void off()
 {
  System.out.println("SW Engineer is off today");
 }
 
 public void quit()
 {
  System.out.println("SW Engineer is quitting");
 }
 
}
 

Relationships


Association
Defines a relationship between classes. Compositon and Aggregation are types of associations
Composition
the Employee is encapsulated within the Company . There is no way for the outside world to get a reference to the Employee. The Employee is created and destroyed with the company
Aggregation
The Company also performs its functions through an Employee, but the Employee is not always an internal part of the Company . Employees can be exchanged, or even completely removed. As the employee is injected, the Employee reference can live outside the  Company.
Dependency
The company does not hold the employee reference. It receives an employee reference only to the scope an operation. Company is  dependent on the Employee object to perform an operation
Abstraction
Defines  the basic operations the implementer should adher to.  Employee interface lists the general behavior of an employee
Realization
A class implements the behavior defined by the other other class or interface
Generalization
A class which is a special form of a parent class

 UML Relationship Pointers



References