Booleans


Collapse Content

Booleans
One of the 4 basic types was the boolean data type, which can store either true or false values. As with ints and Strings, you need to declare the type (boolean) and name, and can assign it a value(true or false). For example:

boolean alive = true;
boolean hungry = false;
boolean haveMoneyLeft = true;

Notice the booleans are not Strings (which would be in quotes), but their own basic values: true and false.

Assigning booleans based on expressions
Often, you don't directly assign true or false to a boolean, but instead provide it with the result of a comparison. For example:

boolean haveMoneyLeft = yourMoney > yourDebts;

yourMoney > yourDebts is a comparison which will return either true or false. That result will then be stored in the boolean variable haveMoneyLeft. This boolean can then be used later.

Using boolean variables in conditional expressions
In previous nodes, we showed how you can use if, while and for statements to execute code only when a certain comparison is true, such as:

if(yourMoney > yourDebts){
    buyStuff();
}

Sometimes you may want to store the results of a comparison to use later. You can use the boolean data type to do so. For example:

boolean haveMoneyLeft = yourMoney > yourDebts;
if(haveMoneyLeft){   //this boolean will be true or false
  buyStuff();       // this will only get executed if haveMoneyLeft is true
}

The boolean haveMoneyLeft can then be used multiple times (so one can check if there's still money without rewriting the whole comparison). However, if 'yourMoney' or 'yourDebts' changes in value, 'haveMoneyLeft' will not automatically check if it should change in value too, so you may need to run the original comparison again then.

Not
If you only wanted to run code if a boolean was false, you could write an equality to check that. For example, to see if haveMoneyLeft was false, you could check
if(haveMoneyLeft == false).
You can also use the NOT operator to get the opposite of a boolean. In Java, an exclamation point is used for NOT: !.
For example:

if( ! haveMoneyLeft){
 declareBankruptcy();
}

Challenge

You will be given two integers a and b. Return true if a is a multiple of b, and false otherwise.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • This error was returned. What is the problem with this syntax?
    Main.java:6: error: unexpected type
    return ((a%b) = 0);
    ^
    required: variable
    found: value
    1 error

  • = is an assignment, you probability want an equality check ==

  • I didn't finish reading the whole lesson but what if
    {
    boolean haveMoneyLeft = yourMoney > yourDebts;
    }

    What if I have a debt of 500gold and yourMoney is 500gold too
    this makes:
    {
    boolean haveMoneyLeft = false;
    }
    right ?

  • Nice this was an easy one. thanks again for the great site.

  • import java.util.Scanner;
    public class BooleanExm
    {
    public static void main(String[] args)
    {
    double a;
    double b;
    Scanner input = new Scanner(System.in);

    cont...
  • import java.util.println;
    public class Main(){
    public static void main(String[] args){
    int a = 4;
    int b = 2;
    if(a%b==0){
    System.out.println("true");
    }else{
    System.out.println("false");
    }

  • import java.util.Scanner;
    public class Main(){
    public static void main(String[] args){
    int a = 4;
    int b = 2;
    if((a%b)==0){
    System.out.println("true");
    }else{
    System.out.println("false");
    }
    }
    }

  • Keep it simple, the answer is right there.....
    boolean x;
    if(a%b ==0){
    x = true;
    }
    else{
    x=false;
    }
    return x;
    }

  • In my solution, I used only =, not == , and I got a correct response. Can you tell me why that is? Thanks.

All Node Comments
Contact Us
Sign in or email us at [email protected]