Thursday, December 30, 2010

sonar 2.4 and 2.5

Excited about Sonar's new features to set Architectural rules in version 2.4, and upcoming Track violations over time, differential views, manual code review in 2.5.

This surely will make my life little easier when doing daily code reviews.

http://www.sonarsource.org/roadmap/
http://www.infoq.com/news/2010/12/sonar-2.4

Thank you sonar source

Wednesday, December 22, 2010

Java Autoboxing - Auto-unboxing

Java 1.5 onward auto-boxing and auto-unboxing were added. This allows you to covert primitives and boxed types primitives back and forth with simple assignment vs calling special functions or constructors.

Lets look at some examples.
int primitiveInt = 1000;                   //primitive int
Integer wrapperInt = new Integer(1000);    // wrapper Integer, boxed type primitive.
Integer autoBoxedWrapperInteger = 1000;    // Autoboxing, primitive is autoboxed to Integer, an
//Object is bing created
int autoUnboxedPrimitiveInt = wrapperInt;  //Auto-unboxing, primitive is being assigned a value by auto-unoxing

//Comparing a primitive to boxed type, boxed type is autoboxed to primitive so the results is
// two primitives are being compared using === as opposed to 2 Objects are being ref compared
// using ==
System.out.println("primitiveInt == wrapperInt  :" +
(primitiveInt == wrapperInt ? true : false));

//Two object references are being compared using ==, therefore the result is false
System.out.println("wrapperInt == autoBoxedWrapperInteger  :" +
(wrapperInt == autoBoxedWrapperInteger ? true : false));

System.out.println("primitiveInt == autoBoxedWrapperInteger  :" +
(primitiveInt == autoBoxedWrapperInteger ? true : false));

System.out.println("primitiveInt == autoUnboxedPrimitiveInt  :" +
(primitiveInt == autoUnboxedPrimitiveInt ? true : false));


Output

primitiveInt == wrapperInt  :true

wrapperInt == autoBoxedWrapperInteger  :false
primitiveInt == autoBoxedWrapperInteger  :true
primitiveInt == autoUnboxedPrimitiveInt  :true

Here are some tips based on reading from Effective Java 2nd edition.

  • Prefer primitives over boxed primitives.

  • When you mix primitives and boxed primitive in single opeartion - boxed-primitive is auto-unboxed.

  • When mixing primitives and wrapper types in expression watch out for "==" comparision.

  • Performance hit due to autoboxing and unboxing, a new object is created when autoboxing is performed.

  • Unboxing can throw NullPointerException

Wednesday, December 15, 2010

HTTP Notes

Keep alive/Persistent Connections : HTTP 1.1 introduced keep alive to keep the connection between web client and server alive and reuse it to serve subsequent requests. In HTTP 1.0 a new connection had to be created for serving each request.

Servlet 3.0 Asynchronous Processing

Came across a great post on Servlet 3.0 asynchronous processing at http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=1

Following are some notes based on the post.

Important thing to note is that it is not a fire and forget mechanism, as in put a request on a queue and return the response right away. The web client  has to wait however long the request processing take place.

It is a way to free up request handling server threads from long running code. Instead the work in offloaded to be handled by manually created thread(s) by application code.

Client response stream can be open for extended period of time, and server can write to response to implement "push" model.

 

 

 

 



Following are some of the important methods from ServletRequest API from Servlet 3.0 .

AsyncContext getAsyncContext()
Gets the AsyncContext that was created or reinitialized by the most recent invocation of startAsync() or startAsync(ServletRequest,ServletResponse) on this request.

boolean     isAsyncStarted() Checks if this request has been put into asynchronous mode.
boolean  isAsyncSupported() Checks if this request supports asynchronous operation.
AsyncContext     startAsync() Puts this request into asynchronous mode, and initializes its AsyncContext with the original (unwrapped) ServletRequest and ServletResponse objects.
AsyncContext     startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
Puts this request into asynchronous mode, and initializes its AsyncContext with the given request and response objects.