Re: The Java no pointer big fat lie!
From: Rene (invalid_at_email.addr)
Date: 02/24/05
- Next message: Rhino: "Re: BoxLayout bug in 1.5?"
- Previous message: Chris Smith: "Re: Redirecting InputStream: 2 questions"
- In reply to: axter: "The Java no pointer big fat lie!"
- Next in thread: bugbear: "Re: The Java no pointer big fat lie!"
- Reply: bugbear: "Re: The Java no pointer big fat lie!"
- Reply: axter: "Re: The Java no pointer big fat lie!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 23 Feb 2005 23:13:39 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.
Java calls them references. They have the same job as a pointer but you
can't do pointer arithmetic, ie pointer+20 is impossible in Java.
Also I believe the references do not point directly to the memory location
of the object they reference but to an intermediate which does that. I
would need to read and confirm that myself though.
> 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.
> However, that also wouldn't be entirely accurate either, but it would
> be closer to the truth then to say Java doesn't use pointers.
If you want to see it that way: ok.
You could also say that references are a special kind of pointers. They are
certainly more specialized and limited.
[lines snipped]
>
> In C++, when a concrete variable is declared, its constructor is always
> called
> When a reference variable is declared, a constructor is never called.
> When a pointer variable is declared a constructor is called only if it
> is initialized by pointing to a NEW-operator.
> In this characteristic, Java address modal matches only the pointer
> modal.
static initialization ?
> 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)
> 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.
> 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.
> 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.
> 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.
> 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)
> 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.
> OK, frag away!
ka-lack :)
CU
René
-- -------------------- http://NewsReader.Com/ -------------------- Usenet Newsgroup Service $9.95/Month 30GB
- Next message: Rhino: "Re: BoxLayout bug in 1.5?"
- Previous message: Chris Smith: "Re: Redirecting InputStream: 2 questions"
- In reply to: axter: "The Java no pointer big fat lie!"
- Next in thread: bugbear: "Re: The Java no pointer big fat lie!"
- Reply: bugbear: "Re: The Java no pointer big fat lie!"
- Reply: axter: "Re: The Java no pointer big fat lie!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|