Thursday, June 30, 2011

Function Inlining and Final methods

In C++ and C# methods donot use polymorphism until unless asked to do so using virtual and override constructs as below.



using System;

public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}

using System;

public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}




However in java this is not the case, it is not optional.
However using the final keyword in your code you can tell the compiler that what methods are not allowed to be overridden.

Java compilers depending on their implementations use various techniques for performance optimization, function inlining being one of them i.e.

if a method is not overridden and it is short then the complier can optimize the method call be means od inlining.

e.getName() can be replaced with e.name

This is a major improvement if you think in terms of CPU. CPU's have prefecthing stratery builtin to speed up the FETCH-DECODE-EXECUTE cycle and it hates branching. So code inlining is definately is a speedup.

Fortunately JIT compilers in VM knows exactly which class overrides a given class and they can check whether any class overrides a given method, and hence decide for inlining.

In cases where you are sure that a methods needs not to be overridden, you can avoid this lookup activity/overload by declaring a method as final.

To sum up it makes a lot of sense if you declare all you methods as final unless you have a good reason that you want polymorphism.

Wednesday, June 8, 2011

Maven + MyEclipse the first view

For the first i tried my hands time on maven2. It is such a wonderful tool and using it with MyEclipse is such a charm.

It is so easy to setup a maven project on MyEclipse. All you need to do is to create a JavaMavenProject. Name is XXX(whatever you want your project to be named). I have named mine as hibernatemaven. It will create a directory structure like below.



Looking at POM.xml (heart and soul of maven), shows



i.e. by default maven takes JUNIT dependecy to the project

Maven uses declarative dependency i.e. Maven itself take care of finding and downloading the required jars of the particular dependency you mention in POM.xml. The dependency resolution is iterative, maven will search for child dependencies as well.

Now when i added following to POM.xml



and made build the project in MyEclipse. All the dependencies of hibernate were added to maven dependencies (mentioned in the directory structure).

One can find the dependency xml entry from mavenrepository.com as i found hibernate dependency entry from http://mvnrepository.com/artifact/org.hibernate/hibernate-core/3.6.4.Final

In additiion to above maven has it repository on your local machine bydefault it can be found at.{user_home}/.m2

All dependency jars are referred from there only.

On the whole my first experience with maven was nice, until next time.