I have the following recursive function prototype:
public void calcSim(Type<String> fort, Integer metric)
Integer metric = 0;
calcSim(fort, metric);
System.out.println("metric: " + metric);
}
I want to print the value of metric as shown above. However it is always zero. Now, when I print at the end of the function, I do get a valid number.
- How do I pass by reference or get the equivalent functionality like in C++
- What all can I do with regards to my parameter passing? (by value, by reference, etc…)
There is no such thing as pass by reference in Java, sorry 🙁
Your options are either to give the method a return value, or use a mutable wrapper and set the value as you go. Using AtmoicInteger cause it is in JDK, making your own that doesn’t worry about threadsafety would of course be mildly faster.
Then inside the calcSim set it with
metric.set(int i);