Hi,
Welcome to my first Java tutorial. In this tutorial I will teach you the basics of booleans.
First of all,
What is a boolean?
A boolean is a variable used in the JAVA programming language which stores a true or false value.
When do I use booleans?
You can use a boolean when you want to check if something is in the right place or to check if something is right or wrong(true or false), for example :
That's an example of how we could use a boolean.
Now a boolean can store two values, "true", "false". We can use them in many different ways.
To start off you need to declare and instiliaze the boolean :
We can now use the isItThere boolean.
We could use it in an if, while, almost any kind of statement.
For example :
This will check the mouse coordinates and give the variable a true or false value depending on where the mouse is.
Another way we can use this is....
If we want to check if something is true, i.e the mouse is in the correct position, then we could use some code as seen below :
Now to use the variable we could do something like :
Well I hope you understood some of that as it is the basics to using booleans in Java.
Enjoy!
Welcome to my first Java tutorial. In this tutorial I will teach you the basics of booleans.
First of all,
What is a boolean?
A boolean is a variable used in the JAVA programming language which stores a true or false value.
When do I use booleans?
You can use a boolean when you want to check if something is in the right place or to check if something is right or wrong(true or false), for example :
Code:
public class test {
public boolean isHeOld = false;
public int age = 67;
public void isHe() {
if(age > 50) {
isHeOld = true;
} else {
isHeOld = false;
}
}
That's an example of how we could use a boolean.
Now a boolean can store two values, "true", "false". We can use them in many different ways.
To start off you need to declare and instiliaze the boolean :
Code:
public class test {[COLOR="Red"]
public boolean isItThere = false;[/COLOR]
}
We can now use the isItThere boolean.
We could use it in an if, while, almost any kind of statement.
For example :
Code:
public class Test {
public boolean isItThere = false;
[COLOR="Red"]public void isHe() {
int currMouseX = MouseInfo.getPointerInfo().getLocation().x;
int currMouseY = MouseInfo.getPointerInfo().getLocation().y;
SpinnerNumberModel model1 = (SpinnerNumberModel) x1.getModel();
SpinnerNumberModel model2 = (SpinnerNumberModel) y1.getModel();
int xInt1 = model1.getNumber().intValue();
int yInt1 = model2.getNumber().intValue();
if(currMouseX == xInt1 && currMouseY == yInt1) {
isItThere = true;
} else {
isItThere = false;
}
}[/COLOR]
}
This will check the mouse coordinates and give the variable a true or false value depending on where the mouse is.
Another way we can use this is....
If we want to check if something is true, i.e the mouse is in the correct position, then we could use some code as seen below :
Code:
public boolean atP1() {
int currMouseX = MouseInfo.getPointerInfo().getLocation().x;
int currMouseY = MouseInfo.getPointerInfo().getLocation().y;
SpinnerNumberModel model1 = (SpinnerNumberModel) x1.getModel();
SpinnerNumberModel model2 = (SpinnerNumberModel) y1.getModel();
int xInt1 = model1.getNumber().intValue();
int yInt1 = model2.getNumber().intValue();
return (currMouseX == xInt1 && currMouseY == yInt1);
}
This means that if the current mouse position = the xInt1 and yInt1 then it will return true. We can call on these methods like such,
[CODE]
if(atP1()) {
//We're there and it returned true
}
else if(!atP1()) {
//We're not there and it returned false
}
Now to use the variable we could do something like :
Code:
public class Test {
public boolean isItThere = false;
public void isHe() {
int currMouseX = MouseInfo.getPointerInfo().getLocation().x;
int currMouseY = MouseInfo.getPointerInfo().getLocation().y;
SpinnerNumberModel model1 = (SpinnerNumberModel) x1.getModel();
SpinnerNumberModel model2 = (SpinnerNumberModel) y1.getModel();
int xInt1 = model1.getNumber().intValue();
int yInt1 = model2.getNumber().intValue();
if(currMouseX == xInt1 && currMouseY == yInt1) {
isItThere = true;
} else {
isItThere = false;
}
}
[COLOR="Red"]public void doStuff() {
if(isItThere == true) {//We could also do "if(isItThere) {" - Do the same thing
System.out.println("We're There");
}
if(isItThere == false) {//We could also do "if(!isItThere) {"-Do the same thing
System.out.println("We're not there");
}[/COLOR]
}
Well I hope you understood some of that as it is the basics to using booleans in Java.
Enjoy!




