Not, they are not regular flags!. Flags are boolean variables that are either true or false.
What are boolean variables?
boolean variables are part of the set of primitive types in java that are:
int, double, String, boolean and etc.
As you probably guessed already these boolean variables are also called flags.
why are they called flags?
The reason why they are called flags is because when they are used to the behavior control a control structure such as condition remember my post about conditions? I will attach a link to the post at the end of this post. This is the general format of when flag/boolean variable is used to control a condition in java:
execute these statements if the is true
The format below tests it out if the flag is true execute the statements inside the curly brackets.
To test out for when the flag is false there is two general ways to do this:
First way(most effective)
one can put a ! in front of the boolean variable like this: !flag This is how to tell java when looking for when the flag is false. The following example shows this in a example:
if(!test)
{
Block of statements;
}
Second Way(Just as effective but not recommended!)
In this case the else{} block of statements represents the !flag because as you recall anything else that is not the flag being true and that only possibility is !flag or false.
import java.util.*;
/**
This class shows how a flag can be used in
a program. This is done by asking the user his/her age
If the age of the user is less than 0
then the flag is set to false
*/
public class Flag
{
public static void main(String[] args)
{
boolean test = false; // declare the flag
int age; // to hold the age of the user
Scanner keyboard = new Scanner(System.in); // construct a Scanner object to get input from the keyboard
System.out.println(“enter your age:”);
age = keyboard.nextInt();
if(age>0)
{
test = true;
}
if(age<0)
{
test = false;
}
if(test)
{
System.out.println(“The age is valid “);
}
if(!test)
{
System.out.println(“the age is invalid”);
}
}
}
The program above is flagged or controlled by it by the test boolean variable. Basically if the age is below zero than test is set to false. Make sense doesn’t it?. If the age is greater than zero then it is valid. Whatever the status is it is displayed to the user. Notice that the !flag operator is used in this program.
To summarize this concept that may seem blurry to you
Flags are variables that are either true or false.
Post something to tumblr ;)
Talk to you guys on the next post!
PEACE!
This link explains conditions:
http://thebigg-v3.tumblr.com/post/83236114365/conditions