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 6 of 6

Thread: the use of && or & or || or |

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default the use of && or & or || or |

    when using the && if the first operand is evaluated into false, then the whole operation is false,
    while using the || if the first operand is evaluated into true, the the whole operation is true,
    basically is the condition happened in this way, the second operand will never be evaluated.. ryt?


    but how bout using a single character ? "&" or "|"

    it will always evaluate every operands...


    my question is

    in what kind of situation this operators are mostly used?

    a single "&" or a single "|".


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: the use of && or & or || or |

    Sinle & and | are bitwise operators not boolean operators, other bitwise operators are things such as >> << >>> <<< ~ ^

    Bitwise operators work on bits within a value.

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: the use of && or & or || or |

    oh? i thought they are booleans ... so its for bitwise.....

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: the use of && or & or || or |

    For boolean values, they can be thought of as taking up only 1 bit (I think technically Java allocates a whole byte, but only 1 bit actually gets used/boolean value)

    So, 1 & 1 = 1 => true && true = true
    However, if we decide to work with the bits inside of say a byte:

        1111 1110
    &  0100 0001
    ----------------
        0100 0000

    And since the && isn't defined for bytes, there is no equivalence to this.

    Another small minor detail is the way Java implements both of these operators.

    Java will "optimize" any && or || type operators, and skip over any unecessary comparisons.

    String a = null;
     
    if (a != null && a.equals("Hello"))

    Here, java will test the condition a != null. It will return false, and java, seeing that this is going to be and'ed to a.equals("Hello"), will just skip that comparison.

    However, if we do this:
    String a = null;
    if (a != null & a.equals("Hello"))

    Java won't optimize this, and will try to evaluate a.equals("Hello"). Of course, this will fail, and Java will throw a nice friendly NullPointerException.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (November 7th, 2009)

  6. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: the use of && or & or || or |

    I would say you never you use the single & or | in normal statements, they are normally used for bitwise operations like helloworld said.

    I think you should prefer the shortcut operators in any situation unless there is a specific need to use something else.

    // Json

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

    chronoz13 (November 7th, 2009)

  8. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: the use of && or & or || or |

    thats the answer im looking for..... using these operators in certain situation.. like the book said.. "we will most likely never ecounter a situation where we cannot use the double ampersand (&&) and double vertical bars...

    and i notice with my threads... i think that i should have a zip of what are bitwise operators.. operations.. or bitwise programs...