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

Thread: Difference between ++i and i++

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difference between ++i and i++

    Hi,

    I'm new to Java and I wanted to understand what is the difference between ++i and i++? I know at times they function the same way and at times they function differently.

    1) What are those cases? Like if i say:

    int i = 5;
    int x = i++;
    vs

    int i = 5;
    int x = ++i

    2)What about cases when it is not just the assignment to a variable. In cases where you're using it immediately?

    int i = 5;
    int j = 7;
    int x = i++ * --j;
    or
    int i = 5;
    int j = 7;
    int x = ++i * j++;

    What is the difference?

    Thanks!


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Difference between ++i and i++

    Postfix and prefix are the search terms: https://www.google.com/search?q=java+postfix+vs+prefix

  3. #3
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Difference between ++i and i++

    When you do a prefix like ++i the value of i is increment before returning and if you do a postfix like i++ then value is incremented after returning.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Location
    kathmandu, nepal
    Posts
    29
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between ++i and i++

    Like if u do a=++i then first i will save its value to a then only add 1 to itself. whereas a=i++ means first i adds 1 to itself then only save the value to a.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Difference between ++i and i++

    Quote Originally Posted by viper_pranish View Post
    Like if u do a=++i then first i will save its value to a then only add 1 to itself. whereas a=i++ means first i adds 1 to itself then only save the value to a.
    Did you verify your understanding with code?

Similar Threads

  1. [SOLVED] What is the difference?
    By 344a in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 19th, 2013, 09:26 AM
  2. What is the difference between these two?
    By Java Trainer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2013, 02:19 AM
  3. Difference ++j and J++
    By rayan2004 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2012, 01:42 AM
  4. What is the difference?
    By soul_samir in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2012, 03:08 PM
  5. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 06:24 AM

Tags for this Thread