Quote:
Originally Posted by Chris27
ptr = (&(ptr[0])) + 1 will again compile to the same assembly.
|
You can't dereference (&) and do pointer arithmetic like that in Java.
Quote:
Originally Posted by Chris27
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.
|
There is no matching array index expression for this:
Array indexing requires a symbol (base of array) and a type (size of element). When an integer can be cast to a pointer, pointer arithmetic can be done without an array.
Quote:
Originally Posted by Chris27
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;
}
}
|
I know you can create a new class, a similar example was on the page I linked. I just said it was simpler in C. Creating new classes or returning multiple references doesn't sound simple to me.
Again, I understand both languages and the reasons why Java doesn't allow explicit pointers (source of bugs, reference counting for garbage collecting, sercurity, etc..).
Lets revisit how this whole conversion happened (with me paraphrasing)
Billy: Yuck Java. Pointers ftw
Jared: What cant you do with Java references?
Me: Pointer Arithmetic.
Jared: Granted, but you better have a good reason.
<Long posts involving example code by Chris, me and Alan>
I thought twice before pressing Submit Reply on my first post because I knew it wouldn't be my last on this thread

Programming debates are interesting (for me atleast) but time consuming. Maybe I should have just let Billy reply or learned from C++ vs LV threads.