Difference between ‘==’ and ‘equals’ operator for comparison:

The comparison operator for objects may confuse some of the java novice programmers. In this article I am going to shed some thoughts on, when and where to use ‘equals’ and ‘==’ operators.

The ‘==’ operator is used to check if the two objects which are being compared are of the same object reference or not. This means to say that comparison is done on the memory location in which the objects are created / located. We use the ‘==’ operator to check whether the objects which we are being compared are the aliases or not. Object alias means, an object which is allocated on the memory and have different names to access the object location. For instance refer the sample code below:

String a = new String(“Bharath”);
 String b = new String(“Bharath”);
System.out.println(String ‘==’ operator output >—-> “ + (a == b)); // always results false, as the reference of object ‘a’ is different  from reference of object ‘b’
  

The ‘equals’ operator is used to compare the value / property contained in the comparing objects. For instance refer the sample code below:

  
String a = new String(“Bharath”);
 String b = new String(“Bharath”);
System.out.println(“String ‘equals’ operator output >—-> “ + a.equals(b));// results true because, the contents/property in both the objects ‘a’ and ‘b’ are the same.
 
example of object reference:


 

String a = new String(“Bharath”);
String b = new String(“Bharath”);
String c= b;
System.out.println(“comparison of ‘c’ and ‘b’ output >—-> “+(c==b)); //results true as object ‘c’ is reference of object ‘b’; in other words ‘c’ is an alias of ‘b’
System.out.println(“comparison of ‘c’ and ‘a’ output >—-> “+(c==a));//results false as object ‘c’ is not a reference of object ‘b’

 

 

 

The header and the footer section incorporation using Struts tiles!

Last week in my project I had an interesting situation; I had to integrate couple of navigation buttons across JSP’s. The buttons had to go into footer section of the pages. But, the existing JSP’s had no footer section. The struts-tiles is in place and had only header and body attributes defined in it. Initially, I was asked to integrate the new requirement at framework level. This decision had made my life easy all I had to do was to follow couple of steps below:

  1. Create a new footer JSP.

  2. Edit the struts-tiles configuration file and add footer attribute into definition node.

  3. If any page demanded no footer; in the value attribute for put node I had to leave it blank.

  4. Last but the least, I had to add the Javascript function for button onclick event into JSP’s.

The life was smart, but, later a decision was made not to make the changes at framework level and had to be integrated only at JSP level. Now, I had to follow few simpler steps as below:

  1. Create a new footer JSP.

  2. Edit each JSP and include the footer JSP after the body tag in JSP.

  3. Last but the least, I had to add the Javascript function for button onclick event into JSP’s.

Though the alter looks simple it’s not a wise thing to do; I thought. The processing time of JSP’s would have reduced if it was made at the framework level, as struts-tiles would have rendered the footer at server start up itself. But, this would have not been a noticeable change. To add bit more to it, the primary concept of adding of navigation into footer section itself seems irrational, as the concept of footer is lost, footer is something which is constant and needs no change when adapted across pages. So, there’s a pain of editing each JSP’s.

Thus, to conclude, I guess the alter decision was wise.

“Use header and footer when the header and footer section are constants, for example, across the pages if the menu is constant, add it into header section. Across the pages if the company details or contact info is to be displaced gracefully embrace them into footer section and it would be the wise decision to adapt them into struts-tiles by doing which we may need not edit individual pages to incorporate header or footer sections.”