This discussion is archived
3 Replies Latest reply: Sep 26, 2009 5:24 AM by 699554 RSS

boolean problem

843789 Newbie
Currently Being Moderated
Hi!
public static void main(String[] args) { 
boolean test = false;


test = (17 >= 16);


System.out.println(test);
 if(test == true) 
 System.out.println(" correct"); 
 else 
 System.out.println(" False");
}
}
I have problem with this code.How dose this condition " test = (17 >= 16)" work.
When I print out the program I get true and correct. Why do I get "true" I do not have
any System.out.println f?r "true".
I try to understand the program.
  • 1. Re: boolean problem
    699554 Newbie
    Currently Being Moderated
    Initially you assign test the value of false. Later you assign the value of (17 >= 16) to test before printing the value of test.

    Is 17 >= 16 ?

    Mel
  • 2. Re: boolean problem
    843789 Newbie
    Currently Being Moderated
    Hi aaabbse@yahoo.com ,

    It is not the way to test a boolean :
    if (test) {
    ...
    is sufficient.
  • 3. Re: boolean problem
    699554 Newbie
    Currently Being Moderated
    p.s. don't be afraid to use meaningful messages in println statements. It may seem tedious at the time but as a program grows, understanding what "correct" or "false" is may juggle the human brain.
    public static void main(String[] args) { 
      boolean test = false;
      test = (17 >= 16);
      System.out.println("The value of test is: " + test);
      if(test) 
        System.out.println("Test is correct"); 
      else 
        System.out.println("Test is False");
    }
    Mel