Results 1 to 6 of 6

Thread: Post increment

  1. #1
    bigo005 is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Question Post increment

    public class Test
    {
    public static void main(String args[])
    {
    public int i=10;
    i=i++;
    System.out.print(i);
    }
    }

    Why the value of i is 10 printed here why not 11?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Post increment

    That code doesn't even compile, so it is not the code you are running.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Post increment

    Next time, wrap your code with CODE in brackets. I'll do it for you this time:

    Java Code:
    public class Test
    {
    public static void main(String args[])
    {
    public int i=10;
    i=i++;
    System.out.print(i);
    }
    }
    The reason lies in the 6th line.
    Java Code:
    i=i++;
    That isn't a valid way to add 1.
    You could do this:
    Java Code:
    i = i + 1;
    or this:
    Java Code:
    i += 1;
    or what I think you tried to do but slightly mistyped:
    Java Code:
    i++;
    But i=i++; is a combination that doesn't work.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Post increment

    see this:

    Java Code:
           int a = 10;
    		System.out.println(a);		// 10
    		System.out.println(a++); 	// 10
    		System.out.println(a);		// 11
    
    		int b = 10;
    		System.out.println(b);		//   10
    		System.out.println(++b); 	//   11
    		System.out.println(b);		//   11
    a++ will first execute SOP and the increase the value while ++b will first increase value and then execute SOP

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Post increment

    Quote Originally Posted by AlexGraal View Post
    Next time, wrap your code with CODE in brackets. I'll do it for you this time:
    If you don't indent it properly, it's still useless.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Post increment

    Get rid of the public int declaration in your main method. It makes no sense for locals. Then it will compile (but it probably won't do what you want as has already been explained).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. post increment inside a loop?
    By brendan89 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 07:40 PM
  2. Increment Counter for ID
    By blkshrk81 in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 02:25 AM
  3. Replies: 3
    Last Post: 05-04-2009, 04:15 AM
  4. quick pre/post increment question
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-01-2009, 06:47 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
  •