How to do call by reference in Java?
How to do call by reference in Java?
You cannot do call by reference in Java. Period. Nothing even comes close. And passing a reference by value is NOT the same as call by reference.
(Real "call by reference" allows you to do this kind of thing:
void swap(ref int i, ref int j) { int tmp = *i; *i = *j; *j = tmp }
int a = 1;
int b = 2;
swap(a, b);
print("a is %s, b is %s\n", a, b); // -> "a = 2, b = 1"
You simply can't do that in Java.)
Since Java doesn't support pointers ...
While this is technically true, it would not be an impediment to what you are trying to do.
Java does support references, which are like pointers in the most important respects. The difference is that you can't treat references as memory addresses by doing arithmetic on them, converting them to and from integer types and so on. And you can't create a reference to a variable because the concept does not exist in Java or the JVM architecture.
How is it possible to call a function by reference in Java like we do in C and C++??
Important note: this is NOT "call by reference". It is "call by value" using a reference to a function1.
Prior to Java 8, references to functions were not supported as values. So you could not pass them as arguments, and you cannot assign them to variables.
However, you can define a class with one instance method, and use an instance of the class instead of a function reference. Other Answers give examples of this approach.
From Java 8 onwards, method references are supported using ::
syntax. There are 4 kinds of method reference:
- Reference to a static method:
ContainingClass::staticMethodName
- Reference to an instance method of a particular object:
containingObject::instanceMethodName
- Reference to an instance method of an arbitrary object of a particular type:
ContainingType::methodName
- Reference to a constructor:
ClassName::new
1 - From a pedagogical perspective, it is unfortunate that C and C++ use the &
symbol for both call-by-reference and for denoting function pointers. However, from a language design perspective, I don't disagree with that design choice.
Usually in java this is solved by using interfaces:
Collections.sort(list, new Comparator() {
@Override
public int compareTo(Object o1, Object o2) {
/// code
}
});
package jgf;
public class TestJavaParams {
public static void main(String[] args) {
int[] counter1 = new int[1];
counter1[0] = 0;
System.out.println(counter1[0]);
doAdd1(counter1);
System.out.println(counter1[0]);
int counter2 = 0;
System.out.println(counter2);
doAdd2(counter2);
System.out.println(counter2);
}
public static void doAdd1(int[] counter1) {
counter1[0] += 1;
}
public static void doAdd2(int counter2) {
counter2 += 1;
}
}
Output would be:
0
1
0
0
Real pass-by-reference is impossible in Java. Java passes everything by value, including references. But you can simulate it with container Objects.
Use any of these as a method parameter:
- an array
- a Collection
- an AtomicXYZ class
And if you change its contents in a method, the changed contents will be available to the calling context.
Oops, you apparently mean calling a method by reference. This is also not possible in Java, as methods are no first-level citizens in Java. This may change in JDK 8, but for the time being, you will have to use interfaces to work around this limitation.
public interface Foo{
void doSomeThing();
}
public class SomeFoo implements Foo{
public void doSomeThing(){
System.out.println("foo");
}
}
public class OtherFoo implements Foo{
public void doSomeThing(){
System.out.println("bar");
}
}
Use Foo
in your code, so you can easily substitute SomeFoo
with OtherFoo
.