Since Java doesnt support pointers, How is it possible to call a function by reference in Java like we do in C and C++??
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 function.
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:
ContainingClass::staticMethodName
containingObject::instanceMethodName
ContainingType::methodName
ClassName::new