aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Design for Collection, or its methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Design for Collection, or its methods?" Watch "Design for Collection, or its methods?" New topic
Author

Design for Collection, or its methods?

Wong Martin
Greenhorn

Joined: Jan 14, 2011
Posts: 25
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!!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Jan 14, 2011
Posts: 25
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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Jan 14, 2011
Posts: 25
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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Jan 14, 2011
Posts: 25
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

Joined: Jan 14, 2011
Posts: 25
""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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I'm glad it helped
Aditya Yagnik
Greenhorn

Joined: Mar 29, 2010
Posts: 7

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

Joined: Jan 14, 2011
Posts: 25
Thanks Aditya! Apologize for my late reply..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design for Collection, or its methods?