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 ++j and J++

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Difference ++j and J++

    This code produce output 1 2, when i use J++ and produce 1 when i use ++J
    Please help me to understand, im new to java. Still trying to understand how loops work.

    int j = 0;
    while (j++ < 2)
    System.out.print(j);


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Difference ++j and J++

    j++ returns the value of j then increments j.
    ++j increments the value of j then returns the value of j.

    Can you work out why you're getting the different results now?

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    rayan2004 (November 30th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Difference ++j and J++

    Hi helloworld922

    Thank you for your quick response. Yes i managed to understand.

    int j = 0;
    while (j++ < 2) // j = 0 / j = 1/ j = 2 and condition false.
    System.out.print(j); // prints incremented value. so J is 1/ J is 2,

    int j = 0;
    while (++j < 2) // j = 1/ j = 2 and condition false
    System.out.print(j); // prints incremented value. so J is 1.

  5. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Difference ++j and J++

    If you want to study more about these, google for Post Fix and Pre Fix Increment. You will get the clear understanding about these.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  6. The Following User Says Thank You to Mr.777 For This Useful Post:

    rayan2004 (November 30th, 2012)

  7. #5
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    20
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Difference ++j and J++

    j++ return the value of j after that it will make increment
    while ++j return from incremented value

Similar Threads

  1. What is the difference?
    By soul_samir in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2012, 03:08 PM
  2. Difference between jboss 4.x,5.x and 6.x
    By vairam in forum Member Introductions
    Replies: 1
    Last Post: May 3rd, 2011, 10:49 AM
  3. [SOLVED] Difference between == and equals.
    By goldest in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2010, 03:15 PM
  4. Replies: 1
    Last Post: August 13th, 2010, 07:58 AM
  5. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 06:24 AM