wood burning stoves*
The moose likes Beginning Java and the fly likes Why do i have to set the boolean value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do i have to set the boolean value" Watch "Why do i have to set the boolean value" New topic
Author

Why do i have to set the boolean value

pammi rao
Greenhorn

Joined: Feb 24, 2010
Posts: 7
This a sample program

class tape{

boolean canrecoed=false;
void plaeit()
{
S.O.P("play the tape);
}

void recordit()
{
S.O.P("recoed tape");
}
}
class taping{
public static void main (String[] args)
{
tape t = new tape;
t.canrecord=true;
t.playit();

if(t.canrecord==true)
{
t.recordit();
}
}
}


i want to know why i set the canrecord initially to false and then set it true again. Have seen this procedure in a l;ot of programs .I dont know the technicality behind this .

Thank you.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Why does a dog lick himself?? Because he can ;-)

You can set the bool to false, true or leave the default value (which is false for boolean). It depends on what you think should be the default value for the variable.

Oh and by the way. Please use code tags and don't use abbreviations such as S.O.P.


JDBCSupport - An easy to use, light-weight JDBC framework -
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

A lot of times booleans are used as conditions in loops and if statements, you use your boolean an a condition in your if statement. If you never set canrecord to true, then you would never be able to record the tape in your example.

Another examples of boolean conditions is in a while loop:




-Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to JavaRanch.

Please UseCodeTags when you post source code, and PostRealCode (instead of a sketch with "S.O.P" instead of "System.out.println", for example).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
pammi rao
Greenhorn

Joined: Feb 24, 2010
Posts: 7
i know if i dont set it to true later on the dvd does not record . however why intially its initialized to false was something i was thinking about . Is the default not already false ?
Well i think i got the bigger picture though
thank you
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Booleans in Java can be three values: true, false, or null.
You are correct in that booleans are initialized to false, but I think its good programming practice to initialize them as false.
What if someone whose background is in a different language is examining your code? they would have to guess at the initial value of the boolean.

-Hunter
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Hunter McMillen wrote:Booleans in Java can be three values: true, false, or null.

That's not true.

boolean is a primitive type. A boolean variable cannot be null; it can only be true or false.

There is the Boolean wrapper class (note, the name begins with an upper-case B). Variables of type Boolean are like other reference variables: they can refer to a Boolean object, or they can be null.

The default value of a Boolean variable is null (not: false).
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

my mistake.

-Hunter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do i have to set the boolean value
 
Similar Threads
operators
Assignment Q
&& and ||
compiler error
Boxing conversions question