10 Replies - 580 Views - Last Post: 04 August 2010 - 09:37 PM Rate Topic: -----

#1 aklo  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 229
  • Joined: 23-January 09

++ i ...what exactly is this.

Posted 03 August 2010 - 08:16 AM

Ok firstly i've done my own reading from the official oracle java tutorial. Here is what the tutorial says.

Quote

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:

class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i); // "4"
++i;
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"
}
}


I still don't get it.

int i = 3;
i++ (result = 4 ok i know this)
++i (result = 5 seems to work like i++)
++i (yet again?? 6...ok still seem to work like i++)
i++ (what? still 6?? i though it should be read as i = 6 + 1)

So what exactly is this weird stuff going on.

Is This A Good Question/Topic? 0
  • +

Replies To: ++ i ...what exactly is this.

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,479
  • Joined: 27-December 08

Re: ++ i ...what exactly is this.

Posted 03 August 2010 - 08:18 AM

++i is different that i++. The first is the prefix operator, while the second is the postfix operator. As the names imply, the prefix operator is evaluated first in an expression, while the postfix operator is evaluated last. So for example:
int x = 0;
array[x++] = 1; //array[0] = 1, x = 1
array[++x] = 2; //x = 2, array[2] = 2


Was This Post Helpful? 2
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: ++ i ...what exactly is this.

Posted 03 August 2010 - 08:18 AM

i++ increments after the operation, if any the variable is contained within, is performed.
++i increments before. Same for decrementing.
Was This Post Helpful? 1
  • +
  • -

#4 aklo  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 229
  • Joined: 23-January 09

Re: ++ i ...what exactly is this.

Posted 03 August 2010 - 08:38 AM

Alright I think I get it.

It is difficult to actually visualize a++ and ++a , but after more careful reading and trying out I think i get it...

just like if i have an out.print(a++) normal I'll just think that it will display the results once everything is done...but actually it will first display the value of a before + 1 to it.

A helpful tick for both of you :D

Problem solved.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,479
  • Joined: 27-December 08

Re: ++ i ...what exactly is this.

Posted 03 August 2010 - 08:44 AM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

#6 adhish94  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 12-July 10

Re: ++ i ...what exactly is this.

Posted 03 August 2010 - 08:47 AM

When we use the post-increment operator (i.e. i++), the original value of the variable is used for the current operation, and THEN it increases (or decreases as in case of i-- i.e. decrement). In case of pre-increment, the value is first incremented (increased) and then the operation is done, using the new value.

Example:


int a = 5; //initial value of a is 5
System.out.println(a++); //post-increment; prints 5
System.out.println(++a); //pre-increment; prints 6



Was This Post Helpful? 1
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2695
  • View blog
  • Posts: 10,555
  • Joined: 15-July 08

Re: ++ i ...what exactly is this.

Posted 03 August 2010 - 10:39 AM

View Postadhish94, on 03 August 2010 - 10:47 AM, said:

When we use the post-increment operator (i.e. i++), the original value of the variable is used for the current operation, and THEN it increases (or decreases as in case of i-- i.e. decrement). In case of pre-increment, the value is first incremented (increased) and then the operation is done, using the new value.

Example:


int a = 5; //initial value of a is 5
System.out.println(a++); //post-increment; prints 5
System.out.println(++a); //pre-increment; prints 6




I think you mean:
int a = 5; //initial value of a is 5
System.out.println(a++); //post-increment; prints 5
System.out.println(++a); //pre-increment; prints 7!!



This post has been edited by Dogstopper: 03 August 2010 - 10:40 AM

Was This Post Helpful? 1
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1500
  • View blog
  • Posts: 3,211
  • Joined: 11-December 07

Re: ++ i ...what exactly is this.

Posted 04 August 2010 - 01:28 AM

As a matter of coding style, it's often a bad idea to use the ++ operators inside expressions. It's fine to use them on their own line to simply increment a variable but people sometimes take a shortcut to do things like access an array element while incrementing the index counter. While this can lead to concise code, it's often confusing when you come back to it.
Was This Post Helpful? 0
  • +
  • -

#9 YasuoDancez  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 128
  • Joined: 30-September 09

Re: ++ i ...what exactly is this.

Posted 04 August 2010 - 03:00 AM

I think it is easy to illustrate the pre and post increment in a loop.

int i = 0;
int sum = 0;

while( some condition is true ){

sum = 5 + i++; 



Will result in sum being equal to 5 + 0 ( the value of i before incrementing it ).
Then i will increment to plus one more ( which will make i = 1 )

Then the next time around...



i = 1; //i will be equal to 1 as said
sum = 5; //sum will still be equal to 5

while( some condition is true ){

sum = 5 + i++; 




Will result in sum being equal to 5 + 1 ( 6 )
Then i will increment to plus one more again( which will make i = 2 now )




int i = 0;
int sum = 0;

while( some condition is true ){

sum = 5 + ++i; 



Will result in sum being equal to 5 + 1 ( the value of i will be plus one first
then sum will be equal to the result of that i.
Will result in sum being equal to 5 + 1 ( 6 )
and then a similar pattern happens each time around

Hope that was illustrated simple enough.

This post has been edited by YasuoDancez: 04 August 2010 - 03:02 AM

Was This Post Helpful? 1
  • +
  • -

#10 adhish94  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 12-July 10

Re: ++ i ...what exactly is this.

Posted 04 August 2010 - 08:31 AM

@dogstopper Yeah, that's a typo! :)
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: ++ i ...what exactly is this.

Posted 04 August 2010 - 09:37 PM

View Postcfoley, on 04 August 2010 - 02:28 AM, said:

As a matter of coding style, it's often a bad idea to use the ++ operators inside expressions. It's fine to use them on their own line to simply increment a variable but people sometimes take a shortcut to do things like access an array element while incrementing the index counter. While this can lead to concise code, it's often confusing when you come back to it.

If you had programmed 20 to 35 years ago you would have known that ++i would save you a few nano seconds compared to i++
Performance oriented habits might be difficult to loose :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1