• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean Vs boolean  RSS feed

 
Varadhan Sesharaman
Ranch Hand
Posts: 30
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Can any one post me a simple eg code to illustrate the diffrence between Boolean and bollean.

That is object and prmitive date type.

How this both differ in usage.


Thanks
 
Sarah Mitchell
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm not expert but I know for sure that you can use Boolean as a variable or object, but you cannot use boolean since it's a reserved word for the variable class of primitive data type !

and since you asked for an example code, I made this for you, it's the simplest thing I could come up with :



as you can see, I used the class boolean to define a variable named Boolean, and in the same way you could used Boolean as an object( I think, lol).

I hope this helps in clearing thins up a little bit .





Good Luck
 
Mohammed Uddin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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).

Mohammed
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Like:



And:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Phew. HIH.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!