3

I am really confuse why in this small example i is still 0:

public static void main(String[] args) {
    int i = 0;
    inc(i);
    System.out.println(i);
}

private static void inc(int i) {
    i++;
}

probably very easy question but I dont see it

hudi
  • 15,555
  • 47
  • 142
  • 246

6 Answers6

11

Java passes parameters by value.

So the i in the method inc is really just a copy of the "original" i in the main method. You increment that copy, but that has no influence on the original variable i outside.

See this question for a more detailed explanation.

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • so I cannot change value of int or Integer in void method ? but for example list I can changed in void method – hudi Feb 09 '12 at 14:46
  • @hudi: Yes, Lists are objects and therefore only a reference is copied, which can then be used to manipulate the original instance. See my answer for an example of how you can use this to simulate references to primitive types (although you probably don't want to do this). – Niklas B. Feb 09 '12 at 14:56
3

This is because you're using a primitive integer, essentially you're passing the value of i to inc not a reference to i. In this case just return a value from your inc method:

public static void main(String[] args) {
    inc i = 0;
    i = inc(i);
    System.out.println(i);
}

private static int inc(int i) {
    return i++;
}

Of course, if you're passing around objects then you are passing by reference and so you will be able to mutate without returning.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

If you want to work. You need to wrap in a class like the following:

class MyInt {
   int i;
}

and then you go on passing an object of MyInt to the "inc" function. In which you would do the following:

    private static void inc(MyInt myInt){
      myInt.i++;
    }

not a great practice, this is just to let you know, good way? you put a setter and getter and do the following

myInt.setI(myInt.getI() + 1);
Navneeth G
  • 7,255
  • 1
  • 15
  • 18
0

The value of i is passed as a copy to the inc() method, so the original int i is not updated.

Primitive arguments are passed to methods as a VALUE copy.

Object arguments are passed as a VALUE copy of the REFERENCE to the object.

cdc
  • 2,511
  • 2
  • 17
  • 15
  • Object arguments are not passed by reference. Everything in Java is pass-by-value. Variables do not directly represent objects; they are references to objects. If you are passing a reference to a method, you are passing a reference by value. That is conceptually not exactly the same as passing an object by reference. – Jesper Feb 09 '12 at 14:51
0

What you are trying to do is passing a parameter by reference, and this is not possible in Java.

In Java parameters are, in general, references passed by value. For native types, you can assume these are passed by value.

The issue is discussed at length here: Is Java "pass-by-reference" or "pass-by-value"? and here http://javadude.com/articles/passbyvalue.htm (this is the best explaination I found).

Community
  • 1
  • 1
Savino Sguera
  • 3,522
  • 21
  • 20
-2

Simply, if you don't want write anything, AtomicInteger class may helps.

lol...

okin2014
  • 323
  • 2
  • 5