aspose file tools*
The moose likes Java in General and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
If the generics change had only changed collections, Java would have been better off adding a dynamic array type.


Why???


We must know, we will know. -- David Hilbert
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Originally posted by Joseph Sweet:


Why???


Where did you get the quote from ? and why why ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Got it here:

http://www2.sys-con.com/itsg/virtualcd/java/archives/0811/close/index.html

BTW they seem to have some mistakes in their code snippets but that's another story.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14103
    
  16

A bit of context around the quote:
Most of the generics changes that your average Java programmer will see are in the collections API and in extending those collections. The collections become easier to use and the compiler supports typing, as we saw above. Gone are the days when you created your own collection type to hold only one type of object; generics makes that code unnecessary.

The way generics change collections is not the end of the story. Code designers will be using generics to make code more flexible in more than just collections. If the generics change had only changed collections, Java would have been better off adding a dynamic array type.

What the author probably meant to say was: If the only reason why generics were added to Java is because Sun wanted to add type safe collections to the language, then we wouldn't have needed such a completely new and powerful concept as generics. Type safe collections could have been implemented in a much more simple way, by providing a dynamic array type (i.e. an array that can dynamically resize).

What it basically comes down to is that there are more reasons than just type safe collections to add generics to the Java language.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Type safe collections could have been implemented in a much more simple way, by providing a dynamic array type (i.e. an array that can dynamically resize).


The thing I don't understand is the reason behind this assertion. First we already have dynamic arrays in Java (anyone said Vector, List?).

Second, I don't see how dynamic arrays can help you with turning all collections into type safe? Some collections are not linear, but trees, dynamic lists etc.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One old guy's reading of this ...

The collections offer rough equivalents of dynamically sized arrays, not real dynamic arrays. If arrays could really resize themselves, making up some non-existant syntax, this one:

String[] myStrings = new String[?];

would be simpler (the original quote suggests) than

Vector<String> myStrings = new Vector<String>();

as long as coders don't want more from generics than typesafe bags - either arrays or collections.
[ April 17, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
My point was that some collections are not linear (e.g. they are not arrays, linked list, etc.). They instrinsically cannot be simulated by arrays, whether the arrays are dynamic or not.

So to make those non linear collections type safe you would have to implement generics.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
i have a suspicion, which may be ill-founded, that the un-resizability of arrays goes back to the history of programming languages.

An array was originally set up using "indexed addressing," where the processor is given the memory address of the first element, and all the other elements occupy the subsequent memory addresses. Passing an array name and an index allows the processor to add the index to the original address and find the required element very quickly [beware: oversimplification!]. So an array uses what is incorrectly called random access to find its elements, and also has a structure which matches the repeating structure of the hardware (memory). Some languages (eg C) will allow the programmer to find the actual memory location (from a pointer), and add 1 to it to get the next element.

But the memory location one beyond the end of the address will contain something different . . .

This means that increasing the size of an array either means using indirect addressing (the last element contains the memory location for the continuation, but it is awkward for the computer to distinguish the different type of addressing), or moving the whole thing to a new memory location. I presume this latter is what the collections framework does; Vector and ArrayList actually call (directly or indirectly) the System.arraycopy method.

Which means it is awkward to design the concept of a resizable array.
[ April 17, 2007: Message edited by: Campbell Ritchie ]
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
It's the no free lunch theorem
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
So to make those non linear collections type safe you would have to implement generics.


Gotcha. I'm trying to think if I've ever had to rely on the non-linear properties of any collection ... blank so far. Or am I missing something really obvious?

VB has resizable arrays with redim. It's manual and probably pretty expensive. Unless they simulate an array with a linked list (!) I guess they copy the old array to a new one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics