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

Thread: Code Experiment - Why the change?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    25
    My Mood
    Devilish
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Code Experiment - Why the change?

    This code is some code that I started out with experimenting, from a bit of code that I
    found in OCA Java SE 7 Certification Guide by Mala Gupta. What puzzles me is the
    output. My question is why the difference? All of the variables that are used in the
    two expressions are the SAME VALUE. I am using Eclipse for my IDE, but I tried it on
    the command line and got the same results. Is it a bug?

    	int a = 0;
    	int b = 0;
    	int c = 0;
    	int d = 0;
    	int e = 0;
    	int f = 0;
    	a = 10; 
                  f = a++ + a + a-- - a-- + ++a;
    	System.out.println("out " + f);
     
    	a = 10; b = 10; c = 10; d = 10; e = 10; f = 0;
    	f = a++ + b + c-- - d-- + ++e;
    	System.out.println("out " + f);
     
    	// Yes, the two are set up the same!
    	f = a++ + b + c-- - d-- + ++e;
    	f = a++ + a + a-- - a-- + ++a;

    The result is:
    out 32
    out 31

    ????




    Ok, so I just simplified it . . .

    	int a = 0;
    	int b = 0;
    	int f = 0;
    	a = 10; 
     
    	f = a++ + a;
    	System.out.println("out " + f);
     
    	a = 10; b = 10; f = 0;
    	f = a++ + b;
    	System.out.println("out " + f);

    Result:
    out 21
    out 20

    It has got to be a bug . . . .


  2. #2
    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: Code Experiment - Why the change?

    It's sleight of hand. You're told the equations are the same, and from that you might conclude that the variables have the same value at each point in the equations (the distraction), but do they?

    I don't understand what tricks like this have to do with certification of anything, but maybe that's why I'm not certified.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Code Experiment - Why the change?

    Are they the same values? They may have been initialized with the same initial value but does it remain like so?

    Do this in your compiler:
    int a = 5;
    System.out.println(a);
    a++;
    System.out.println(a);
    a--;
    System.out.println(a);
     
    // While at it why not try this as well so you can see something else that you may experience in the future.
    int b = 10;
    System.out.println(b++);
    System.out.println(b);

  4. The Following User Says Thank You to Ubiquitous For This Useful Post:

    Skywola (October 20th, 2013)

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    25
    My Mood
    Devilish
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Code Experiment - Why the change?

    Ok, I see, said the blind man . . . .

     
    	a = 10; 
    	System.out.println("a = " + a);
    	f = a++ +a;
    	System.out.println("a =" + a + " a = " + a);
     
    	System.out.println("out " + f);
     
    	a = 10; b = 10; f = 0;
    	System.out.println("a =" + a + " b = " + b);
    	f = (a++) +b;
    	System.out.println("a =" + a + " b = " + b);
    	System.out.println("out " + f);

    The "a" is being incremented on the top expression . . . because it is "a"!!!!
    "b" is not being incremented, because it is not "a"!!!
    Duuuhhhh
    Last edited by Skywola; October 19th, 2013 at 02:47 PM. Reason: clarification

Similar Threads

  1. assignment1- change pseudocode into java code
    By fasya alya in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 14th, 2013, 05:14 AM
  2. How to change my code?
    By Henk in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 22nd, 2012, 03:23 PM
  3. Other ways i can change this code
    By tylerk888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 14th, 2012, 10:23 AM
  4. Trying to repeat experiment 100 times but results vary for elapsed time
    By IHeartProgramming in forum Loops & Control Statements
    Replies: 3
    Last Post: October 22nd, 2012, 01:36 PM
  5. change java code project to exe file
    By mostafa_mohamed in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 5th, 2012, 10:55 AM