incrementing and decrementing in JAVA

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 955 Views - Last Post: 13 January 2013 - 05:52 PM Rate Topic: -----

#1 mbilal1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-September 12

incrementing and decrementing in JAVA

Posted 02 January 2013 - 05:07 PM

Hey everyone, will i was just reviewing java from the beginning and i came across something that i never understood.
Ok so when you increment something, you use
 
int x=7
x=x++;


and the output is 8.

then why is it that when you do something like this:

int x,y,z;
x=42;
y=x++;
z=++x;



the output is
x=44
y=42
z=44



shouldn't it have been,
x=42
y=43
z=43

why did it skip to 44 instead of 43? and why did it give x the value 44, when i had declared x to be 42?

If someone can please explain how this works.
The same thing happens when you try decrementing it.

int a,b,c;
a=10;
b=a--;
c=--a; 



the output is
1=8
b=10
c=8



instead of a=10, b=9, c=9? like where did the 8 come from and doesn't a=10 and not 8?

thank you for explaining this!

Whoops! mean a=8, not 1=8

View Postmbilal1, on 02 January 2013 - 05:06 PM, said:

Hey everyone, will i was just reviewing java from the beginning and i came across something that i never understood.
Ok so when you increment something, you use
 
int x=7
x=x++;


and the output is 8.

then why is it that when you do something like this:

int x,y,z;
x=42;
y=x++;
z=++x;



the output is
x=44
y=42
z=44



shouldn't it have been,
x=42
y=43
z=43

why did it skip to 44 instead of 43? and why did it give x the value 44, when i had declared x to be 42?

If someone can please explain how this works.
The same thing happens when you try decrementing it.

int a,b,c;
a=10;
b=a--;
c=--a; 



the output is
1=8
b=10
c=8



instead of a=10, b=9, c=9? like where did the 8 come from and doesn't a=10 and not 8?

thank you for explaining this!


Is This A Good Question/Topic? 0
  • +

Replies To: incrementing and decrementing in JAVA

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10569
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: incrementing and decrementing in JAVA

Posted 02 January 2013 - 05:09 PM

This statement x=x++; is equivalent to the two statements below:
x = x;
x++;



That should shed some light on what is happening in your second snippet.

And when you have:
z = ++x;



That is equivalent to:
++x; //increment x first
z = x; //then assign to z

//instead of 
z = x;
x++; 

//which is equivalent to
z = x++;


Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: incrementing and decrementing in JAVA

Posted 02 January 2013 - 05:53 PM

View Postmbilal1, on 03 January 2013 - 01:07 AM, said:

Ok so when you increment something, you use
 
int x=7
x=x++;



No, you don't. You either use x = x+1;, x += 1; or x++.

x = x++; changes the value of x twice on the same line, making it hard to see what's going on. It also doesn't increment x.

Quote

and the output is 8.


No, it's 7.

Quote

then why is it that when you do something like this:

int x,y,z;
x=42;
y=x++;
z=++x;



the output is
x=44
y=42
z=44



The most important thing to realize is that ++x and x++ actually change the value of x. So if you use ++ on x twice, it'll get incremented twice and thus get the value 44.

The next thing is that x++ returns the old value of x and ++x returns the new value of x. So when you do y = x++; while x is 42, x will be incremented by 1 (becoming 43) and y will become 42 (the old value of x).

View Postmacosxnerd101, on 03 January 2013 - 01:09 AM, said:

This statement x=x++; is equivalent to the two statements below:
x = x;
x++;



Actually it's equivalent to this:

// Save the old value of x because that's
// the return value of x++
int tmp = x;
// Increment x
x = x + 1;
// Assign the saved return value to x
x = tmp;



Note that the value of x will be unchanged after the code executes.

This post has been edited by sepp2k: 02 January 2013 - 05:58 PM

Was This Post Helpful? 1
  • +
  • -

#4 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: incrementing and decrementing in JAVA

Posted 12 January 2013 - 10:51 AM

Here is a general rule when it comes to decrement and increment.

Quote

++x; Increment and then use it
--x; Decrement and then use it
x++; Use it and then increment
x--; Use it and then decrement

Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: incrementing and decrementing in JAVA

Posted 12 January 2013 - 11:58 AM

View Postk3y, on 12 January 2013 - 06:51 PM, said:

Quote

x++; Use it and then increment
x--; Use it and then decrement


That's a bit misleading though. If you phrase it like that, one might easily think that x = x++; should in fact cause x to be incremented by one because it would first use x to perform x = x and then increment it. It might also lead one to think that f(x++) would first call f with x as the argument and then increment x after the method returns.

Since these things are not true, I think it'd be less misleading to phrase it like this:

x++: Increment x and return the old value of x.
x--: Decrement x and return the old value of x.

This post has been edited by sepp2k: 13 January 2013 - 06:28 AM

Was This Post Helpful? 2
  • +
  • -

#6 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: incrementing and decrementing in JAVA

Posted 12 January 2013 - 03:46 PM

The way I understand prefix and postfix expressions is like this:
A prefix expression (i.e. ++x) will ALWAYS increment/decrement a value by one.
A postfix expression (i.e. x++) will ONLY increment/decrement the value after the original value has been evaluated by the compiler and/or the JVM.

Here is a sample piece of code. See the comments within the code for greater details.
public class PrePostDemo
{
	public static void main(String []args)
	{
		int x = 3;
		int y = 4;
		
		// Applying the prefix operator
		++x;
		System.out.println("x is " + x);	// x is 4
		System.out.println("x is " + ++x);	// x is 5
		++x;
		System.out.println("x is " + x);	// x is 6
		
		// Applying the postfix operator
		y++;
		System.out.println("y is " + y);	// y is 5
		
		y++;
		System.out.println("y is " + y);	// y is 6
		
		/* In the print statement below, y is still 6 
		 * because the original value of 6 has not been incremented
		 * just yet while y++ (or y = y + 1) is within the parameters 
		 * of the method println()
		 */
		System.out.println("y is " + y++);
		
		/* In the print statement below, y is now 7 
		 * because the original value of y has been evaluated
		 * earlier. 
		 */
		System.out.println("y is " + y);
	}
}


You can learn more about this at The Java Tutorials. Hope this is helpful.
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: incrementing and decrementing in JAVA

Posted 12 January 2013 - 03:57 PM

View Postjjh08, on 12 January 2013 - 11:46 PM, said:

		/* In the print statement below, y is still 6 
		 * because the original value of 6 has not been incremented
		 * just yet while y++ (or y = y + 1) is within the parameters 
		 * of the method println()
		 */
		System.out.println("y is " + y++);



Again: That is not true. The value of y will be incremented before println is called, not after. However the value given to println will be the old value of y. If you want to see the difference consider this code:

class Bla {
    static int y = 42;

    static void f(int z) {
        System.out.println("y = " + y + ", z = " + z);
    }

    public static void main(String[] args) {
        f(x++);
    }
}



Here the value of y inside f will be 43, not 42. So clearly y is incremented before the method is called. Only z is 42 because y++ evaluates to the old value of y.

This post has been edited by sepp2k: 12 January 2013 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: incrementing and decrementing in JAVA

Posted 12 January 2013 - 04:49 PM

View Postsepp2k, on 12 January 2013 - 03:57 PM, said:

View Postjjh08, on 12 January 2013 - 11:46 PM, said:

		/* In the print statement below, y is still 6 
		 * because the original value of 6 has not been incremented
		 * just yet while y++ (or y = y + 1) is within the parameters 
		 * of the method println()
		 */
		System.out.println("y is " + y++);



Again: That is not true. The value of y will be incremented before println is called, not after. However the value given to println will be the old value of y. If you want to see the difference consider this code:

Oh sorry. I actually knew that it would be incremented before println() but I guess I just wrote it down incorrectly. I was trying to say that even though y++ was within the parameter, y will still show 6 until the next println(y) is called...Maybe I just don't know how to put it in words correctly :(/> but I agree with what you are saying.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7752
  • View blog
  • Posts: 13,110
  • Joined: 19-March 11

Re: incrementing and decrementing in JAVA

Posted 12 January 2013 - 05:02 PM

And all of this is why you should avoid using the ++ and -- operators.
They're never needed, and they create bug habitat.

This post has been edited by jon.kiparsky: 12 January 2013 - 05:02 PM

Was This Post Helpful? 2
  • +
  • -

#10 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: incrementing and decrementing in JAVA

Posted 12 January 2013 - 06:12 PM

Or you can just remember that:

x++

means add 1 to x after I use the value if x

and

++x

means add 1 to x before I use the value of x.

I think Jon's (Kipper..haha) advice is good, as well.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: incrementing and decrementing in JAVA

Posted 13 January 2013 - 06:28 AM

View Postfarrell2k, on 13 January 2013 - 02:12 AM, said:

Or you can just remember that:

x++

means add 1 to x after I use the value if x


View Postsepp2k, on 12 January 2013 - 07:58 PM, said:

That's a bit misleading though. If you phrase it like that, one might easily think that x = x++; should in fact cause x to be incremented by one because it would first use x to perform x = x and then increment it. It might also lead one to think that f(x++) would first call f with x as the argument and then increment x after the method returns.

Since these things are not true, I think it'd be less misleading to phrase it like this:

x++: Increment x and return the old value of x.

Was This Post Helpful? 0
  • +
  • -

#12 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: incrementing and decrementing in JAVA

Posted 13 January 2013 - 06:59 AM

View Postfarrell2k, on 13 January 2013 - 02:12 AM, said:

Or you can just remember that:

x++

means add 1 to x after I use the value if x


This is MUCH easier to understand.
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: incrementing and decrementing in JAVA

Posted 13 January 2013 - 07:10 AM

View Postfarrell2k, on 13 January 2013 - 02:59 PM, said:

View Postfarrell2k, on 13 January 2013 - 02:12 AM, said:

Or you can just remember that:

x++

means add 1 to x after I use the value if x


This is MUCH easier to understand.


And you don't agree that it's also easier to misunderstand? I.e. that it might lead people to think that x = x++ increments x or that f(x++) will not change x until after f returns?
Was This Post Helpful? 1
  • +
  • -

#14 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: incrementing and decrementing in JAVA

Posted 13 January 2013 - 08:26 AM

I agree it's easy to misunderstand. The whole thing is a confusing mess.

Our definition are one in the same, worded differently.
Was This Post Helpful? 0
  • +
  • -

#15 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: incrementing and decrementing in JAVA

Posted 13 January 2013 - 08:43 AM

View Postfarrell2k, on 13 January 2013 - 03:26 PM, said:

I agree it's easy to misunderstand. The whole thing is a confusing mess.

Our definition are one in the same, worded differently.

I think the point sepp2k is making is that your definition and his are not equivalent, and can produce measurably different results.

Consider the test program:

public class Test1 {
    private int x = 0;
    
    public void doTest() {
	test(x++);
    }
    
    private void test(int y) {
	System.out.println(this.x);
    }

}


By your definition, it should print "0", by sepp2k's definition it should print "1". It indeed does print "1";
Was This Post Helpful? 2
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3