Safari 4 – New browser …

Earlier today Apple unveiled the first public beta of the Safari 4 browser. I downloaded the browser and took it for a quick spin … and I have to say, this is a huge improvement on what I’ve come to expect from Safari.
Safari 4 is a nice browser. Feels to me a lot like Google Chrome, but a little more stylish. Read more..

What is use of serialVersionUID

 During object serialization, the default Java serialization mechanism writes the metadata about the object, which includes the class name, field names and types, and superclass. This class definition is stored as a part of the serialized object. This stored metadata enables the deserialization process to reconstitute the objects and map the stream data into the class attributes with the appropriate type
Everytime an object is serialized the java serialization mechanism automatically computes a hash value. ObjectStreamClass’s computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value.The serialVersionUID is also called suid.
So when the serilaize object is retrieved , the JVM first evaluates the suid of the serialized class and compares the suid value with the one of the object. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not InvalidClassException exception is thrown.

Changes to a serializable class can be compatible or incompatible. Following is the list of changes which are compatible:

  • Add fields
  • Change a field from static to non-static
  • Change a field from transient to non-transient
  • Add classes to the object tree

List of incompatible changes:

  • Delete fields
  • Change class hierarchy
  • Change non-static to static
  • Change non-transient to transient
  • Change type of a primitive field

So, if no suid is present , inspite of making compatible changes, jvm generates new suid thus resulting in an exception if prior release version object is used .
The only way to get rid of the exception is to recompile and deploy the application again.

If we explicitly metion the suid using the statement:

private final static long serialVersionUID = <integer value>

then if any of the metioned compatible changes are made the class need not to be recompiled. But for incompatible changes there is no other way than to compile again.

Dependency Injection in Spring

Dependency Injection (DI): is a way to let the framework or the container work out the complexities of service instantiation, initialization and sequencing and supplies the service references to the clients as required. In spring IOC container has the responsibility of doing DI.


Consider a simple example:


public interface Vehicle {

                 void displayData();



A FourWheeler class implements Vehicle interface.


public class FourWheeler implements Vehicle {


      private String name;

      private int purchaseValue;

      private int noOfTyres;

      public FourWheeler(){}                          // default constructor                   

public FourWheeler(String name, int purchaseValue, int noOfTyres) {


   = name;

            this.purchaseValue = purchaseValue;

            this.noOfTyres = noOfTyres;


      public void displayData() {

            System.out.println(” Name of vehicle is “+ name);

            System.out.println(“Purchase Value is “ +purchaseValue);

            System.out.println(“Tyres the vehicle has “ +noOfTyres);


      public String getName() {

            return name;


      public void setName(String name) {

   = name;


      public int getPurchaseValue() {

            return purchaseValue;


      public void setPurchaseValue(int purchaseValue) {

            this.purchaseValue = purchaseValue;


      public int getNoOfTyres() {

            return noOfTyres;


      public void setNoOfTyres(int noOfTyres) {

            this.noOfTyres = noOfTyres;




The different ways in which the class can be instantiated using DI in spring.xml:


1) Setter based injection: The values of the properties of the class are set with the help of setter. Each class needs to declare the setter of the properties and their values are set from the spring configuration file.


 <?xml version=“1.0” encoding=“UTF-8”?>



       <bean id=“fourWheeler” class=“com.src.FourWheeler”> 

            <property name=“noOfTyres”> <value>4</value>



<property name=“name” value=“Honda”/>                                  

<property name=“purchaseValue” value=“900000”/>  




In above example the setter methods are created for noOfTyres, name, purchaseValue in FourWheeler  class and the values are set using <property> <value> tag in xml file.

Note that for each property the value is set using a different syntax. All syntaxes are valid and

can be used depending on the programmer liking. In this declaration the default constructor of class is called.


2)  Constructor Based injection: In this case the values are set using constructor of the java class. The call to constructor is made from the xml file using <constructor-arg>.  For the above mentioned java class constructor injection will be:


<?xml version=“1.0” encoding=“UTF-8”?>


<beans >      


      <bean id=“fourWheeler” class=“com.src.FourWheeler”> 

                        <constructor-arg> index=“0” value=“Honda”/>

                        <constructor-arg type=“Integer”>







In the above example the values to the bean is passed using <constructor-arg>. The sequence in which the parameters are passed should match with the constructor available in the class.


3) Static factory method: In the less common case where the container invokes a static, factory method on a class to create the bean, the class property specifies the actual class containing the static factory method that is to be invoked to create the object (the type of the object returned from the invocation of the static factory method may be the same class or another class entirely.


Consider the following method present in FourWheeler class:

public static TwoWheeler createInstance(){

            return new TwoWheeler();



Note that the method has to be static. And following line of code in xml:


<bean id=“twoWheeler” class=“com.src.FourWheeler” factory-method=“createInstance”/>


After execution of the above line twoWheleler bean will represent the TwoWheeler object which is created by calling the function from FourWheeler Class. The attribute factory-method tells which method to call present in the class mentioned. This scenario could possibly use in cases where Factory pattern is required.


4) Instance Factory method: In this case instead of having a static method a non-static method is present to create the object.


Consider the following method present in FourWheeler class:

public TwoWheeler createInstance(){

            return new TwoWheeler();



Note that the method need not be static. And following line of code in xml:


<bean id=“parentfourWheeler” class=“com.src.FourWheeler”/>


<bean id=“fourWheeler” factory-bean = “parentFourWheeler” factory-method=“createInstance” />


Here the twoWheeler object is created from a non-static method of class FourWheeler.

Use of hashcode() and equals()

Use of hashCode() and equals(). 

Object class provides two methods hashcode() and equals() to represent the identity of an object. It is a common convention that if one method is overridden then other should also be implemented.

Before explaining why, let see what the contract these two methods hold. As per the Java API documentation:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashcode() method must consistently return the same integer, provided no information used in equals() comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.
  • It is NOT required that if two objects are unequal according to the equals(Java.lang.Object) method, then calling the hashCode() method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Now, consider an example where the key used to store the in Hashmap is an Integer. Consider that Integer class doesn’t implement hashcode() method. Just for this example The code would look like:

  map.put(new Integer(5),”Value1″);
String value = (String) map.get(new Integer(5));
//Output : Value is null

Null value will be displayed since the hashcode() method returns a different hash value for the Integer object created at line 2and JVM tries to search for the object at different location.

Now if the integer class has hashcode() method like:

public int hashCode(){
return value;

Everytime the new Integer object is created with same integer value passed; the Integer object will return the same hash value. Once the same hash value is returned, JVM will go to the same memory address every time and if in case there are more than one objects present for the same hash value it will use equals() method to identify the correct object.

Another step of caution that needs to be taken is that while implementing the hashcode() method the fields that are present in the hashcode() should not be the one which could change the state of object.

Consider the example:

publicclass FourWheeler implements Vehicle {


      private String name;



      public FourWheeler(){}


public FourWheeler(String name, int purchaseValue) {

   = name;

            this.purchaseValue = purchaseValue;


      publicvoid setPurchaseValue(int purchaseValue) {

            this.purchaseValue = purchaseValue;




      publicint hashCode() {

            finalint prime = 31;

            int result = 1;

            result = prime * result + ((name == null) ? 0 : name.hashCode());

            result = prime * result + purchaseValue;

            return result;



FourWheeler fourWObj = new FourWheeler(“Santro”,”333333);
//Output: null

We can see that inspite of passing the same object the value returned is null. This is because the hashcode() returned on evaluation will be different since the purchaseValue is set to ‘555555’ from ‘333333’. Hence we can conclude that the hashcode() should contain fields that doesn’t change the state of object.

One compatible, but not all that useful, way to define hashCode() is like this:

public int hashcode(){
return 0;

This approach will yield bad performance for the HashMap. The conclusion which can be made is that the hashcode() should(not must) return the same value if the objects are equal. If the objects are not equal then it must return different value.

Overriding equals() method

Consider the example:

publicclass StringHelper {


      private String inputString;


      public StringHelper(String string) {





      publicint hashCode() {





      publicstaticvoid main(String[] args) {


            StringHelper helperObj = new StringHelper(“string”);

            StringHelper helperObj1 = new StringHelper(“string”);

            if(helperObj.hashCode() == helperObj1.hashCode()){

                  System.out.println(“HashCode are equal”);



                  System.out.println(“Objects are equal”);


                  System.out.println(“Objects are not equal”);





      public String getInputString() {




// Output:
HashCode are equal
Objects are not equal

We can see that even though the StringHelper object contains the same value the equals method has returned false but the hashcode method has return true value.

To prevent this inconsistency, we should make sure that we override both methods such that the contract between both methods doesn’t fail.

Steps that need to be taken into consideration while implementing equals method.

1. Use the == operator to check if the argument is a reference to this object.                                                                If so, return true. This is just a performance optimization, but one that is worth doing if the comparison is potentially expensive.

2. Use the instanceof operator to check if the argument has the correct type.

If not, return false. Typically, the correct type is the class in which the method occurs. Occasionally, it is some interface implemented by this class. Use an interface if the class implements an interface that refines the equals contract to permit comparisons across classes that implement the interface. Collection interfaces such as Set, List, Map, and Map.Entry have this property.

3. Cast the argument to the correct type. Because this cast was preceded by an instanceof test, it is guaranteed to succeed.

4. For each “significant” field in the class, checks if that field of the argument matches the corresponding field of this object.          If all these tests succeed, return true; otherwise, return false

5. When you are finished writing your equals method, ask yourself three questions: Is it symmetric? Is it transitive? Is it consistent?

The correct implementation if equals method for the StringHelper class could be:


      publicboolean equals(Object obj) {

            if (this == obj)


            if (obj == null)


            if (getClass() != obj.getClass())


            final StringHelper other = (StringHelper) obj;

            if (inputString == null) {

                  if (other.inputString != null)


            } elseif (!inputString.equals(other.inputString))





Google’s GDrive

The GDrive service will provide anyone (who trusts Google with their data) a universally accessible network share that spans across computers, operating systems and even devices.  Users will no longer require third party applications to emulate this behaviour by abusing Gmail storage.

In a Windows environment, most users know how to use the typical C: in “My Computer”.  Network drives work exactly the same but are given a different letter and the files within are not stored on the computer.  If my suspicions are correct and GDrive is simply a network share, most applications could take advantage of this service without modification.

The question of course is how Google will monetize a service like this.  I cannot see how file storage using a network share could be used to serve up advertisements — so maybe they won’t.  In some screenshots of Gmail for domains, it appears there are different “account plans” that I assume provide additional email addresses.  Could a similar system work for online storage?  For example, 1GB free and pay $5 for each additional.

Another way to generate income from this service would be to provide users a DVD backup of their data for a small monthly fee.  Depending on how often a GDrive is used by someone, it could make sense to receive a backup on a regular schedule — while they are at it, why not include some “cool” extras on those DVD’s?

Online storage with GDrive could be an important part of Google’s future plans — universal access to your data will soon become reality. Watch out and wait.

Program to check rectangle overlapping.

Algorithm : 

  • Two rectangles can overlap if one rectangle has 0,1,2,4 corners inside the other rectangle. The check of above mentioned condition could result is many different combinations. Remember overlapping rectangle cannot have 3 corners inside.
  • Other way we can say that two rectangle overlap if the region of one rectangle lies inside the other.

various overlapping possibilites

  • The best way to find is to identify whether an overlapping area is present or not which can be known if the below mentioned all conditions are true.

If we check that

·        The left edge of B is to the left of right edge of R.

·         The top edge of B is above the R bottom edge.

·         The right edge of B is to the right of left edge of R.

·        The bottom edge of B is below the R upper edge.

Then we can say that rectangles are overlapping.


Consider an example: There are two rectangles as shown in diagram – Black Rectangle (B) and Red rectangle(R).



If we check that

·        The left edge of B  is to the left of  right edge of  R. The selected area will be :




·        The top edge of B is above the R bottom edge. So the selected area will be:


·        The right edge of B is to the right of left edge of R. The selected area will be:






·        The bottom edge of B is below the R upper edge. The selected area will be:




Hence all conditions are true we can say that rectangles are overlapping.

Therefore we can see that all the conditions are valid and hence rectangle is overlapping.


Below is the source code attached. The logic is only present in checkOverlap() method, rest is used to display the GUI and check.


package com;


import java.awt.*;

import java.awt.event.*;


import javax.swing.*;


public class RectangleOverlap extends JPanel{


      int r1x1,r1x2,r2x1,r2x2 ;

      int r1y1,r1y2,r2y1,r2y2 ;

            int r1width,r2width ;

            int r1height,r2height ;

      static JButton btn = new JButton(“Check”);

      public RectangleOverlap(int r1x1,int r1y1,int r1x2,int r1y2,int r2x1,int r2y1,int r2x2,int r2y2){









            r1width = Math.abs(r1x1-r1x2);

            r2width = Math.abs(r2x1-r2x2);

            r1height = Math.abs(r1y1-r1y2);

            r2height = Math.abs(r2y1-r2y2);




      private void addActionListener() {

            btn.addActionListener(new ActionListener(){


                  public void actionPerformed(ActionEvent e) {






      private void checkOverlap() {

            // condition to check whether the rectangles are overlapping or not.s

            boolean isOVerlap= ((r1x2 >= r2x1) &&

                  (r1y2 >= r2y1) &&

                  (r1x1 <= r2x2) &&

                (r1y1 <= r2y2));


      if(isOVerlap ){

            JOptionPane.showMessageDialog(null, “OVerlap”);


            JOptionPane.showMessageDialog(null, “No OVerlap”);





      protected void paintComponent(Graphics g) {

            g.drawRect(r1x1,r1y1 , r1width, r1height);

            g.setColor(new Color(123,232,122));

            g.drawRect(r2x1, r2y1, r2width,r2height);



      public static void main(String args[]){

            JFrame frame = new JFrame();


// input to check overlap condition.

// the order followed as : enter coordinate for 1st rectangle as lower limit and upper limit to rectangles          

            frame.getContentPane().add(new RectangleOverlap(20,30,120,130,10,50,160,120),BorderLayout.CENTER);


            frame.addWindowListener(new WindowAdapter(){


                  public void windowClosing(WindowEvent e) {