How to Pass an Integer Class Correctly by Reference

How can I pass an Integer class correctly by reference?

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";
}

Java : Best way to pass int by reference

You can try using org.apache.commons.lang.mutable.MutableInt from Apache Commons library. There is no direct way of doing this in the language itself.

How to Increment a class Integer references value in java from another method

No, objects aren't passed by reference. References are passed by value - there's a big difference. Integer is an immutable type, therefore you can't change the value within the method.

Your n++; statement is effectively

n = Integer.valueOf(n.intValue() + 1);

So, that assigns a different value to the variable n in Increment - but as Java only has pass-by-value, that doesn't affect the value of n in the calling method.

EDIT: To answer your update: that's right. Presumably your "MyIntegerObj" type is mutable, and changes its internal state when you call plusplus(). Oh, and don't bother looking around for how to implement an operator - Java doesn't support user-defined operators.

Pass Integer.class as parameter

"1" is a String and incompatible with Integer.class

new Predicate<>("id", "=", 1, Integer.class)

should be ok

or

new Predicate<>("id", "=", "1", String.class)

Passing an integer by reference in Python

It doesn't quite work that way in Python. Python passes references to objects. Inside your function you have an object -- You're free to mutate that object (if possible). However, integers are immutable. One workaround is to pass the integer in a container which can be mutated:

def change(x):
x[0] = 3

x = [1]
change(x)
print x

This is ugly/clumsy at best, but you're not going to do any better in Python. The reason is because in Python, assignment (=) takes whatever object is the result of the right hand side and binds it to whatever is on the left hand side *(or passes it to the appropriate function).

Understanding this, we can see why there is no way to change the value of an immutable object inside a function -- you can't change any of its attributes because it's immutable, and you can't just assign the "variable" a new value because then you're actually creating a new object (which is distinct from the old one) and giving it the name that the old object had in the local namespace.

Usually the workaround is to simply return the object that you want:

def multiply_by_2(x):
return 2*x

x = 1
x = multiply_by_2(x)

*In the first example case above, 3 actually gets passed to x.__setitem__.

java Integer reference

As said in the other answers, Java does only call-by-value, and the ++ operator only effects a variable, not an object. If you want to simulate call-by-reference, you would need to pass a mutable object, like an array, and modify its elements.

The Java API has some specialized objects for this, like java.util.concurrent.atomic.AtomicInteger (which additionally also works over multiple threads), and org.omg.CORBA.IntHolder (used for call-by-reference for remote calls by the CORBA mechanism).

But you can also simply define your own mutable integer:

class MutableInteger {
public int value;
}

class C {
public C(int[] i) {
++i[0];
}
}
class D {
public D(MutableInteger i) {
++i.value;
}
}
class E {
public E(AtomicInteger i) {
i.incrementAndGet();
}
}
public class Jaba {
public static void main(String args[]) {

int[] iii = new int[]{ 0 };
System.out.println(iii[0]);
new C(iii);
System.out.println(iii[0]);
MutableInteger mi = new MutableInteger();
System.out.println(mi.value);
new D(mi);
System.out.println(mi.value);
MutableInteger ai = new AtomicInteger(0);
System.out.println(ai);
new E(ai);
System.out.println(ai);
}
}

How to increment an integer by reference?

int is a primitive type so there is no reference being assigned, only values. You can wrap it in a class:

public class IntegerHolder {
private int value;

public IntegerHolder(int value) {
this.value = value;
}

public void increment() {
value++;
}
...
}

An Integer-Object: Does it become assigned by value or by reference to a method?

In the Counter constructor indeed a reference to the original Integer object is passed and stored.

However, the assignment counter = i; creates a new Integer object and assigns a reference to the new object to the variable counter. Thus the myInt still refers to the original object.

And by the way, Integer is immutable, meaning the value of an Integer object can never change. So even if you hold a reference it will always have the same value.

Passing integer by reference

You can pass a pointer to classLevelInt as int*.

classLevelInt = 2;
[self someMethod:&classLevelInt];

- (void)someMethod:(int*)anInt {
//do some stuff
if(somecondition){
*anInt = 2 + 3; //some operation
}
}

A second way, you can directly change classLevelInt in the same class.

- (void)someMethod {
//do some stuff
if(somecondition){
classLevelInt = 2 + 3; //some operation
}
}


Related Topics



Leave a reply



Submit