Re: The Java no pointer big fat lie!
From: axter (temp_at_axter.com)
Date: 02/26/05
- Next message: iksrazal: "Re: JAXB: Nested Type hides an Enclosing Type."
- Previous message: nobody: "Re: Hibernate Question..."
- In reply to: Rene: "Re: The Java no pointer big fat lie!"
- Next in thread: Chris Smith: "Re: The Java no pointer big fat lie!"
- Reply: Chris Smith: "Re: The Java no pointer big fat lie!"
- Reply: Rene: "Re: The Java no pointer big fat lie!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 25 Feb 2005 15:12:52 -0800
>>> Whenever I read a Java book and it states that Java has no
pointers, it
>>> makes my teeth grind. It would be more accurate to say Java only
uses
>>> pointers.
>>
>>> That's right. Java only uses pointers.
>>
>>
>>No references. A pointer is a memory address in C, no more, no less.
That's
>>not what a reference in Java is.
My comments are referring to C++ comparison and not C. Since C doesn't
have
reference types, it wouldn't be a good comparison.
In C++, a reference can not change what it's pointing to. That's not
what a
reference is in Java is.
In C++, you can not assign a new operator to a reference. That's not
what a
reference is in Java is.
>>> In C++, a concrete variable must be initialized to address valid
>>> memory.
>>> A reference variable must also be initialized to address valid
memory.
>>> A pointer may be initialized to point to nothing at all, or to
point to
>>> valid memory.
>>> In this characteristic, Java address modal matches only the pointer
>>> modal.
>>
>>
>>A java object reference must be initialized to address valid objects.
I'm
>>using your words to show that this sentence can be constructed so one
can
>>could say that the Java address model matches the reference model of
C++.
>>(When only looking at this aspect)
No, a Java object can be initialize to point to NULL.
You can not do that with a C++ concrete object or a C++ reference.
>>> In C++, a concrete variable (once initialized) can never change the
>>> memory it's addressing.
>>
>>
>>This is wrong. The compiler and language give you no tools to do that
but
>>you can do it as you wish. You have full control over the contents in
>>memory, includign the stack frame. You can change it as you wish.
Nobody is
>>protecting you to shoot yourself in the foot.
My statement is correct with reference to the C++ language. I'm not
talking
about hacking into the memory. That would be like me saying that Java
has pointer arithmetic via JNI. That's out side of the scope of the
language,
and out side of the scope of this topic.
>>> A reference variable (once initialized) can never change the memory
>>> it's addressing.
>>> A pointer can change what it's pointing to at any time.
>>> In this characteristic, Java address modal matches only the pointer
>>> modal.
>>
>>
>>Not in Java. You can only point to valid objects of a valid type (or
>>subtype) or NULL. It's not a pointer, its a reference. A pointer is a
>>number which is a memory location. Not so in Java. Thus it makes
sense to
>>not call it a pointer but look at it if it were one, if that helps
you
>>working with java.
That's not correct. The Java language specification states that a
reference is either a pointer or a null.
>>> In C++, you cannot assign a concrete variable to new operator.
>>> A reference variable cannot be directly assigned to a new operator.
>>> A pointer can be directly assigned to a new operator.
>>> In this characteristic, Java address modal matches only the pointer
>>> modal.
>>
>>
>>Well Instantiation is not assigning an operator to a reference in
Java.
>>There are very few operators in Java and there's no user-defineable
>>operator overloading either. "new" instantiates and returns a
reference
>>which you can then assign.
>>
>>
>>> In C++, pointer arithmetic cannot be performed on a concrete type.
>>
>>
>>You can. Nobody prevents you from doing it. Sure it's a hack, but you
can.
This is also incorrect. The C++ compiler is preventing you from doing
it.
>>
>>
>>> Pointer arithmetic cannot be performed on a reference type.
>>
>>
>>You can just as well. Just look to see what your reference type is in
>>memory and where it gets stored (stack frame) and mess around, swap
it
>>against another, etc.
>>
This is again outside of the scope of the language, and the same can be
said for Java if someone wanted to hack it.
Both are outside of the scope of the language standard, and outside
of the scope of this topic.
>>
>>> Pointer arithmetic can of course be performed on a pointer
variable.
>>> This is where Java memory modal and C++ pointers differ. You
cannot
>>> perform pointer arithmetic on a Java address method.
>>
>>
>>Yes, and that's a damn wise decision to make it so.
>>
>>
>>> Other then pointer arithmetic, Java's memory modal is very much a
C++
>>> pointer memory modal.
>>
>>
>>Nope, I disagree. It has similar components. But it's not the same
thing
>>and that's why they are called references in Java and not pointers,
so as
>>to not cause confusions. References are "weaker" in the sense that
you can
>>do less things with 'em than with pointers. But the concept is far
more
>>powerful. It enables things that are simply not possible otherwise. I
find
>>it always amazing what just one level of indirection can gain you
(often at
>>the cost of complexity though)
As I previously stated, they're not exactly the same, but it has more
in
common with pointers then it does with C++ reference or C++ concrete
types.
IMHO, the only reason they call it a reference in Java, is to not to
scare
away any newbie's to the language.
"A pointer by any other name is still a pointer."
>>> So if a Java book really wanted to be more accurate, it would say:
>>> "Java only uses pointers, and Java pointers lack the ability to
>>> perform pointer arithmetic."
>>
>>
>>No there's more to it, at least in my opinion. Yes, references and
pointers
>>are somewhat similar. It is not the same thing though.
You're right. They're not the same. And that's why I'm saying
Java is using pointers, and not reference types.
They may call it a reference, but when compared to C++ reference and
C++ pointer types, Java variables are closer to a C++ pointer, with the
one exception of it lacking pointer arithmetic.
- Next message: iksrazal: "Re: JAXB: Nested Type hides an Enclosing Type."
- Previous message: nobody: "Re: Hibernate Question..."
- In reply to: Rene: "Re: The Java no pointer big fat lie!"
- Next in thread: Chris Smith: "Re: The Java no pointer big fat lie!"
- Reply: Chris Smith: "Re: The Java no pointer big fat lie!"
- Reply: Rene: "Re: The Java no pointer big fat lie!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|