The Artima Developer Community
Sponsored Link

Java Answers Forum
parametrized types (again)

1 reply on 1 page. Most recent reply: Mar 20, 2006 6:24 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
M Adawi

Posts: 9
Nickname: adawi
Registered: Mar, 2006

parametrized types (again) Posted: Mar 20, 2006 5:26 AM
Reply to this message Reply
Advertisement
Tell me if I am wrong.
in java language specifications 3rd edition, it was told that "All parametrized types shares the same class at runtime".

I think this differ from templates in C++ because templates make it as if their is more than one class in memory, each with a different type.

now I am scraching my head wondering that if parametrized types is trying to reduce ClassCastException in runtime and convert those runtime exceptions to compile time errors, so the code is more safer, and that is all parametrized types do.

calling parametrized type GENERIC is not accurate because actually any polymorphic behavior is generic. you can make LinkedList generic by making node of type Object,but what if you want to add some interface to the list, then you have to create a parametrized type
LinkedList <T extends Object & SomeInterface>,

in other words, I cannot say that
Vector<T> (where T extends Object by default) is any thing except more than "safty generic code" and actually, you can do it without <T>, by making Vector takes Object reference.

another question:
Is parametrized types is something provided only since jdk 5.0.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: parametrized types (again) Posted: Mar 20, 2006 6:24 AM
Reply to this message Reply
To the 2nd question: Yes, this exists only since version 1.5

I don't know exactly how the internal system works, but in older version a Vector or a LikedList or whatever xould only store instances of Object. So if you wanted to access one of the elements you always had to cast it to it's type first.
You could also cast it to another class type, but at runtime this woudl result in a ClassCastException.

Since Java 1.5 you can specify the class of the stored elements. This has 3 effects:
1. You can add only objects of the specified or an extending class.
2. If you access a element it is automatically casted to the right class
3. Class Cast Exceptions at runtime are not possible anymore if you don't force them (casting to Object, then casting to a wrong class for example).

The generic classes make programming easier and more secure.
I really love this feauture, it really improves the readability of the code and you must really WANT to create a ClassCastException.
//old:
Vector myVector = new Vector();
myVector.add(anyObject);
.
.
((TargetClass)myVector.get(1)).targetClassMethod();
 
 
//new:
Vector myVector<TargetClass> = new Vector<TargetClass>();
myVector.add(targetClassInstance);
.
.
myVector.get(1).targetClassMethod();

Flat View: This topic has 1 reply on 1 page
Topic: help with a constructor if it doesn't accept string arguments Previous Topic   Next Topic Topic: rmiregistry healing needed!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use