Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: What's wrong with this boolean?

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default What's wrong with this boolean?

    I thought I understood how the boolean logic worked in Java perfectly fine.... but recently when I was writing some code I noticed something wasn't working well with my boolean variable. So, wrote a quick test code to see if my idea of how booleans work was indeed correct. It wasn't....

    In the below program, the idea was to have the loop break after one iteration because the boolean variable would be changed to false. It didn't work though. The JVM outputs infinite "In the loop" prints.

    So what did I miss with the boolean concept? I would appreciate the help!

    public class LoopTest {
      public static void main(String[] args) {
        boolean isRunning = true;
        while(isRunning = true) {
          System.out.println("In the loop");
          isRunning = false;
        }
      }
      System.out.println("Out of the loop");
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's wrong with this boolean?

    while(isRunning = true) {
    I believe this error is on the list of classic beginner errors.
    = is assignment, == is the comparision operator

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: What's wrong with this boolean?

    I thought == didn't work for boolean

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's wrong with this boolean?

    I thought
    Have you tried it?

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: What's wrong with this boolean?

    Quote Originally Posted by Norm View Post
    I believe this error is on the list of classic beginner errors.
    = is assignment, == is the comparision operator
    I tried making the suggested changes:
    public class LoopTest {
      public static void main(String[] args) {
        boolean isRunning = true;
        while(isRunning == true) {
          System.out.println("In the loop");
          isRunning = false;
        }
      }
      System.out.println("Out of the loop");
    }

    The compiler returned the following errors:
    javac LoopTest.java
    LoopTest.java:9: <identifier> expected
      System.out.println("Out of the loop");
                        ^
    LoopTest.java:9: illegal start of type
      System.out.println("Out of the loop");
                         ^
    2 errors

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's wrong with this boolean?

    Check the placement of statement flagged as in error.
    Where is it? What method is it in?

  7. The Following User Says Thank You to Norm For This Useful Post:

    bgroenks96 (June 6th, 2011)

  8. #7
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: What's wrong with this boolean?

    Quote Originally Posted by Norm View Post
    Check the placement of statement flagged as in error.
    Where is it? What method is it in?
    Whoops... that's a major noob error. Fixed it and it works!

    Just to wrap up though, I use the comparison operator when stating boolean loop, and then the assignment operator when I want it redefined in order to end the loop?

    And +1 thanks for you.

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: What's wrong with this boolean?

    You use == when comparing primitives (int, long, double etc). You use equals method when comparing Objects. However you can use == to compare Objects when you want to know if two variables reference the same object.

    Since booleans are primitives you can use == but it is more widely accepted to not use comparisons at all. Since booleans have a true or false value then you just use the boolean in the condition.
    boolean bool1 = true;
    boolean bool2 = false;
    if(bool1)
    if(! bool2)

  10. #9
    Banned
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with this boolean?

    idk i cant read it

Similar Threads

  1. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 10:18 AM
  2. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  3. [SOLVED] Can't get boolean to change!!
    By Day2Day in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2010, 07:20 PM
  4. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM
  5. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM

Tags for this Thread