Results 1 to 7 of 7

Thread: Java Operators

  1. #1
    Ash-infinity is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default Java Operators

    Hi all,
    I'm new to java. Please help anyone regarding this question....

    int a=1;

    a += ++a + a++;

    compiler gives answer as 5.
    but according to the logic it should give answer 7.

    please anyone explain this....
    thanks millions.......

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Basically there are four operators.

    1. Compound operatos
    2. Prefix increment
    3. Postfix increment
    4. Addition

    Do you know about those?

  3. #3
    Ash-infinity is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    ya, i know.....
    but i am confuse with the order of execution. still im didnt get the answer.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Postfix operators are executed once the process is finish. Prefix operators are executed before the process finish.

    Java Code:
    a += ++a + a++;
    a = a + ++a + a++;
    a = a + (++a) + (a++);
    Execution start from left to right.

    Java Code:
    a = a + (++a) + (a++);
         // Post fix not effected, so
    a = a + (++a) + (a);
    a = 1 + (2) + (2);
    Answer is 5, hope it's clear.

  5. #5
    Deepa is offline Member
    Join Date
    Nov 2008
    Posts
    85
    Rep Power
    0

    Default Re

    Hi,


    Ques->a += ++a + a++;

    Ans->I think,the evaluation start from left to right.

    a=a + ++a + a++;
    a=1 + ++a + a++;
    a=1 + 2 + a++;
    a=1 + 2 + 2
    a=5

  6. #6
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi Deepa,
    The above answer is correct..

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Deepa View Post
    Hi,


    Ques->a += ++a + a++;

    Ans->I think,the evaluation start from left to right.

    a=a + ++a + a++;
    a=1 + ++a + a++;
    a=1 + 2 + a++;
    a=1 + 2 + 2
    a=5
    Evaluation is from left to right, seems you are talking about the same thing here.

Similar Threads

  1. Using the bitwise operators
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:39 PM
  2. Demonstration of the mathematical operators
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-16-2008, 11:02 PM

Posting Permissions

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