Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass an Integer class correctly by reference?

I am hoping that someone can clarify what is happening here for me. I dug around in the integer class for a bit but because integer is overriding the + operator I could not figure out what was going wrong. My problem is with this line:

Integer i = 0; i = i + 1;  // ← I think that this is somehow creating a new object! 

Here is my reasoning: I know that java is pass by value (or pass by value of reference), so I think that in the following example the integer object should be incremented each time.

public class PassByReference {      public static Integer inc(Integer i) {         i = i+1;    // I think that this must be **sneakally** creating a new integer...           System.out.println("Inc: "+i);         return i;     }      public static void main(String[] args) {         Integer integer = new Integer(0);         for (int i =0; i<10; i++){             inc(integer);             System.out.println("main: "+integer);         }     } } 

This is my expected output:

 Inc: 1 main: 1 Inc: 2 main: 2 Inc: 3 main: 3 Inc: 4 main: 4 Inc: 5 main: 5 Inc: 6 main: 6 ... 

This is the actual output.

 Inc: 1 main: 0 Inc: 1 main: 0 Inc: 1 main: 0 ... 

Why is it behaving like this?

like image 723
sixtyfootersdude Avatar asked Jul 25 '10 20:07

sixtyfootersdude


People also ask

How do you pass a class by reference in Java?

Java supports pass-by-value,0 but there are three different ways to create a pass-by-reference in Java. Make the member variable public inside a class. Return a value from a method and update the same inside the class. Create a single element array and pass it as a parameter to the method.

Is Integer a reference type in Java?

Java has a two-fold type system consisting of primitives such as int, boolean and reference types such as Integer, Boolean. Every primitive type corresponds to a reference type.

Can we just cast the reference to an int?

You're not casting to an int, no Object can ever be cast to an int.


2 Answers

There are two problems:

  1. Integer is pass by value, not by reference. Changing the reference inside a method won't be reflected into the passed-in reference in the calling method.
  2. Integer is immutable. There's no such method like Integer#set(i). You could otherwise just make use of it.

To get it to work, you need to reassign the return value of the inc() method.

integer = inc(integer); 

To learn a bit more about passing by value, here's another example:

public static void main(String... args) {     String[] strings = new String[] { "foo", "bar" };     changeReference(strings);     System.out.println(Arrays.toString(strings)); // still [foo, bar]     changeValue(strings);     System.out.println(Arrays.toString(strings)); // [foo, foo] } public static void changeReference(String[] strings) {     strings = new String[] { "foo", "foo" }; } public static void changeValue(String[] strings) {     strings[1] = "foo"; } 
like image 160
BalusC Avatar answered Sep 27 '22 16:09

BalusC


The Integer is immutable. You can wrap int in your custom wrapper class.

class WrapInt{     int value; }  WrapInt theInt = new WrapInt();  inc(theInt); System.out.println("main: "+theInt.value); 
like image 27
Markos Avatar answered Sep 27 '22 17:09

Markos