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: nooby question

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default nooby question

    I just wanted to know is it possible to do something like x==1|2 or do I have to do x==1 | x ==2?

    Sorry for the waste of time. I haven't been able to find it on google (might not be searching the right terms).

    Thanks in advance
    Last edited by boogerface; September 18th, 2011 at 07:22 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: nooby question

    Are you trying to make x an array?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nooby question

    wow, I didn't even explain it properly xD

    I mean't in an if statement. if (x == 1|2) {}

    Thanks again
    Last edited by boogerface; September 18th, 2011 at 07:22 PM.

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

    Default Re: nooby question

    Are you asking how to test if x is equal to 1/2?
    What does the term: 1|2 mean?
    The | operator is the bitwise OR. 1 ORed with 2 is 3. Try this:
    System.out.println(3 == (1|2)); // this expression is true

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nooby question

    | means or. So I want to know if it's possible to check if x == 1|2 (x is equal to 1 or 2) without having to use 2 statements if (x==1 | x==2) {}
    Last edited by boogerface; September 18th, 2011 at 09:15 PM.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: nooby question

    Oh! ok. As far as i know, the correct if statement would be :

    if ((x == 1)|(x==2)) {}

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nooby question

    I'm guessing there's no way to make it shorter then?

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: nooby question

    Unfortunately, i don't think there is =(

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: nooby question

    Quote Originally Posted by Bertorox View Post
    Oh! ok. As far as i know, the correct if statement would be :

    if ((x == 1)|(x==2)) {}
    '|' is a bitwise or operator - are you looking to do math, or to check equality? If the latter, use '||'. You could write this another way:
    if ( 1 <= x && <= 2 ){}

    Looking for shorthand ways of doing things could at times be considered bad practice, as it can obfuscate code and make it very unreadable - both by someone else and by yourself some time down the line when you try and figure out what the heck you were trying to do by looking at your own code.