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: Really Quick n00b question, should take three seconds to answer

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Really Quick n00b question, should take three seconds to answer

    int n = 5;
    n = n++;
    System.out.println(n);

    Well, this prints out 5, can anyone explain please? Thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Really Quick n00b question, should take three seconds to answer

    See the response to Help with prefix/postfix

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Really Quick n00b question, should take three seconds to answer

    edit - nevermind

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Location
    New York
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Really Quick n00b question, should take three seconds to answer

    It is bad coding because n is initialized with 5, then n is incremented to 6 and then n is equal to 6.
    When n is incremented to 6 it is the same thing as saying n = n + 1; n++ is called a short hand or idiom.
    So what is happening is this...
     
    int n = 5;
    n = (n = n + 1); 
    System.out.println(n);

    !!

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Really Quick n00b question, should take three seconds to answer

    Quote Originally Posted by yasuocodez View Post
    It is bad coding because n is initialized with 5, then n is incremented to 6 and then n is equal to 6.
    When n is incremented to 6 it is the same thing as saying n = n + 1; n++ is called a short hand or idiom.
    So what is happening is this...
    int n = 5;
    n = (n = n + 1); 
    System.out.println(n);

    !!
    No; it is bad code, but that's not what is happening, you've missed the whole point - the OP wants to know why the output is 5 instead of 6.

    The result of the postfix expression is the original value; the increment operation is performed on the right hand expression value after the assignment and is, in this case, thrown away.

Similar Threads

  1. [SOLVED] Begginer Question please help ASAP! (Easy to Answer)
    By Bagzli in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2010, 09:00 PM
  2. Replies: 5
    Last Post: August 5th, 2010, 09:16 PM
  3. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  4. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM
  5. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 04:48 PM