944,216 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1682
  • Java RSS
Nov 6th, 2009
0

Boolean class

Expand 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sridhar123 is offline Offline
5 posts
since Oct 2009
Nov 6th, 2009
0
Re: Boolean class
Click to Expand / Collapse  Quote originally posted by sridhar123 ...
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:

java Syntax (Toggle Plain Text)
  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
Reputation Points: 38
Solved Threads: 8
Junior Poster
KirkPatrick is offline Offline
162 posts
since Apr 2009
Nov 6th, 2009
0
Re: Boolean class
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)
Featured Poster
Reputation Points: 1939
Solved Threads: 955
Posting Expert
JamesCherrill is online now Online
5,816 posts
since Apr 2008
Nov 6th, 2009
0
Re: Boolean class

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.
Reputation Points: 38
Solved Threads: 8
Junior Poster
KirkPatrick is offline Offline
162 posts
since Apr 2009
Nov 6th, 2009
0
Re: Boolean class
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).
Featured Poster
Reputation Points: 1939
Solved Threads: 955
Posting Expert
JamesCherrill is online now Online
5,816 posts
since Apr 2008
Nov 6th, 2009
0
Re: Boolean class
... and it always makes me wince).
++
Me too. Not only is it unnecessary, the code just reads a whole lot cleaner without them.
Java Syntax (Toggle Plain Text)
  1. if (isComplete) {...
Last edited by Ezzaral; Nov 6th, 2009 at 5:00 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 7th, 2009
0
Re: Boolean class
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
++
Me too. Not only is it unnecessary, the code just reads a whole lot cleaner without them.
Java Syntax (Toggle Plain Text)
  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 :
Java Syntax (Toggle Plain Text)
  1. if(state == false)
than
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 7th, 2009
0
Re: Boolean class
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Algorithm for creating a linked list that is sorted as nodes are inserted
Next Thread in Java Forum Timeline: Need Project Suggestion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC