Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do i have to set the boolean value

 
pammi rao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
pammi rao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my mistake.

-Hunter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic