• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Why were non generic collections allowed in earlier versions of Java?

 
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before Java 1.5 we were having below kind of lists without generics :


This means someone can enter an object of type String as well as object of time Animal in the list which would create problem at run time.

With generics this problem was solved as it would enforce to have only list with Strings or Animals .

But why was it allowed to enter anything in the list before Java 1.5 when we knew that that it would create problem at run time if it contains objects if type string as well as Animal?

Thanks

 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . With generics this problem was solved . . . .

It was by no means solved. It is still possible to create raw type objects, which circumvent most of the type‑checking. Sun threatened to ban raw types in Java6, but that threat appears never to have been carried out. Maybe it was not found to be feasible.
To maintain backward compatibility it was necessary to implement generics by erasure, so a Collections Framework class isn't a reifiable type. The backing data structures store their elements in an Object[] or as an Object rather than in an E[] or as an E, That necessitates casting all instances accessed to (E) myElement. Generics was a well‑established practice long before Java®, and Gosling took notice of older languages before witing Java®. Some of those languages, e.g. Eiffel, already implemented generics. There were plans to add generics as long ago as 1999, but it took until 2004 to achieve.
I have my own theory why generics wasn't implemented in Java1.0, but I have no evidence for that theory.
 
Master Rancher
Posts: 172
25
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your theory, Campbell?
 
Saloon Keeper
Posts: 12027
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implementing generics is HARD. If the designers had the idea at all to add generics, they probably thought it would take too much time while they had a perfectly usable language ready to ship.

Maybe they didn't realize what problems it would cause to delay the addition of generics until much later.
 
Saloon Keeper
Posts: 22126
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be glad. The very earliest versions of Java were especially poor in collections. As it grew, collections of Objects were added. When generics were implemented, the old forms had to have backwards compatibility - remember, Java is not only write-once/run-everywhere, but also to a large degree write-once/run forever. It deprecates rather than doing like Microsoft and pulling the rug out from under you. It can be really scary to have to dig out an old compiler and old libraries and even an old IDE in the wee morning hours to to a 1-line fix on a critical system.

So
is now interpreted as shorthand for


Why were generics so slow in arriving? The designers of Java had seen the messes that C++ had introduced and very deliberately excluded many C++ features in order to ensure a more secure and more reliable environment. Also, the C++ generic/template environment has a lot of compiler overhead, and Java was already slow on the machines and JVMs of the day.

Years of improvements and research have allowed some of these missing features to be added in later Java versions, although sometimes they're qualified and not the full C++ feature set. Java, after all, still has its original goals.

One C++ feature I do miss, though, is operator overloading. It would be really nice for business apps to be able to define a Java currency type with arithmetic operators and none of the imprecision that floating-point introduces.
 
Monica Shiralkar
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry . I think I did not ask my question properly. My question is not why did Java introduce generics late . My question is that until Java came up with generics in 1.5 why didn't they just do this much that If someone inserts first object of type string in an arraylist (non generic in those days ofcourse ) and as soon he enters another object as say int, it could show an error saying you cannot insert multiple types in a list.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question is self‑contradictory, like when somebody suggested the village barber shave Bertrand Russell.
 
Monica Shiralkar
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it self contradictory ?
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are asking why generics wasn't implemented when generics wasn't implemented.
 
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:why didn't they just do this much that If someone inserts first object of type string in an arraylist (non generic in those days ofcourse ) and as soon he enters another object as say int, it could show an error saying you cannot insert multiple types in a list.


I think that kind of restriction would have been unwise. Would you disallow adding a Dog to a list after a Cat has been added? What if this was a list of Pet? What if it was a list of DomesticAnimal? What about different types that implemented the same interface, would those also be allowed? Try to implement the restriction you're thinking about then see what kind of problems it will create.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Try to implement the restriction you're thinking about . . .

The only way to implement that is generics, which is why I thought the question was self‑contradictory.
 
Monica Shiralkar
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
I think that kind of restriction would have been unwise. Would you disallow adding a Dog to a list after a Cat has been added? What if this was a list of Pet? What if it was a list of DomesticAnimal? What about different types that implemented the same interface, would those also be allowed? Try to implement the restriction you're thinking about then see what kind of problems it will create.



Yes, thanks .
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Yes, thanks


Let me see if I'm clear on what you're saying "Yes" to: If it were up to you, you would have NOT allowed a Dog to be added to a List after a Cat was added. Is that right?
 
Tim Holloway
Saloon Keeper
Posts: 22126
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I'm writing a compiler and scanning tokens. Some tokens will be strings, some will be Integers, Float and so forth, and I want to put them all together in a List or a Tree? What mechanism would understand that if the collection set its element type based on the first entry added?
 
Monica Shiralkar
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
Let me see if I'm clear on what you're saying "Yes" to: If it were up to you, you would have NOT allowed a Dog to be added to a List after a Cat was added. Is that right?



Yes ,that is what I was thinking but as you said that way it would have created problems .
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic