Results 1 to 10 of 10
Like Tree5Likes
  • 1 Post By jim829
  • 4 Post By Norm

Thread: Or signs "||" "|" in java programs

  1. #1
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Or signs "||" "|" in java programs

    Hello,
    Can someone tells me what is the difference between "||" "|" in java program . I know both of them means "or" but when we should use either one?
    Thank you

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Or signs "||" "|" in java programs

    "||" is used for disjunctive tests and when used evaluates to a boolean. "|" is a bitwise or to set bits in numerical values.
    E.g.

    Java Code:
    a = 10; // 1010 in binary
    a = a | 4; // 4 = 100 in binary
    a now equals 14, 1110 in binary.

    "&&" and "&" are the respective ANDing operations. Check out the tutorials link in my signature for more
    on Java.

    Regards,
    Jim
    skj likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Or signs "||" "|" in java programs

    The "|" can be used as a boolean OR operator that is not a "short-circuit" operator. IE the second expression will be evaluated even if the first one is true. It must be a carry-over from C.
    Java Code:
          int x=0, y=0;
          if(x++ == 0 || y++ == 0 ) {   //<<  use | or || to see the difference
             System.out.println("x="+x +", y="+y); // with | x=1, y=1   // with ||  x=1, y=0
          }
    DarrylBurke, jim829, skj and 1 others like this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Or signs "||" "|" in java programs

    I think I'll also read the tutorials.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: Or signs "||" "|" in java programs

    Quote Originally Posted by Norm View Post
    The "|" can be used as a boolean OR operator that is not a "short-circuit" operator. IE the second expression will be evaluated even if the first one is true. It must be a carry-over from C.
    Java Code:
          int x=0, y=0;
          if(x++ == 0 || y++ == 0 ) {   //<<  use | or || to see the difference
             System.out.println("x="+x +", y="+y); // with | x=1, y=1   // with ||  x=1, y=0
          }
    Thank you so much

  6. #6
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: Or signs "||" "|" in java programs

    Lets say the second expression is false is it going to be evaluated as true I mean
    y++!=0

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Or signs "||" "|" in java programs

    What result did you get when you tried it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Or signs "||" "|" in java programs

    More dramatic results can be obtained with the | and & operators (versus the || and && operators); I guess this is the reason why Sun/Oracle forbids the use of Java for nuclear power applications:

    Java Code:
    PowerPlant harrisBurg= new PowerPlant();
    if (harrisBurg.isSafe() & harrisBurg.switchOn())
       System.out.println("Everything OK here");
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Or signs "||" "|" in java programs

    Interesting that it could have the exact same disastrous result if the second condition were !harrisBurg.switchOn()

    db <-- far far away from Harrisburg
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Or signs "||" "|" in java programs

    Quote Originally Posted by DarrylBurke View Post
    Interesting that it could have the exact same disastrous result if the second condition were !harrisBurg.switchOn()

    db <-- far far away from Harrisburg
    Yep, Java is particularly unsafe at the semantic level, especially if you don't pay enough attention to the nitty gritty details ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 1
    Last Post: 11-03-2012, 12:38 AM
  3. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  4. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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