> The pounds (#) may not
> be necessary. However, I include them to further insist
to Coldfusion to pass
> by value rather than by reference.
It does nothing of the sort. Pound-signs simply flag to CF that
a char
sequence represents a variable name rather than a string, in
situations
where there could be ambiguity:
- within a string, eg: "My name is #name#", as opposed to "My
name is
name".
- outside of CF tags, eg: <cfoutput>My name is
#name#</cfoutput>, as
opposed to <cfoutput>My name is name</cfoutput>
Within your example there is no ambiguity, so the pound signs
are simply
redundant.
All your sample code is, likewise, redundant.
I'll simply repeat the Coldfusion fact. The statement
<cfset var1 = #var2#> assigns the value of var 2 to the
variable var1.
All you are demonstrating is that the copied-by-reference
objects have
different REFERENCES. Well of course they do. Each of those
references
(different memory locations in the stack) point to the same
memory location
(in the heap) for the actual object instance data.
No disagreement there, as my code in fact demonstrates the
point. It was simply to show Coffeeflower that my starting point
was two objects rather than two instances of one object.
When you set obj to null, you are setting the REFERENCE to null,
not the
object. Incidentally, no GC will be done on the object until
ALL the
references to it are deleted (and even then, only when Java
decides to do it).
Your argument is still good in Java. However, I think it
needs some qualification in Coldfusion. My hypothesis is based on
the pound (#) symbol, for which there is no equivalent in Java.
I'll maintain the distinction you make between references on the
stack and objects on the heap. Not only is it unimpeachable, it
will now help me present my hypothesis more clearly.
An object reference points to the location of an object and
its metadata on the heap. Object references, like the primitives,
<cfset myVar = 5>
<cfset x = #myVar#>
are stored on stack memory. I therefore wonder whether one
can use the pound (#) symbol, or any other native Coldfusion
technique, to create a primitive-like assignment. Could we, by
replacing the reference to the heap with a primitive value
assignment, create a copy of an object on the stack?