0
How to pass a value by reference to another function without creating a class or an instance of a class in java?
I want to pass by reference and not value. but unlike c++ where we can use pointers and addresses in java we have no such symbols (&,*). Instead of creating a new class and passing it's object for reference is there any simpler way?
2 Antworten
+ 3
Java doesn't have pointer arithmetic as C or C++, but you can create an empty object without calling the constructor.
+ 2
Use a method to get the value you want, setting your current variable to whatever the method returns.
Note* This is still not passing by reference.
Example/
int a = 5;
// Now I want a new value for a
a = factorial(a);
// what the method looks like
int factorial(int num){
if(num == 1)
return 1;
return num * factorial(num - 1);
}