Results 1 to 11 of 11
  1. #1
    dardar is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default question about the operator ++

    hi, i came across the next code:

    x = 1;
    y = ++x;
    System.out.println(y);
    prints 2

    x = 1;
    y = x++;
    System.out.println(y);
    prints 1

    what is the reason for the diffrent result of constant y?

  2. #2
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Lightbulb

    Google.. postincrement and preincrement operators :rolleyes:
    The Quieter you become the more you are able to hear !

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    what is the reason for the diffrent result of constant y?

    The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

    The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:

    class PrePostDemo {
    public static void main(String[] args){
    int i = 3;
    i++;
    System.out.println(i); // "4"
    ++i;
    System.out.println(i); // "5"
    System.out.println(++i); // "6"
    System.out.println(i++); // "6"
    System.out.println(i); // "7"
    }
    }

    for more details, see here

  4. #4
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
    Exactly.. try this. if the above example by j2me64 has clarified the usage of post/pre increment operators.

    Java Code:
    class NewClass {
        public static void main(String[] args) {
            int b=5; int c= (b++) + (++b) + (++b) + (++b);
            System.out.println(c);
        }
    }
    The Quieter you become the more you are able to hear !

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,008
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    Exactly.. try this. if the above example by j2me64 has clarified the usage of post/pre increment operators.

    Java Code:
    class NewClass {
        public static void main(String[] args) {
            int b=5; int c= (b++) + (++b) + (++b) + (++b);
            System.out.println(c);
        }
    }
    Don't ever do that; it may be defined in Java but it causes undefined behaviour in both C and C++; besides, expressions like the above are only used in pathetic examples. btw. you don't need brackets there.

    kind regards,

    Jos

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by dardar View Post
    hi, i came across the next code:

    x = 1;
    y = ++x;
    System.out.println(y);
    prints 2

    x = 1;
    y = x++;
    System.out.println(y);
    prints 1

    what is the reason for the diffrent result of constant y?
    ++ in java is the equivaliant of saying the variable plus 1. so for e.g.

    Java Code:
    int x=7;
    int y= ++x;
    would mean increment x and then store its current value in the variable y, which would be 8.

    the other is this
    Java Code:
    int x=7;
    y=x++;
    which would mean assign y the current value of x i.e.(7) and then increment x.

    both examples could be written in longer terms as:

    assign after incrementing
    Java Code:
    int x=7;
    int y= x+1;
    and.. assign before incrementing.
    Java Code:
    int x=7;
    int y= x;
    x=x+1;

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    assign after incrementing
    Java Code:
    int x=7;
    int y= x+1;
    I think you mean:
    Java Code:
    int x=7;
    x = x + 1
    int y= x;
    </pedant-mode>:)

  8. #8
    chaminga is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default question about the operator ++

    When you use

    int x =1;
    int y= ++x;
    it's mean
    x= 1;
    x= x+1;
    y = x;
    first increment the value of the x the assign that value to y.

    when you use following format

    int x=1;
    int y= x++;
    then it's mean
    x=1;
    y=x;
    x= x+1;

    first assign value to y. then executing next line x value will be increased.

  9. #9
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default

    what if,

    int x=1;
    x=x++;

    what value would x hold now 1 or 2? It holds 1 after execution of x=x++. why is it so?

  10. #10
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    int x=1;
    x=x++;
    Java Code:
    x = 1
    I'd like this explained as well!
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,008
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Sno View Post
    int x=1;
    x=x++;
    Java Code:
    x = 1
    I'd like this explained as well!
    The postfix operator expression ++ has a value, which is the old value of x and it has as a side effect that the operator increments the value of its operator x. So, in baby steps:

    x++ has the value 1 (remember this value)
    x++ increments the value of x so x == 2 now
    x= value of postfix ++ operator expression (see above)

    so x= 1 (and for a very short period of time it had the value 2)

    kind regards,

    Jos

    ps. here's a nice code snippet that explains it all:

    Java Code:
    public class TrickyStuff  {
    	
        private static int x= 1;
        
        public static void main(String[] args) {
        
        	(new Thread() {
        		public void run() {
        			while(x == 1);
        			System.out.println( "x != 1" );
        			System.exit(0);
        		}
        	} ).start();
    
        	System.out.println("x= "+x);
    	while(true) { x= x++;}
        }
    }
    Last edited by JosAH; 08-16-2010 at 02:44 PM.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. help with not equal to operator !=
    By manowar689 in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 12:10 AM
  3. bitwise not ~ operator
    By kezkez in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 06:49 AM
  4. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 04:45 PM
  5. Confusing with bitwise NOT operator
    By Willi in forum New To Java
    Replies: 4
    Last Post: 10-16-2009, 11:06 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
  •