Just to add, I think that Boolean is an object unlike boolean which is a reference variable of Boolean. Since Boolean is an object it can be used as an element when passing objects to methods as opposed to references copies (although as of Java 6 auto boxing takes care of everything even if you pass boolean which expects an object type of Boolean).
Correct, the Boolean object is a wrapper object which allows you to assign boolean literals to via autoboxing, and use the value of the object using auto-unboxing.
Also remember that any object, and therefor also Boolean objects, can be null!
Another advantage (I think) is that you can use the Boolean object in generic typing, in which primitive types are not allowed.
Varadhan Sesharaman wrote:Can any one post me a simple eg code to illustrate the diffrence between Boolean and bollean.
Boolean is a bit of an oddity. In theory, there really should be only two possible objects:
Boolean.TRUE and Boolean.FALSE but in fact it's quite possible to create your own, viz:
Boolean myBoolean = new Boolean(true);
Also, checking a Boolean generally involves unboxing it; either explicitly:
if (myBoolean.booleanValue()) ... or implicitly
if (myBoolean) ... both of which involve added time (miniscule, but may become significant if used jillions of times).
It is possible to use
if (myBoolean == Boolean.TRUE) ... but ONLY if you know that 'myBoolean' can only contain one of the two provided constants (which will be true if it was created with Boolean.valueOf() or by autoboxing of a boolean value).
And then of course, a Boolean can be null...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here