• Log in or sign up for an account here: https://www.sythe.org/login

Tutorial On Boolean's

iJava

.Previously known as RSGoldRush
Joined
Nov 21, 2011
Sythe Gold
491
Messages
1,204
Tutorial On Boolean's
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 :

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!
 
Tutorial On Boolean's
Two things quick issues:
  • You should code to the java coding conventions
  • Booleans and other primitive types can't be null
 
Tutorial On Boolean's
Two things quick issues:
  • You should code to the java coding conventions
  • Booleans and other primitive types can't be null

I whipped this up fast so it may not follow the conventions. I fixed the second as I got confused with a String, don't ask me why.
 
Tutorial On Boolean's
How on earth is someone going to understand the code you gave if they don't understand what a boolean is?
 
Tutorial On Boolean's
How on earth is someone going to understand the code you gave if they don't understand what a boolean is?

I gave a brief explanation and they can always use Google.
 
Tutorial On Boolean's
No, look http://lmgtfy.com/?q=What+Is+A+Boolean+Java now click the first link and it has a greater explanation on a boolean. If someone is going to learn Java then they should be willing to work hard.

So you're basically saying there is no purpose to your tutorial as it would be better to google "what is a boolean"?

Edit: I'm not going to post anymore, as I hate it when people argue on my threads - so I won't on yours - it was just a suggestion to keep it simple rather than over-complicating a simple issue.
 
Runescape Gamblingrunescape gambling
Back
Top