Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Java programming question. Boolean

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Java programming question. Boolean

    Hi guys.

    So i was told if i wanted to check a boolean, if it is true or false. I could do it as follows

    boolean end = false;

    Code:
    while(!end)
    And that this would check if the boolean is false

    Code:
    while(end)
    And this if it is true.

    But i was wondering, how is it that you can skip the != and == signs in each loop. Is this just something that Java have implemented? Or is their a higher meaning. And can this only be done with boolean's or can it also be used in other situations? For loops, if then statements and so on.

    Kind regards.

  2. #2
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java programming question. Boolean

    both cases evaluate to a boolean so it's the same thing really.

    i tend not to do:

    Code:
    while(!end)
    but do

    Code:
    while(end == false)
    It's easier for a pleb like me to understand it when i re-read it again six months down the line.

  3. #3
    Join Date
    Jul 2007
    Location
    Brisbane Australia
    Beans
    128
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: Java programming question. Boolean

    It is the same in c/c++. I don't know about java, but in c you can use the same construct to check an integer value for zero or non-zero where zero=false, any other value =true.

    Sometimes you might use the same for a pointer, declare a pointer and set it to NULL and you can test it with the same boolean construct and it will be false, if it has a value it =true.

    And yes ... it should work for if for and repeat statements. Any sort of boolean test.


    lol @ your next comment muteXe
    Last edited by davetv; August 1st, 2012 at 10:07 AM.

  4. #4
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java programming question. Boolean

    Quote Originally Posted by davetv View Post
    It is the same in c/c++. I don't know about java, but in c you can use the same construct to check an integer value for zero or non-zero where zero=false, any other value =true.
    thankfully this type of thing is not allowed in java.

  5. #5
    Join Date
    Jul 2007
    Location
    Brisbane Australia
    Beans
    128
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: Java programming question. Boolean

    Hi muteXe ... why is that bad? Your "thankfully" comment gives me the vibe. Don't you think that is a versatile use of that construct?

    I don't see it as confusing as long as you know the typedef and history of the tested variable.
    Last edited by davetv; August 1st, 2012 at 03:23 PM. Reason: kmow is not a cromulent word

  6. #6
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java programming question. Boolean

    The while loop condition only requires and expression that returns a boolean. There are many ways to have that happen, and not all of them involve an equals/not equals sign. This is a simple case, where just the variable's name gives you a boolean value, which the while loop checks for true/false. You can also do things like negate it, use a boolean and/or on it with other expressions, or return a boolean from a function (eg, while(iter.hasNext()).

    The situation in C is slightly different. C has no innate boolean class, so the while loop condition looks for integers. If the integer is 0, the condition evaluates to false. Otherwise, it is true. C implicitly "casts" (reinterprets) a lot of things to int, which lets you put a few more things in the condition expression. Any numeric type checks whether it equals 0, and any other type casts to int and checks if that value is 0. In most cases this means checking if a pointer is null or not. C++ has a similar situation for primitive types because it is (mostly) a superset of C.
    Last edited by schauerlich; August 1st, 2012 at 03:39 PM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  7. #7
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Java programming question. Boolean

    Quote Originally Posted by schauerlich View Post
    C implicitly "casts" (reinterprets) a lot of things to int
    I know I should probably let this go, but casts are always explicit. The word you're looking for is "converts".

    Code:
    int i = (int) 1.0; /* this line contains a cast (explicit conversion) */
    int j = 1.0; /* this line relies on implicit conversion and doesn't have a cast */

  8. #8
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java programming question. Boolean

    Just to add an example to extend to what schauerlich quite correctly is saying:

    If you have 2 ints, variable1 and variable2, in C or C++ this is perfectly allowable:

    Code:
    if(variable1 - variable2)
    {
        // do some stuff --- A
    }
    else
    {
        // do some otherstuff   --- B
    }
    so you are 'if'ing on an integer (not a true or false), and if variable1 and 2 were the same then you'd get a if(0) i.e. it would evaluate to false and path B would execute.

    This kinda thing wouldn't even compile in java. It expects, in an 'if', a built-in boolean type.

  9. #9
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Java programming question. Boolean

    Quote Originally Posted by schauerlich View Post
    C implicitly "casts" (reinterprets) a lot of things to int, which lets you put a few more things in the condition expression. Any numeric type checks whether it equals 0, and any other type casts to int and checks if that value is 0.
    No! In C, when you write

    Code:
    if (foo)
    it is interpreted as

    Code:
    if (foo != 0)
    and then the usual rules for comparison apply. In some cases, there will be a conversion (not a cast!), but not always. In particular, if foo is a pointer, the expression foo != 0 will have value 0 is foo is a null pointer, and 1 otherwise. There is no conversion performed!

    And it is not just pedantry, either. The conversion from pointer to int is implementation-defined, so it is possible that a null pointer foo, when cast to an int, will give a value of 1. Even then, though, the expression foo != 0 will still have value 0, because foo is a null pointer.
    Last edited by Bachstelze; August 1st, 2012 at 10:49 PM.
    「明後日の夕方には帰ってるからね。」


  10. #10
    Join Date
    Apr 2008
    Location
    Australia
    Beans
    276
    Distro
    Ubuntu

    Re: Java programming question. Boolean

    Quote Originally Posted by muteXe View Post

    Code:
    while(end == false)
    It's easier for a pleb like me to understand it when i re-read it again six months down the line.
    But 6 months later... "What the hell does end mean!?"
    By the people, for the people.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •