Re: The Java no pointer big fat lie!
From: Rene (invalid_at_email.addr)
Date: 02/26/05
- Next message: Anthony Borla: "Re: log java.util.logging.Logger to a file"
- Previous message: CD1: "log java.util.logging.Logger to a file"
- In reply to: axter: "Re: The Java no pointer big fat lie!"
- Next in thread: marcus: "Re: The Java no pointer big fat lie!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 26 Feb 2005 11:44:59 GMT
"axter" <temp@axter.com> wrote:
> >>> 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.
If you wish, that does not make a difference in my book. Ok, I know C far
better than C++. Or more specifically, my C++ knowledge is weak. But as in
C you can manipulate any arbitrary memory location at your bidding in C++
so my main argument still holds.
As an aside, by memory model I mentally include what you can do in memory
and what you can't - and there is a big difference in Java to C and C++.
This may not be exactly the definition of memory model (what is the exact
definition of memory model of a language by the way?), but I find it
captures the essence better - at least for me.
> In C++, a reference can not change what it's pointing to. That's not
> what a
> reference is in Java is.
You cannot change it in the language itself, because the language does not
allow it. You can change it in memory however - because the language allows
arbitrary and full control over memory. Not so in Java. You can NOT change
a reference in Java to point to a "wrong" object. NULL is special and is
allowed, but not wrong types. And there is no workaround to that. When you
use JNI you use an interface to another language. That's no longer Java,
it's C (or C++) that may mess with memory but not Java.
> In C++, you can not assign a new operator to a reference. That's not
> what a
> reference is in Java is.
Sorry I don't understand that sentence. You can never assign an operator to
any reference in Java. Operators are not assignable.
> >>> 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.
No, that is then not an initialized object.
> You can not do that with a C++ concrete object or a C++ reference.
You can. You have full control over the whole memory space. The language
does not prevent you from doing it, though it does not give you the tools
to do it trivially easy either.
> >>> 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.
No, JNI is only the interface. JNI has not the power to do it either. The
language invoked via JNI (usually C or C++) can do it. Java can't.
And it was my understanding that this might be the most important point in
the whole discussion (and thus very well in the scope of the topic). You
just cannot do it in Java. Java not only gives you no tools to manipulate
the memory at your will easily, it has NO mechanism at all to allow this
kind of thing.
Thus I would distinguish pointers (of C and C++ and other languages) from
references in Java (and again from reference types in C++). What you can do
with them and what they are used for is just too different to make the
distinction worthwhile.
As I've said, if you want to call Java's references pointers, that's ok.
But there's more difference than just Java prohibiting arithmetic on them.
> >>> 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.
Null in Java is not a number but a special reference type (castable to any
other type). And this makes null just another valid object-type.
Pointers in C are just a number specifiying the *exact* memory address in
the virtual address space of a process. This may be so in one Java VM but
may be comepletely different in another Java VM, enabling the latter to
completly reorder the memory layout of objects in order to reduce memory
fragmentation. Such a thing is impossible in C and C++ because of their use
of pointers which are not controllable by the language.
[snipped some lines]
> >>> 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.
int i;
void * pi = &i;
now search the stack frames and data segment for the value in pi.
Manipulate. Done.
Try to do the same in Java. Impossible. JNI can't do it either. C can. C++
can.
> >>> 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.
No it cannot be done in Java. But if you perceive this as outside of the
scope of the topic, then that's your call. I was pointing out that I
include such thoughts into the memory model of a language. There are
obviouse large differences which, as I argued, makes it useful to
distinguish between pointers and references.
You were right from the start that Java does have something like pointers.
Nobody disagreed with you on that point. Not me, not anyone else. I just
tried to explain the rationale of doing so from my point of view.
[snipped some more]
> >>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.
As has been pointed out, why do you insist that Java has no references
since it is something different than the reference type in C++ but then you
reverse the argument when it comes to pointers and state the contrary?
In the end, it's just a more or less arbitrarily named "thing". You could
write books over the difference of "integer" between many languages.
References in Java are Java's pointers. There are more differences than
just the lack of pointer arithmetric (or "reference arithmetric" if you
want). Call 'em so if you want. Be aware of the differences though. It
makes perfect sense to _me_ to not call them pointers.
CU
René
-- -------------------- http://NewsReader.Com/ -------------------- Usenet Newsgroup Service $9.95/Month 30GB
- Next message: Anthony Borla: "Re: log java.util.logging.Logger to a file"
- Previous message: CD1: "log java.util.logging.Logger to a file"
- In reply to: axter: "Re: The Java no pointer big fat lie!"
- Next in thread: marcus: "Re: The Java no pointer big fat lie!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|