Boolean class

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 5
Reputation: sridhar123 is an unknown quantity at this point 
Solved Threads: 0
sridhar123 sridhar123 is offline Offline
Newbie Poster

Boolean class

 
0
  #1
Nov 6th, 2009
Public final static Boolean FALSE=new Boolean(false);
Public final static Boolean TRUE=new Boolean(true);
what is the meaning of above two statements?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 115
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster
 
0
  #2
Nov 6th, 2009
Originally Posted by sridhar123 View Post
Public final static Boolean FALSE=new Boolean(false);
Public final static Boolean TRUE=new Boolean(true);
what is the meaning of above two statements?
There should be more code to go with this, otherwise it's pretty hard to tell what you are trying to accomplish.

I'm also not quite sure why someone would want to create a true/false when the point of a boolean is to determine whether it is true or false.

For example:

  1. private boolean isWindows;
  2.  
  3. if (isWindows == true)
  4. // do something only a windows os can do
  5. else
  6. // do something only a unix os can do

Obviously there are more operating system's than those two but that shows how a boolean would work. Or at least how I use my booleans instead of creating true false
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,004
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 149
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #3
Nov 6th, 2009
These two statements create two constants TRUE and FALSE (Java constants are captialised by convention). These constants are members of the Boolean class, with values true and false respectively.
Back in the old days befor Java 1.5 the compiler would not convert automatically between Boolean (the class) members and boolean (primitive) values, so it was useful to have constants for the class members. Now Java will autobox true and false into Boolean class members automatically this is no longer of any great use.

ps sridhar123:

if (isWindows == true)
is a dumb way of saying
if (isWindows)
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 115
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster
 
0
  #4
Nov 6th, 2009
Originally Posted by JamesCherrill View Post

if (isWindows == true)
is a dumb way of saying
if (isWindows)
haha agreed, unless for some reason it's set to false above

thanks for the insight as well, i wasn't aware that was the reason for creating TRUE and FALSE
Last edited by KirkPatrick; Nov 6th, 2009 at 2:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,004
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 149
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #5
Nov 6th, 2009
Originally Posted by KirkPatrick View Post
haha agreed, unless for some reason it's set to false above
No, the two statements are equivalent for both boolean values.
(This is a little obsession of mine. I guess I see
if (booleanExpression == true)
once a week or so, and it always makes me wince).
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,506
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #6
Nov 6th, 2009
Originally Posted by JamesCherrill View Post
... and it always makes me wince).
++
Me too. Not only is it unnecessary, the code just reads a whole lot cleaner without them.
  1. if (isComplete) {...
Last edited by Ezzaral; Nov 6th, 2009 at 5:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,432
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 186
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #7
Nov 7th, 2009
Originally Posted by Ezzaral View Post
++
Me too. Not only is it unnecessary, the code just reads a whole lot cleaner without them.
  1. if (isComplete) {...
Yea, sometimes, but IMO not all the times. It depends on the variable
name. When giving a variable name, if giving it isBlah doesn't
necessarily make sense, for example, suppose you have a state flag
the in that case I would rather do this :
  1. if(state == false)
than
  1. if(!state)

I am not sure which one would be faster, but I guess it depends on
many things, but I assume it would be the same in a decent
compiler.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,506
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #8
Nov 7th, 2009
Your example is only a case of poor variable naming. Why would you name a boolean "state"? What would "state equals true" mean in a logical description of the program flow? If it is truly a boolean state then you can easily assign a name that accurately reflects that binary nature.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC