Posts

Showing posts from March, 2010

Java Puzzle – Magic of Auto-boxing

  Auto-boxing is introduced in JDK 5.0.  it converts primitive data variable to corresponding wrapper classes and vice versa without any explicit efforts. For example int to Integer and Integer to int . Here is the one program using auto-boxing feature 1: class AutoBoxingTest 2: { 3: public static void main(String[] args) 4: { 5: boolean b1= true , b2= true ; 6: System.out.println(" boolean Result = "+ compare(b1,b2)+" \n "); 7: System.out.println(" Int Result = "+ compare(10,10)+" \n "); 8: System.out.println(" String Result = "+ compare(" ABC "," ABCE ")+" \n "); 9: System.out.println(" float Result = "+ compare(10.00f,10f)+" \n "); 10: System.out.println(" Mixed Result = "+ compare(10l,10l)+" \n "); 11: System.out.println(" Mixed[long,int...

XML Oriented vs Annotation oriented programming

What is XML Oriented programming ? – In this approach every application have one or set of documents associated with it. This xml documents contain application configuration information which are defines application’s run time behavior. It is very easy to maintain frequently changing configurations setting using xml document. What is Annotation Oriented Programming ? – Annotation oriented programming language leverage active code generation with the use of declaration tags embedded within the application source code to generate any other kind of source code, configuration files etc. XML vs Annotation 1. XML duplicates a lot of information like class, method name etc. But annotation are part of class it self so no duplication of information. For example web.xml <servlet> <servlet-name> DemoServlet </servlet-name> <servlet-class> demo.servlet.DemoServlet </servlet-class> </servlet> <servlet-map...

Understanding volatile keyword in java in details

What is Atomic ? The term atomic is related to the atom, once considered the smallest possible unit. When computer code is considered atomic it can not be interrupted during its execution, We can also define atomic code as code that can’t be found in an intermediate state. Atomicity in Java - Java specifies that basic loading and storing of variable is the atomic. That means the value of the variable can’t be found in a interim state during the store not be changed in the middle of loading the variable to a register. Java Memory Model – Unfortunately , java’s memory model is a bit more complex. Threads are allowed to hold the values of variable in local memory. In that case, when one thread changes the values of the variable, another thread may not see the changed value. This is particularly in loops where looping is controlled by a variable. The lopping may be have already loaded the value of the variable into the register and does not necessary notice when another thread cha...

Java Puzzle – Method Overloading Part 2

This is with reference with previous block, but next level. Program public class OverloadingPuzzle { public void display(Object obj){ System.out.println("Object version"); } public void display(String str){ System.out.println("String Version"); } public void display(Integer str){ System.out.println("Integer Version"); } public static void main(String[] args){ OverloadingPuzzle obj = new OverloadingPuzzle(); obj.display(null); } } Now I am confused, will it print String Version or Integer Version. Output like me Compiler is also confused here, there will be compilation error Uncompilable source code - reference to display is ambiguous, both method display(java.lang.String) in testproject.OverloadingPuzzle and method display(java.lang.Integer) in testproject.OverloadingPuzzle match

Java Puzzle – Method Overloading

Program - public class OverloadingPuzzle {     public void display(Object obj){         System.out.println("Object version");     }     public void display(String str){         System.out.println("String Version");     }     public static void main(String[] args){         OverloadingPuzzle obj = new OverloadingPuzzle();         obj.display(null);     } } I was expecting that this program prints “ Object Version ” but …. Output String Version Why ? – Searching ….

Composite Design Pattern by example

Image
This post explains Composite design pattern with an example. When to use ? – Use Composite pattern when you have treat a collection of objects as they were once one thing. With Composite design pattern it is very easy to simplify operations over collection data structure. It reduces complexity of the code required if you were going to handle collection as special case. How to Use? - The Composite pattern consist of Component, Leaf and Composite Classes as shown below class diagram Component – Component declares the common interface that both the single and composite nodes will implements Leaf – The leaf class represent the singular atomic data type implementing he component interface.     Composite – Composite is an abstract entity which declares common functionality which are needed by concrete implementation for handling collection data. An Example -   Item.java public interface Item {     public double getPrice(); ...

JSF 2.0 By Example

Image
This is a hands on experience with JSF with 2.0 specification. Here JSF framework is explained using simple login example using NetBean 6.5. First of all What is JSF ? A server side user interface component framework for Java technology based web application. It is based on popular MVC framework and compatible with large enterprise applications as well as mobile device based application. It supports customizable UI components which are capable of rendering them self in html browsers as well as WAP devices. Here is the step by step approach 1. Create a Java Web Application and select JSF framework using new project wizard Netbean 6.5 supports latest version of JSF that is 2.0, but you can select previous version using ‘Registered Library” option on framework selection step of wizard. By default wizard creates following files in your application a. web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0"...