Thread: Booleans

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Booleans


    Hi

    i am new to java. while studying java i was not able to understand one the line in below code.
    Code:
    Demonstrate boolean values.
    class BoolTest {
    public static void main(String args[]) {
    boolean b;
    b = false;
    System.out.println("b is " + b);
    b = true;
    System.out.println("b is " + b);
    // a boolean value can control the if statement
    if(b) System.out.println("This is executed.");
    b = false;
    if(b) System.out.println("This is not executed.");
    // outcome of a relational operator is a boolean value
    System.out.println("10 > 9 is " + (10 > 9));
    }
    }

    here i want to know why below line is not printing"This is not executed"

    if(b) System.out.println("This is not executed.");

    thx
    japper
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,597
    Rep Power
    1906
    I expect the previous text was printed out ("This is executed"). Look at what had been changed after that.

    I.e. what is the value of b? What should the value of b be to print it out?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0
    sorry not getting your point

    also what is if(b) is doing here
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,597
    Rep Power
    1906
    Please look at the documentation for if-statement.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0
    what i understand now is

    before below line the value of b is false
    if(b) System.out.println("This is not executed.");

    and if the value is false then the statement will not print the line in case of IF statement

    am i write?
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,597
    Rep Power
    1906
    That is correct.

IMN logo majestic logo threadwatch logo seochat tools logo