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

Thread: Evaluation of operators in expressions.

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

    Default Evaluation of operators in expressions.

    Hi, I'm at the stage in my book where it teaches me the order of evaluation with operators.

    It says 1 % 2 * 3 evaluates to 3.

    I cant work out how this is true?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Evaluation of operators in expressions.

    What would you expect it to evaluate to?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Evaluation of operators in expressions.

    Well you do multiply first right? So it evaluates to 1 % 6, but that cant be right?

    What is the right way of evaluating this?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Evaluation of operators in expressions.

    Recommended reading: Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Basically, the mod operator IS a multiplicative operator (so is division), so they are the same precedence, so they are evaluated left-to-right.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Evaluation of operators in expressions.

    So what is the remainder of 1 % 2?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Evaluation of operators in expressions.

    Quote Originally Posted by TP-Oreilly View Post
    So what is the remainder of 1 % 2?
    What do you think it is? What happens when you write a small program to test it out?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Evaluation of operators in expressions.

    1 % 2 is 1 when i tested it, so yeah it'll evaluate to 3, but how does it evaluate to that, obviously it does, but atm i'm not understanding why. 1 devided 2 is .5, so can you tell me how it evaluates to 1 please.

    Thanks.

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Evaluation of operators in expressions.

    % is not divide - it's "remainder after integer division". 1 can be integer-divided by 2 exactly zero times. The remainder is 1.

  9. The Following User Says Thank You to Sean4u For This Useful Post:

    KevinWorkman (September 23rd, 2011)

Similar Threads

  1. Expressions in Java
    By naushadjadoon in forum Threads
    Replies: 3
    Last Post: October 14th, 2011, 05:16 AM
  2. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 07:35 AM
  3. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM
  4. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 09:25 AM
  5. regular expressions
    By brad35309 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2010, 08:30 PM