• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design for Collection, or its methods?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Just wanna know the usage of generic type in object:



The code is trying to have fun with my question (not really funny though..). Anyway, my question is:

1. According to the polymorphism in Java, An object can pass by value in case we want it to be used by several times. So beside the consideration of safety reason, in java.util.Collection, what is the usage of declaring something like Set s = new HashSet <Integer> () ?? Once you use it in another place like HashSet hs = (HashSet)s, it doesn't really matter that whatever generic-type it is. For instance:


Even though the compiler will complain there is an unsafe operation or whatever it is, the code runs without error:



Even if we declare in a different generic type, it is still without a runtime problem (Or any hiding problems??). My question is, can I say that the generic type is somehow designed for the methods defined in Collection classes, instead of class itself?? Thank you!!
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should always parameterize your generic types. Sure, the code will run without them, but the point is that the compiler uses the parameter types to help you find bugs in your code. If you use raw types (when you don't use the type parameters), you are disabling a very useful feature of the compiler, which is designed to help you.
 
Wong Martin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,

Thank you! Perhaps my question wasn't clear...
Actually, I am trying to understand the usage of:



Why shoud I use it? It doesn't help, and by the way, just make me confused indeed:



Java is a strongly typed programming language; however, this made me wonder, how the generic type I declared after the keyword "new" works to help me, as Java itself also has a polymorphic mechanism? So:



What does it mean? And:



What is the relation between them (generic type)??

Thanks!!
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does work, but you're using it wrongly. You are mixing raw types with parameterized types. Here's what you should do instead:

You should always use a parameter type after a generic type identifier. It doesn't matter if you're casting, assigning or creating a new object. It ensures you and the compiler are on the same wavelength.
The only reason you can leave the parameter types out is because of backwards compatibility. If Java was designed with generics from the start, you wouldn't even have been able to leave parameterization out.
 
Wong Martin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan,

You hit the point which I was trying to say. No doubt I used a wrong way in generic type for those cases, and this is exactly what I was trying to say. Why does Java offer its user an opportunity to be wrong? I mean if we compare the features between generic-type and polymorphism:



This is incomparable in logic for sure; however, it came up with some questions to me. Why? Why did the inventors of Java make this happen? Any usage for programming (Line7)? Any reason? Any feature I haven't understood yet?? What is the relation between "ArrayList <Integer> al;" and "new ArrayList <Integer> ();"??
I am not focusing on right or wrong. It seems like Java has left programmer an option. This is my responsibility as a programmer to handle it in appropriate way, but as a student, I just want to make myself clear.
I hope this time my question has hit the point.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wong Martin wrote:Why does Java offer its user an opportunity to be wrong?



Like I said, because of backward compatibility. A lot of code was written in Java before version 1.5, when generics were introduced. Let's take an example. the java.util.List class exists since version 1.2. Let's say you made a program before Java 1.5, that used a List. Generics didn't exist, so your program would just use raw types.

When 1.5 came around the corner, the designers chose to change classes like List to use generic types, so you could do compile time type checking. However, if they forced you to parameterize all your types properly, this meant your old program would suddenly break.

Now, what the designers could have done instead, was create a new class for *every* type that uses generics, e.g. GenericList, and deprecate the raw types. You can imagine this is a lot of work, and makes the API very bloated. Instead, they allowed you to still use raw types, and just let the compiler give you a warning when you write new code that uses raw types.

What is the relation between "ArrayList <Integer> al;" and "new ArrayList <Integer> ();"??



What do you mean? The one is a variable declaration and the other is a constructor call. It doesn't have anything to do with generics.
 
Wong Martin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan,

You resolved all my doubts! Thank you for your patience!!

What is the relation between "ArrayList <Integer> al; // ArrayList al; " and "new ArrayList <Integer> ();"??

Actually, I couldn't understand the intention of such programming syntax when I first saw them and have no idea about its history. Now it all makes sense after your explanation.



 
Wong Martin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
""The only reason you can leave the parameter types out is because of backwards compatibility. If Java was designed with generics from the start, you wouldn't even have been able to leave parameterization out. ""

I haven't recognized that you had already explained the reason of my question!
Sorry for I was stupid~
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad it helped
 
Greenhorn
Posts: 7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Wong,

Adding to Stephan's very clear explanation, using older version of Collection syntax will work - even if you insert different types of objects. However, it is very likely to throw ClassCastException(occurs at runtime and will break your application) when you try to retreive the objects from collection.

Also, since Generics are implemented by type erasure in java, check for <Type> is done only at compile time and types are erased post compilation. Therefore, bytecode(please compare .class files) generated will be exactly same in case of parameterized and non-parameterized collection.

Slightly off the topic, I would recommend you read other uses of Generics if not already - HolderClasses.

Please let us know if you have any questions.

Best regards,
Aadi
 
Wong Martin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Aditya! Apologize for my late reply..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic