Hello, I'm fairly new to Java, I'm very experienced with C++ and C# in which you can pass by reference - extremely useful. Take for example this bit of code in C#:
class MyClass { public MyClass(int i) { m_i = i; } public int m_i; } class Program { static void DoSomething(ref int i) { i = i * 2; } static void Main(string[] args) { MyClass x = new MyClass(1); DoSomething(ref x.m_i); DoSomething(ref x.m_i); DoSomething(ref x.m_i); } }
Just at the end of this program x.m_i will be equal to 8. As far as I can see this is not possible in Java: you can't pass a double by reference, using a Double will kick in the autoboxing so that won't work either. The only "solution" in Java would be to pass in a double[] (of length 1) or to make a wrapper class, both nasty solutions because a user may want to just hold a double as a member of their class just as I have, for reasons such as not allocating more memory for a class and generally not being bloated. Does anybody have any ideas of a solution to this if there is one? if there isn't will there be one in Java?
Peter