Thursday, November 22, 2012

Difference between "==" operator and equals() method


Difference between "==" operator and equals() method

The “==” operator:

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location. A very simple example will help clarify this:

By definintion, the objects are all created on the heap. When you create an object, say,

Object ob1 = new SomeObject();
Object ob2 = new SomeObject();

We have 2 objects with exactly the same contents, lets assume. We also have 2 references, lets say ob1 is at address 0x1234 and ob2 is at address 0x2345. Though the contents of the objects are the same, the references differ.

Using == compares the references. Though the objects, ob1 and ob2 are same internally, they differ on using this operation as we comare references. ob1 at address 0x1234 is compared with ob2 at address 0x2345. Hence, this comparison would fail.

object.equals() on the other hand compares the values. Hence, the comparison between ob1 and ob2 would pass. Note that the equals method should be explicitly overridden for this comparison to succeed.

The equals() method: 

The equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.

The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory. This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal. Here is an example that will help clarify this:

String obj1 = new String("xyz");

String obj2 = new String("xyz");

if(obj1.equals(obj2))
   System.out.printlln("obj1==obj2 is TRUE");
else
  System.out.println("obj1==obj2 is FALSE");

Java’s String class overrides the equals() method to compare the characters in a string. This means that the comparison between the 2 String objects returns true since they both hold the string “xyz”.

Monday, November 12, 2012

Build Tools


Build tool allows developers and build managers to describe the build process also build tools help the programmer to build their project efficiently. 

Build tools are programs that automate the creation of executable applications from source code. Building incorporates compiling, linking and packaging the code into a usable or executable form.

Build process is repetitive process where programmer builds the project again and again to test the changes in the project code. While developing a project code changes needs to be test and the testing requires the build of the code and finally deploying on the server for testing. Build tools automate the repetitive process and allows the programmer to concentrate on the project development. It also reduces the overall build time. Build tools makes the build process just a single click work. 

In summary, a build tool provides a mechanism to describe the operations and structure of the build process. When the build tool is invoked it uses the description, examines the current state of affairs to determine the steps that need to be executed and in which order, and then manages the execution of those steps.

There are many build tools are available. Followings are few Java build tools available.
  • Rake
  • Ant
  • Maven