4

I'm developing an java application and now I noted an a strange behaviour that confused me. I have the following situation:

    Integer currentIndex = 0;
    doSomethings(currentIndex);
    System.out.println("Print "+currentIndex);


    private void doSomethings(Integer currentIndex){
        //Do other things and update my current index
        currentIndex++;


    }

but I get always 0 like value. I remember that the objects are passed like reference in java, while the primitive types like copy. Why Do I get 0 in this case?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Skizzo
  • 2,883
  • 8
  • 52
  • 99
  • 1
    If possible, avoid using these boxed classes in java. If you do operations like this, there is implicit unboxing in the background, and in case you would return it, there would be a re-boxing. – Balázs Édes Oct 06 '14 at 11:23
  • Your method `doSomething` doesn't return anything, and you're confusing yourself having a variable called `currentIndex` which is also the name of the passed-in variable. As it stands your method simply increments the (local) passed-in value, which isn't the same as the one you declare in the main call code. – Ben Oct 06 '14 at 11:24
  • Note that reference in Java and C are two different things. Reference in Java is somewhat like pointer in C. – Dmitry Zaytsev Oct 07 '14 at 14:56

7 Answers7

9

Instance of Integer is immutable. It means you cannot change its value. When you increase it by one, Integer gets converted into int (this is called "unboxing"), then this int gets increased by one. You can even cast it back to Integer. But this will be already another instance of Integer, not the one you've passed to the method. That's why your original reference stays equal to zero all the time.

Byte code :

private static void doSomethings(java.lang.Integer);
  Code:
   Stack=2, Locals=1, Args_size=1
   0:   aload_0
   1:   invokevirtual   #56; //Method java/lang/Integer.intValue:()I  --> get the int value of the Integer
   4:   iconst_1 --> constant value 1
   5:   iadd   --> add int value and 1
   6:   invokestatic    #16; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In --> get another integer with value increased by 1
teger;
   9:   astore_0
   10:  return
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
2

This is an interesting construct.

Note that Objects in Java don't allow custom operator implementations. And that the ++ operator is not defined for a java Object.

So java will do a trick that is call 'auto boxing'. This converts Integer to int and vice versa when needed.

What happens when you do:

Interger myInt = 0;
int i = myInt++; // this line

is as follows: myInt will be converted to int, then that int value is incremented and store in i. Note that the integer value is not saved to myInt.

This is what happens in your function, the ++ only happens in a temporary variable, not in the actual Integer object. Note that because the Integer object is immutable, this is not even possible.

Thirler
  • 20,239
  • 14
  • 63
  • 92
1

Everyone gives correct solution and beworker also give nice reason. I add up one reason.

Java always pass everything by value. So in your code when you pass currentIndex it pass by value so any changes to currentIndex in dosomething() method not reflected outside of block.

For more explanation . I refer you to see Eng.Fouad answer he very well explained this concept .

Community
  • 1
  • 1
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40
1

Java is not pass by reference. It is pass by value ( value of a reference). Above code does not work since Integer is an Immutable class. So you cannot change it's value.

So get your method work you need to return value in your method as below.

private Integer doSomethings(Integer currentIndex){

            return currentIndex++;   
 }
0
    currentIndex = doSomethings(currentIndex);
    private Integer doSomethings(Integer currentIndex){
          //Do other things and update my current index
          return currentIndex++;     
    }
Sharon Haim Pour
  • 6,595
  • 12
  • 42
  • 64
0

Go through the following code

  Integer currentIndex = 0;
  currentIndex = doSomethings(currentIndex);
  System.out.println("Print "+currentIndex);


  private Integer doSomethings(Integer currentIndex,){
    //Do other things and update my current index
    return currentIndex++;

doSomethings() is increment the value of the currentIndex and return that. Then store that object into the currentIndex reference

Good Luck !!!

0

Java is pass by value these two articles explain well

http://www.javaranch.com/campfire/StoryCups.jsp http://www.javaranch.com/campfire/StoryPassBy.jsp

Read in that order.