Saturday, 26 July 2014

Usage of "this" operator in Java

The this keyword is a reference to the current object.
Another way to think about it is that the this keyword is like a personal pronoun that you use to reference yourself. Other languages have different words for the same concept. VB uses Me and the Python convention (as Python does not use a keyword, simply an implicit parameter to each method) is to use self.
If you were to reference objects that are intrinsically yours you would say something like this:
My arm or my leg
Think of this as just a way for a type to say "my". So a psuedocode representation would look like this:

class Employee
{
    private int age;

    public Employee(int age)
    {
        my.age = age;
    }
}


First lets take a look on code
public class Employee  {

private int empId;
private String name;

public int getEmpId() {
    return this.empId;
}

public String getName() {
    return this.name;
}

public void setEmpId(int empId) {
    this.empId = empId;
}

public void setName(String name) {
    this.name = name;
}
}
In the above method getName() return instance variable name. Now lets take another look of similar code is
public class Employee  {

private int empId;
private String name;

public int getEmpId() {
    return this.empId;
}

public String getName() {

    String name="Yasir Shabbir";
    return name;
}

public void setEmpId(int empId) {
    this.empId = empId;
}

public void setName(String name) {
    this.name = name;
}


public static void main(String []args){
    Employee e=new Employee();
    e.setName("Programmer of UOS");

    System.out.println(e.getName());

}
}
Output Yasir Shabbir
  • this operator always work with instance variable(Belong to Object) not any class variable(Belong to Class)
  • this always refer to class non static attribute not any other parameter or local variable.
  • this always use in non static method
  • this operator cannot work on static variable(Class variable)
**NOTE:**It’s often a logic error when a method contains a parameter or local variable that has the same name as a field of the class. In this case, use reference this if you wish to access the field of the class—otherwise, the method parameter or local variable will be referenced.









Thursday, 24 July 2014

JDBC Driver Types

Java Database Connection is a set of classes that help us to connect and communicate with database.
Every vendor of database add the code in classes that implements JDBC interface and you don't need to re-invent the wheel from scratch; you have just call the public method and every particular method of driver know how to react and behave.

Let's consider 
createConnection(); here is the method and every jdbc vendor driver know how to create a connection with database.Oracle vendor driver know how to create connection with oracle database and similarly Microsoft vendor Driver know how to create connection with the database etc.

Note :You have use JDBC interface from java.sql classes, so the type of driver will not have any logic impact in your code, it will only have run time / deployment impact.




Type of JDBC Driver
  
There are 4 JDBC driver tpes/level
Type 1: JDBC-ODBC Bridge driver (Bridge)

Type 2: Native-API/partly Java driver (Native)


Type 3: All Java/Net-protocol driver (Middle ware)


Type 4: All Java/Native-protocol driver (Pure)



Type 1
ODBC (Open database Connectivity) is actually Microsoft protocol to chat fetching and updating  the data of database.JDBC -ODBC bridge driver which really means an ODBC connection wrapped in JDBC clothing.
 In this implemented class in Java makes calls to the code written in Microsoft languages (native), which speaks directly to the database.
 In this you first create DSN (Domain Source Name) in the client machine then it's registered with the JDBC.
This is very first and slow driver type of JDBC relatively than other driver type;because translation or conversion and many step require to reach database data.


 
Type 2 Driver
A type 2 JDBC driver is like a type 1 driver, except the ODBC part is replaced with a native code part instead.Actually the type-2 drivers  use a Java (JDBC) API to call native api and native code work with the database.





Type 3 Driver

Type 3 drivers basically proxy (or relay) the request to another network resource. That typically incurs an extra network hit, but again, that doesn't say much about actual performance.follows a three-tiered approach whereby the JDBC database requests are passed through the network to the middle-tier server. The middle-tier server then translates the request (directly or indirectly) to the database-specific native-connectivity interface to further the request to the database server.

Type 4 Driver
The Type 4 uses Java  libraries to communicate directly with the database server.This driver is fastest among all drivers.The Java program connects directly to the database if type  driver used.




Sunday, 20 July 2014

N Tier Architecture

How do you structure an application to support such operational requirements as maintainability,re-usability, scalability and robustness? The answer lies in using N tier architecture.
N tier architecture means splitting up the system into N tiers, where N is a number from 1 and up. 
1 tier architecture is the same as a single process architecture.
2 tier architecture is the same as a client / server architecture etc.
3 tier architecture is a very common architecture. A 3 tier architecture is typically split into a presentation or GUI tier, an application logic tier, and a data tier. 

2 Tier Architecture
The two-tier architecture is like client server application. The direct communication takes place between client and server. There is no intermediate between client and server.





The above figure shows the architecture of two-tier. 


Three tier Architecture
The architecture in which whole application infrastructure is divided in to three Tier 

  1. Client Tier/ Presentation Tier
  2. Business Intelligence / Processing Tier
  3. Data Tier


Below picture explain the above picture 


Presentation Tier
Presentation tier are use for where client can interact the with the application.In this Tier there are HTML, CSS, JSP are used.Usually user can interact with application using Web browser (Presentation Layer).

Middle / Business Logic Tier

Business tier are the actually tier for programmer, in this business rule and regulation are defined how system respond and how to react when and where.
This tier is for  Servlet, Core Java other technologies are ASP.net or PHP work only.


Data Tier
Database developer , maintainer and operator are working in this tier.In this SQL (Structure Query Language) are been used for to interact with database and database may be Oracle, SQL Server or MySQL.

Looking on above working strategies , we have realized that work is divided into module and specialized person.Everyone know how to work and perform. If any tier may get some error then only particular tier will be solved , not to check other tier.This give us Highly cohesion and loosely coupled system.
Let suppose if you have desktop system, and your keyboard not working properly then you check only your keyboard not your monitor or mouse.




Object Serialization


Object serialization is a process of converting an object instance into a sequence of bytes (which may be binary).Serialized object is an object represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.After a serialized object has been written into a file, it can be read from the file and deserialized—that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
It is very useful when you want to transmit one object data across the network, for instance from one JVM to another.



In Java ; you can achieve Object Serialization by implement Serializable.
Here is the good convention for to make object serialization
  • First implement the Serializable interface 
  • Your class like POJO (Plain Old Java Object)


Code Example
Employee.java
public class Employee implements Serializable {

        private int empId;
        private String name;

        public int getEmpId() {
            return empId;
        }

        public String getName() {
            return name;
        }

        public void setEmpId(int empId) {
            this.empId = empId;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "EMployee id : " + empId + "  \nEmployee Name : " + name;
        }
}
Another Main public class
pubic class Main{
        public static void main(String[] args) throws FileNotFoundException, IOException,   ClassNotFoundException {

            String filename = "data.txt";
            Employee e = new Employee();
            e.setEmpId(101);
            e.setName("Yasir Shabbir");
        enter code here
            FileOutputStream fos = null;
            ObjectOutputStream out = null;

            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(e);

            out.close();


            // Now to read the object from file
            // save the object to file
            FileInputStream fis = null;
            ObjectInputStream in = null;

            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            e = (Employee) in.readObject();
            in.close();

            System.out.println(e.toString());
        }
    }
}