Re: Java Generic programming using subclassing

From: Paul O'Grady (someone_at_springfield.com)
Date: 02/17/05


Date: Thu, 17 Feb 2005 01:52:11 +0000 (UTC)


John C. Bollinger wrote:
> Paul O'Grady wrote:
>
>> One last question however, before I implement this is:
>>
>> (This stems from my C/C++ background - excuse me if I'm being too
>> *anal*ytic). I want to represent the 2D array as a memory slice (i.e.
>> a "stretched out" iD array. I want to alloc memory (well not directly)
>> to a contiguos block, and then use array indexing to partition the
>> slice into seperate colums.
>
>
> Sorry, you can't do that.
>
> That is to say, Java nowhere guarantees that the elements of an array
> are stored contiguously in memory. Now chances are that any Java
> implementation you ever use _will_ store the elements of a 1D array
> contiguously in memory, but you can't assume it. It doesn't matter,
> though, because you can't test it either. Please recognize also that
> Java arrays are full-fledged objects in their own right, with their own
> classes distinct from their component type. This is unlike in C and
> C++, where arrays are just a convenient alternative syntax for pointers.
>
> You certainly can use a 1D sequential data structure (e.g. an array or a
> List) as a backing store for data access via two or more indices, but
> you should consider what you expect to gain by doing so. Before the
> word "performance" escapes your fingers, let me warn you that the
> strongly prevailing opinion in this forum is in agreement with the maxim
> that "premature optimization is the root of all evil". In point of
> fact, all modern Java VMs implement some form of just-in-time
> compilation to native code, and these compilers are quite good at
> optimization, especially when they see patterns they recognize. People
> are notoriously bad at predicting where performance bottlenecks will
> occur, so unless you have a known performance problem and a profile to
> show what's causing it, you should avoid writing contorted code intended
> to sidestep performance problems.
>
>> I'll have a class like this:
>>
>> class MemBlock {
>> Number[] data ; //Contiguous block
>> int numCols ;
>> int numRows ;
>> int currCol ; //This will be the variable that indicates which
>> "column" is being referred to
>> }
>>
>> so I can write some thing like this:
>>
>> a = new MemBlock(1000,10) ; // numrows, numcols respectively
>> b = a ; // (Thinking in C, if these were pointers, then I have just
>> assigned the base address of the array a to b )
>
>
> Try not to think in C (or C++); it will get you into trouble when you
> are trying to write Java. You may be using a valid model, but I'm not
> sure. Let's look in a bit more detail:
>
> MemBlock a = new MemBlock(1000, 10);
>
> Presuming that you give the MemBlock class a suitable constructor, this
> statement will instantiate a new MemBlock object and assign a reference
> to it to variable "a".
>
> MemBlock b = a;
>
> This copies the reference stored in variable "a" to variable "b". The
> two variables now both refer to the same object, and can be used
> interchangeably to examine or mutate that object.
>
>> b.currCol = 5 ; //This asigns b to the 5th "column" in the "memory
>> block"
>
>
> I'm not sure I follow you there, but do note that in the context of the
> previous statements, that one is equivalent to
>
> a.currCol = 5;
>
> I.e. you have only created one MemBlock object, to which variables "a"
> and "b" both refer, and changes you make to it through one copy of the
> reference will be visible through the other.
>
>> The theory is I can then use variable b as a reference to the 5th
>> colum in MemBlock a.
>
>
> This sounds pretty unlikely. There is no distinction at this point
> between the object "b" refers to and the one that "a" refers to. It is
> probably more useful, in fact, to call it "the MemBlock to which a
> refers" than to call it "MemBlock a", because there is no special
> association between an object and any particular copy of its reference.
>
> If you're going to care about distinct "banks" within your memory block
> then why not use a standard Java 2D array in the first place? Each
> major index of the 2D array then will refer to a distinct 1D array, with
> its own reference that you can use to construct a view of just that part
> of the array. The 1D subarrays can even be of different lengths if that
> happens to be useful.
>
> You may also want to look into List.subList(), which can provide a
> (mutable!) view of a portion of a List.
>
> All in all I get the feeling that you're trying to write C in Java,
> which can be done but is rarely advantageous. I'd advise you to spend
> some time learning the typical Java idioms for the kind of tasks you're
> trying to code -- where they differ from C idioms it's generally because
> they work better in Java, one way or another, than the C-like
> alternatives (not because Java programmers are weak-minded or lazy).
>
>> Is this vaild in Java, and will any other references to the MemBlock
>> variable be released (or garbage collected) when the variable goes out
>> o f scope. ?
>
>
> I don't see anything that looks illegal, but I don't think the code you
> describe will have the effects you describe. As for garbage collection,
> you rarely, if ever, have to worry about it: no object will ever be
> collected while there is still a hard reference (the only kind we've
> discussed so far) to it reachable from any live thread via a chain of
> hard references. Once it ceases to be so reachable an object becomes
> eligible for garbage collection, and will in fact be collected before
> the VM throws any OutOfMemoryError (but otherwise is not guaranteed ever
> to be collected).
>

Many thanks for all that John. I am beginning to see the way forward. I
may come back with some more questions if I get stuck. Many thanks once
again.



Relevant Pages

  • Re: Why C# and Java have got it wrong
    ... there are GC languages other than Java. ... > you used any language which conveniently supports higher order functions ... and I must apologize: garbage collection is ... > a reference to the proxy object living on the stack. ...
    (comp.programming)
  • Re: How to destroy arrays
    ... I know when it's eligible for garbage collection. ... When a is an array reference, ... I nowhere readed that implyment of Scott it was only, ...
    (microsoft.public.dotnet.general)
  • Re: How do I Create ragged arrays in Excel VBA?
    ... garbage collection is based on reference counting. ... array B - a reference which won't be removed until *A* is ... When ReDim Bis run a brand new ...
    (microsoft.public.excel.programming)
  • Re: Passing arrays ByVal vs ByRef
    ... reference type (such as an Array), you get a copy of the reference (the ... original array is modified when you modify the passed parameter. ... Convert VB to C#, C++, or Java ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Java Generic programming using subclassing
    ... Java nowhere guarantees that the elements of an array ... Now chances are that any Java ... Presuming that you give the MemBlock class a suitable constructor, ... eligible for garbage collection, and will in fact be collected before ...
    (comp.lang.java.programmer)