CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: a=a++;

  1. #1
    Join Date
    Oct 2004
    Posts
    2

    Question a=a++;

    I am surprised by the output of the following Java code. Normally, I would not do "a=a++;". But I just could not explain why the "a=" would yield a wrong result.
    Please help.

    =============================================
    import java.io.*;

    public class Test {

    public static void main (String args[]) {
    int a = 1;
    System.out.println(a);
    a=a++;
    System.out.println(a);
    }

    }

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: a=a++;

    this topic is often a cause for confusion in newcomers to java. it has been discussed in depth and entirety before, and a good, clear explanation may be found here:

    http://www.codeguru.com/forum/showthread.php?t=270776
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: a=a++;

    incidentally, writing a=a++ is very poor coding style, for obvious reasons, and is to be avoided in all situations. a similar thing could be said of:

    String s;
    while(s = input.readLine() != null)

    this is better written as:
    for(String s = in.readLine(); s!=null; s= in.readLine())



    in programming, like in english.. one sentence one point. one statement one purpose. avoiding combining assignment with other operations is the best option in all cases.


    "everything should be as simple as possible, but no simpler.." - Einstein
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: a=a++;

    Quote Originally Posted by samli
    I am surprised by the output of the following Java code. Normally, I would not do "a=a++;". But I just could not explain why the "a=" would yield a wrong result.
    You may be surprised by it, but that doesn't mean it's wrong. The result is, in fact, exactly as the Java Language Specification requires.

    I don't understand it, therefore it's wrong...
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Oct 2004
    Posts
    2

    Thumbs up Re: a=a++;

    Thank you all for the explanation, which helps me understand the code.
    I also enjoy the comment dlorde made. I really should keep that I mind when I troubleshoot my code in the future.
    Thanks again.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,405

    Re: a=a++;

    Quote Originally Posted by samli
    I also enjoy the comment dlorde made. I really should keep that I mind when I troubleshoot my code in the future.
    You mean to consult the Java language specification and check the facts rather than guess? My god is that allowed! I thought it was considered cheating.

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: a=a++;

    if only i'd been allowed a copy of the JLS in my biology exam..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured