3

I have an array of ints:

private int array[];

If I also have a method called add, then what's the difference between the following:

public void add(int value) {
   array[value]++; VS ++array[value];
}

p.s. on a separate note, what's the difference between int array[] vs int[] array ? Thanks

atoregozh
  • 425
  • 1
  • 4
  • 11

2 Answers2

7

what's the difference between int array[] vs int[] array ?

There is none. It's just java convetion to create arrays like int[] array, it's more clear.

If I also have a method called add, then what's the difference between the following:

   public void add(int value) {
       array[value]++; VS ++array[value];
    }

In this code, there is not any difference. But the difference in general is:

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6

//EDIT

As Vince Emigh mentioned in comments below, this should be in answer too ...

As you know, ++ increments the number by 1. If you call it after the variable, your program will increment the number, and if needed imediately (like when you increment inside the println params), returns what the value was BEFORE incrementing (resulting in your 5). Adding it before your var will result in your program increasing the value right away, and returns the incremented value. If you dont use the variable imediately, like you do when youre printing it out, then it really doesnt matter, since they both increment.

Community
  • 1
  • 1
Matej Špilár
  • 2,617
  • 3
  • 15
  • 28
  • 1
    Nice answer. +1 in 5 minutes (tomorrow in UTC). However, you could add what ++x and y++ actually do in words. – Anubian Noob May 03 '14 at 23:55
  • 2
    @AnubianNoob As you know, `++` increments the number by 1. If you call it after the variable, your program will increment the number, and if needed imediately (like when you increment inside the println params), returns what the value was BEFORE incrementing (resulting in your 5). Adding it before your var will result in your program increasing the value right away, and returns the incremented value. If you dont use the variable imediately, like you do when youre printing it out, then it really doesnt matter, since they both increment – Vince May 04 '14 at 00:00
  • I know, but it should be included in the answer. – Anubian Noob May 04 '14 at 00:02
  • @AnubianNoob I didnt write this answer. I have also seen/answered this question multiple times today ( http://stackoverflow.com/questions/23448740/why-does-var-and-var-1-work-differently#comment35943186_23448740 one example), so i didnt bother making my own official answer, since there was one already here – Vince May 04 '14 at 00:04
  • 1
    @VinceEmigh Ive edited my answer, sorry i should mention that :) – Matej Špilár May 04 '14 at 00:09
  • thanks @VinceEmigh this part of the comment: "If you dont use the variable imediately, like you do when youre printing it out, then it really doesnt matter, since they both increment" is something that I was looking for. I appreciate it. – atoregozh May 04 '14 at 00:26
3

This example will make pre -increment and post increment clearer In pre-increment the value is incremented before the operation , in post increment it is done before the operation.

        arr[0]=1;
        int var1=arr[0]++;
        System.out.println(var1);
        arr[0]=1;
        int var2=++arr[0];      
        System.out.println(var2);

Here I am doing an assignment, if I have a post increment , the increment occurs after the assignment. If i have a pre increment the increment occurs before the assignment

The output

1
2
Biswajit_86
  • 3,661
  • 2
  • 22
  • 36