| 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: 490
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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: 490
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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: 490
|
|
my mistake.
-Hunter
|
 |
 |
|
|
subject: Why do i have to set the boolean value
|
|
|