Friday, September 03, 2004

Re: Java's pass "by reference" demystified

An interesting thing to note is that in Java there is no way to create a "swap" method for primitives. A swap method is where you send in two arguments and have their values interchanged. Now, there are wrapper objects for all primitives - like Integer, Doublt etc... It's possible with that. With plain primitives, it's impossible.

Of course, I got to throw in how C# differs. There is a "ref" keyword which does pass by (true) reference as in C++. So you can infact create a swap in C#...

private void swap( ref int x, ref int y )
    int temp = x;
    x = y;
    y = temp;

Btw, I'm also doing a course on OOP using Java. We got some introductory material on Java fundamentals.

No comments: