View Single Post
  #17   Spotlight this post!  
Unread 18-05-2009, 00:39
Chris27's Avatar
Chris27 Chris27 is offline
Registered User
AKA: Chris Freeman
FRC #1625 (Winnovation)
Team Role: Alumni
 
Join Date: Mar 2005
Rookie Year: 2004
Location: Mountain View
Posts: 196
Chris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant future
Re: **FIRST EMAIL**/Java and Orbit Balls

Quote:
Originally Posted by The Lucas;860118
So 4 instructions are necessary for [B
ptr = array + i;[/b] and only 2 instructions for ptr++; in this assembly language.
So what's your point? Of course an expression that contains multiple variables will generally be more complex then an expression that contains a single variable. ptr = (&(ptr[0])) + 1 will again compile to the same assembly. Array indexing is just an encapsulation for pointer arithmetic as I explained earlier. Any pointer arithmetic expression will have a matching array index expression and both of these expressions will compile to the exact same instructions.

Quote:
I Googled "java swap" and this site explains it pretty well. Basically when you pass by reference to a function in Java it gives the function a new (different) reference to the object and there is no way to change the original reference in the passing function. In C++, the pointer is like an absolute reference (memory location) that is the same everywhere and you can even use pointers to pointers (no references to references in Java).
If you want to change a reference to an object, just have the method return a new reference. If you want a series of references, just encapsulate the references in objects. If you absolutely need to have access to the virtual address of an object, then Java just isn't the right language for the job.

Here is swap with references:

Code:
public class MyInt {
	
	int data;
	public MyInt(int x)
	{
		this.data = x;
	}
	
	public static void swap(MyInt a, MyInt b)
	{
		a.data ^= b.data;
		b.data ^= a.data;
		a.data ^= b.data;
	}

}

Last edited by Chris27 : 18-05-2009 at 01:09.