Saturday 21 December 2013

How to know the .class is complied by which jdk version?

In a compiled class (.class file) we see is “ca fe ba be”. This is the java magic number which says that this is a java binary class file. It is immediately followed by minor_version and then major_version.

Sample hex view of a java binary class:
ca fe ba be 00 00 00 32 ...

Sunday 15 December 2013

Java Server Pages

Java Server Faces (JSF) is a Java-based web application framework intended to simplify development integration of web-based user interfaces.

JSF 2 uses Facelets as its default templating system. Other view technologies such as XUL can also be employed. In contrast, JSF 1.x uses JavaServer Pages (JSP) as its default templating system.

JSF is often used together with Ajax, a Rich Internet application technology. Ajax is a combination of technologies that make it possible to create rich user interfaces. The user interface components in Mojarra and Apache MyFaces were originally developed for HTML only, and Ajax had to be added via JavaScript.

Latest developments

Facelets (which was designed specifically for JavaServer Faces) was adopted as the official view technology for JSF 2.0. This eliminates the life-cycle conflicts that existed with JSP, forcing workarounds by Java developers. Facelets allows easy component/tag creation using XML markup instead of Java code, the chief complaint against JSF 1.x.

The new JSF developments also provide wide accessibility to Java 5 annotations such as @ManagedBean, @ManagedProperty and @FacesComponent which removes the need for faces-config.xml in all cases except framework extension. Navigation has been simplified, removing the need for faces-config.xml navigation cases. Page transitions can be invoked simply by passing the name of the desired View/Facelet.

Addition of Partial State Saving and DOM updates are part of the built in standardized Ajax support.
The latest JSF release has built-in support for handling resources like images, CSS and Javascript, allowing artifacts to be included with component libraries, separated into JAR files, or simply co-located into a consistent place within the web-application. Includes logical naming and versioning of resources.

JSF 2.0 also includes a number of other changes like adding support for events, separate development, staging, and production modes, similar to RAILS_ENV in Ruby on Rails, and significantly expanding the standard set of components.

 

 

Saturday 9 November 2013

No SQL vs SQL

Types


SQL      : One type (SQL database) with minor variations

No SQL : Many different types including key-value stores, document databases, wide-column stores, and graph databases

Development History

SQL      : Developed in 1970s to deal with first wave of data storage applications

No SQL : Developed in 2000s to deal with limitations of SQL databases, particularly concerning scale, replication and unstructured data storage

Examples

SQL      : MySQL, Postgres, Oracle Database

No SQL : MongoDB, Cassandra, HBase, Neo4j

Data Storage Model

SQL      : Individual records are stored as rows in tables, with each column storing a specific piece of data about that record

No SQL : Varies based on database type. For example, key-value stores function similarly to SQL databases, but have only two columns ("key" and "value"), with more complex information sometimes stored within the "value" columns. Document databases do away with the table-and-row model altogether, storing all relevant data together in single "document" in JSON, XML, or another format, which can nest values hierarchically.

Schemas

SQL : Vertically, meaning a single server must be made increasingly powerful in order to deal with increased demand.

No SQL : Horizontally,The database automatically spreads data across servers as necessary

Development Model

SQL : Mix of open-source (e.g., Postgres, MySQL) and closed source (e.g., Oracle Database)

No SQL :  Open-source

Supports Transactions

SQL : Yes, updates can be configured to complete entirely or not at all

No SQL :  In certain circumstances and at certain levels (e.g., document level vs. database level)

Data Manipulation

SQL : Specific language using Select, Insert, and Update statements, e.g. SELECT fields FROM table WHERE…

No SQL : Through object-oriented APIs

Consistency
SQL : Can be configured for strong consistency

No SQL : Depends on product. Some provide strong consistency (e.g., MongoDB) whereas others offer eventual consistency (e.g., Cassandra)

No SQL

No SQL
  • A NoSQL database provides a mechanism for storage and retrieval of data that employs less constrained consistency models than traditional relational databases. 
  •  NoSQL databases are finding significant and growing industry use in big data and real-time web applications.
  •   NoSQL systems are also referred to as "Not only SQL" to emphasize that they may in fact allow SQL-like query languages to be used.

NoSQL encompasses a wide variety of different database technologies and were developed in response to a rise in the volume of data stored about users, objects and products, the frequency in which this data is accessed, and performance and processing needs. Relational databases, on the other hand, were not designed to cope with the scale and agility challenges that face modern applications, nor were they built to take advantage of the cheap storage and processing power available today. 

The Benefits of NoSQL

When compared to relational databases, NoSQL databases are more scalable and provide superior performance, and their data model addresses several issues that the relational model is not designed to address:
  • Large volumes of structured, semi-structured, and unstructured data
  • Agile sprints, quick iteration, and frequent code pushes
  • Object-oriented programming that is easy to use and flexible
  • Efficient, scale-out architecture instead of expensive, monolithic architecture

NoSQL Database Types

  • Document databases pair each key with a complex data structure known as a document. Documents can contain many different key-value pairs, or key-array pairs, or even nested documents.
  • Graph stores are used to store information about networks, such as social connections. Graph stores include Neo4J and HyperGraphDB.
  • Key-value stores are the simplest NoSQL databases. Every single item in the database is stored as an attribute name (or "key"), together with its value. Examples of key-value stores are Riak and Voldemort. Some key-value stores, such as Redis, allow each value to have a type, such as "integer", which adds functionality.
  • Wide-column stores such as Cassandra and HBase are optimized for queries over large datasets, and store columns of data together, instead of rows.

Saturday 26 October 2013

How to avoid deadlock in Java Threads

What is deadlock ?

 When two or more threads waiting for each other to release lock and get stuck for infinite time , situation is called deadlock . it will only happen in case of multitasking.

How do you detect deadlock in Java ?
 
if nested synchronized block or calling one synchronized method from other or trying to get lock on different object then there is good chance of deadlock if developer is not very careful.

write code which will result in deadlock ?

public void method1(){
synchronized(String.class){
System.out.println("Aquired lock on String.class object");

synchronized (Integer.class) {
System.out.println("Aquired lock on Integer.class object");
}
}
}

public void method2(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}

How to avoid deadlock in Java ?
 
public void method1(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}

public void method2(){
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");

synchronized (String.class) {
System.out.println("Aquired lock on String.class object");
}
}
}


 

Now there would not be any deadlock because both method is accessing lock on Integer and String object in same order . so if thread A acquires lock on Integer object , thread B will not proceed until thread A releases Integer lock , same way thread A will not be blocked even if thread B holds String lock because now thread B will not expect thread A to release Integer lock to proceed further.

 

Sunday 20 October 2013

Java Memory Types

Java has only two types of memory when it comes to JVM.

Heap memory and Non-heap memory.

Heap Memory

Class instances and arrays are stored in heap memory. Heap memory is also called as shared memory. As this is the place where multiple threads will share the same data.

Non-heap Memory

It comprises of ‘Method Area’ and other memory required for internal processing. So here the major player is ‘Method Area’.

Method Area

As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means run time constants and static fields.


Memory Pool

Memory pools are created by JVM memory managers during run time. Memory pool may belong to either heap or non-heap memory.

Runtime Constant Pool

A run time constant pool is a per-class or per-interface run time representation of the constant_pool table in a class file. Each run time constant pool is allocated from the Java virtual machine’s method
area.
jvm memory

Saturday 19 October 2013

Abstract class vs Interface

Abstract class:
  • cannot be instantiated.
  • is a special type of class in which you can have members without implementation.
  • as we know in C#/VB/Java, a class can only inherit from 1 class. This also applies for Abstract Class.
  • normally used for framework-type library classes: providing default behavior for some of its class members, but forcing the developer to implement others.
  • is believed to be faster in Java,
  • the aim: making sure something is *eventually* implemented.
  • A class can inherit an abstract class without implementing all its abstract method.
    However only a class that has all its method implemented can be instantiated to an object.
  • IS-A relationship.
  • e.g. Student IS A Person, Employee IS A Person.
// 'framework library' for a person
// a person can enrol and submit
// however, the class that consume this framework library
// need to provide 'where' the paperwork need to be sent
public abstract Person
{
    public abstract SendPaperWork(string paperwork)

    public void Enrol()
    {
        SendPaperWork("enrolment");
    }

    public void Submit()
    {
        SendPaperWork("report");
    }
}

// by inheriting Person abstract class
// we are enabling student to enrol and submit
// however, SendPaperWork need to be implemented
// because we need to tell it explicitly 'where' 
// to send the enrolment/ submission
public class Student : Person
{
    public override SendPaperWork(string paperwork)
    {
        School.Send(paperwork);
    }
}

// an employee send the paperwork to a different 'place' than student
public class Employee : Person
{
    public override SendPaperWork(string paperwork)
    {
        Company.Send(paperwork);
    }
}
Interface:
  • cannot be instantiated.
  • is a special type of abstract class in which all the members do not have any implementations.
  • enables polymorphism. A class can implement more than 1 Interfaces.
  • normally used for application classes: providing contract for ensuring interactibility.
  • the aim: making sure something is interchangeable.
  • A class that implements an Interface need to contain all the implementation, otherwise the compiler will throw an error.
  • CAN-DO relationship.
  • e.g. Student CAN enrol, Student CAN submit assignment.
public interface ICanEnrol
{
    void Enrol();
}

public interface ICanSubmit
{
    void Submit();
}

public class Student : ICanEnrol, ICanSubmit
{
    public void Enrol()
    {
        School.Send("enrolment");
    }

    public void Submit()
    {
        School.Send("report");
    }
}

public class Employee : ICanEnrol, ICanSubmit
{
    public void Enrol()
    {
        Company.Send("enrolment");
    }

    public void Submit()
    {
        Company.Send("report");
    }
}

public class MailServer
{
    public void SendAllSubmissions()
    {
        // AllSubmitters is a collection of students and employees
        foreach (ICanSubmit submitter in AllSubmitters)
        {
            // The MailServer does not care if 
            // the submitter is a student
            // or an employee, as long as it can submit
            submitter.Submit()
        }
    }
}