Results 1 to 7 of 7
  1. #1
    SirFalcon is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default Pre- and post-increments

    Ok, so I read the related section at the Java site about Assignment, Arithmetic, and Unary Operators, but I honestly don't get the code they provide:

    Java Code:
    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"
         }
    }
    I understand that i++ adds 1 to the original value, and ++i adds 1 to the incremented value.

    When i++ prints '6', does that mean the original value of i=3 is changed to 5 at this point?

    I also don't get why i=7 at the end.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by SirFalcon View Post
    I understand that i++ adds 1 to the original value, and ++i adds 1 to the incremented value.
    Do you have a book chapter on this? If so, you should re-read it as again your assumptions about your understanding is incorrect. Both i++ and ++i add to the original value. ++i adds to the int before it is used and i++ adds to it after it has been used. Again, this will probably be better explained in your book.
    Last edited by Fubarable; 10-29-2009 at 02:46 AM.

  3. #3
    SirFalcon is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    The Java class I am taking unfortunately does not provide us with a book so I am stuck with Powerpoint slides that aren't really helpful. I might go out and buy a book I guess.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by SirFalcon View Post
    I might go out and buy a book I guess.
    I would agree with this 100%. When I'm learning a new programming language, I usually go out and buy several books because it is tough getting these new concepts into my head. Many of the books I buy are used, so they're obtained on the cheap. Also, you should check out the Sun Java tutorials which are better than cheap -- they're free. You'll find them here: Sun Java Tutorial Big Index

    The section on the incr and decr operators can be found here: operators
    Last edited by Fubarable; 10-29-2009 at 02:53 AM.

  5. #5
    SirFalcon is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    I'll check out those links. And thank-you very much for your advice. :D

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    To go through your code:
    Java Code:
         public static void main(String[] args){
              int i = 3;
    	  i++;                                // add one to i
    	  System.out.println(i);	// display this number
    	  ++i;			        // add again to i
    	  System.out.println(i);	// display this number
    	  System.out.println(++i);	// first add one to i, THEN display it.
    	  System.out.println(i++);	// first display i, THEN after it's been displayed, add one to it.
    	  System.out.println(i);	// "7"
         }

  7. #7
    SirFalcon is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Makes perfect sense now! You have been a great help, thank-you.

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2009, 03:15 AM
  2. my first post
    By kaviyasivashanmugham in forum New To Java
    Replies: 2
    Last Post: 03-13-2009, 09:50 AM
  3. post count
    By codeflip in forum Forum Lobby
    Replies: 2
    Last Post: 03-12-2009, 02:20 AM
  4. First post out of the way..
    By sirwiggles in forum Introductions
    Replies: 0
    Last Post: 02-06-2009, 10:44 PM
  5. First post ever
    By pbpersson in forum Introductions
    Replies: 4
    Last Post: 08-16-2008, 05:30 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
  •