Sagewire Logo

Generics references

3 Message(s) by 2 Author(s) originally posted in java programming


From: jupiter Date:   Monday, May 28, 2007
I have noticed that the type in generic declarations isn't
"inherited" by new reference s to the object . This seems
inconsistent to me but maybe there's a good reason for it. To wit:

ArrayList<String> list 1 = new ArrayList<String>();

ArrayList list2 = list;

list1.add(new Integer(3)); //compiler catches it as expected.
list2.add(new Integer(3)); //compiler is fat, dumb and happy.

I thought the idea of a reference was to point to the original
object and pick up all of the relevant data. Why not type?


From: dagon Date:   Tuesday, May 29, 2007
wrote in message:
I've noticed that the type in generic declarations isn't
"inherited" by new references to the object. This seems
inconsistent to me but maybe there's a good reason for it.



Type inference is intentionally weaker than it could be. There are good
reasons for it, I suspect, but it annoys me sometimes.

To wit:
ArrayList<String> list1 = new ArrayList<String>();
ArrayList list2 = list;



List2 is explicitly NOT declared as a parameterized type. It happens to have
an ArrayList<String> in it now, but you could assign it an ArrayList<Object>
later without error.

list1.add(new Integer(3)); //compiler catches it as expected.
list2.add(new Integer(3)); //compiler is fat, dumb and happy.



Right, because you told it to be.

I thought the idea of a reference was to point to the original
object and pick up all of the relevant data. Why not type?



Type is tricky. It's both a property of the variable and a property of the
referent. The object type can be more specific than the variable, and that's
how polymorphism works.

Note the similarity to:
LinkedList list1 = new LinkedList();
List list2 = list1;
list1.addFirst("first"); // allowed, addFirst is a method on LinkedList
list2.addFirst("first"); // disallowed, addFirst isn't on the List interface
--
Mark Rafn dagon@xxxxxxxxxxx <http://www.dagon.net/>


From: jupiter Date:   Tuesday, May 29, 2007
wrote in message:
I have noticed that the type in generic declarations isn't
"inherited" by new references to the object. This seems
inconsistent to me but maybe there's a good reason for it.
Type inference is intentionally weaker than it could be. There
are good
reasons for it, I suspect, but it annoys me sometimes.
To wit:
ArrayList<String> list1 = new ArrayList<String>();
ArrayList list2 = list;
List2 is explicitly NOT declared as a parameterized type. It
happens to have
an ArrayList<String> in it now, but you could assign it an
ArrayList<Object>
later without error.
list1.add(new Integer(3)); //compiler catches it as expected.
list2.add(new Integer(3)); //compiler is fat, dumb and happy.
Right, because you told it to be.
I thought the idea of a reference was to point to the original
object and pick up all of the relevant data. Why not type?
Type is tricky. It's both a property of the variable and a
property of the
referent. The object type can be more specific than the
variable, and that's
how polymorphism works.



Great piece of insight, Mark. I never differentiated between type
and other properties, so this is nice piece to become aware of.> Note the similarity to:
LinkedList list1 = new LinkedList();
List list2 = list1;
list1.addFirst("first"); // allowed, addFirst is a method on
LinkedList
list2.addFirst("first"); // disallowed, addFirst isn't on the
List interface



Nice example indeed. This is yet another case of how implicit
casting can cause headaches for we normally-brained people.



Next Message: Collections.class methods anachronisms?


Blogs related to Generics references

Dealing With A Mental Disorder; Your Anxieties, And Your Fears
Jewelry Tanzanite Sterling Silver Hearts Finance Insolvency International Tax Twin Falls Idaho Tanning Salons In San Antonio Texas Real Estate Agent Arkansas Generic Water Filter For Refrigerator Swimming Pool Safety Fence Del Mar ...

News & News In Spanish
Fondé en 1999 et aujourd'hui leader dans son domaine, Gameloft conçoit des jeux pour les téléphones incluant les technologies Java, Brew ou Symbian, dont le parc installé devrait dépasser deux milliards d'unités en 2008. ...

What Party Supplies do I Need to Make My Party a Hit?
Do It Yourself Child Safety Pool Fence People Profile Lawyerlitigationvioxx Australiatravel Interior Design School Directory Mounting Usb Hard Drive Linux Downloading Latest Indian Mp3 Songs Fendi Shoes Malaysiawebhosting ...

gate iron price wrought
HPMA stairs posts Copolymer-Bound Doxorubicin Induces gate iron decors for wooden fences Apoptosis in iron gate arch Ovarian front gate Carcinoma iron gate photos Cells by the Java generic. Take the Stage iron gate pictures wrought iron ...

Java Tutorials Part-2
The Applet class is defined in the java.applet.Applet package which we just imported. Since HelloWorldApplet is a subclass of the Applet class, our HelloWorldApplet automatically inherits all the functionality of the generic Applet ...

Free Ringtones For Wwe - Compose Ringtone - Signet Ring Cell
6 billion by wwe ringtones for wwe frog ringtones cellular free ringtones java gamesfree ringtones generic norvasc buy drugs free mp3 real ringtones for wwe john cena ringtone softwaredownload free nokia ringtones free nextel cingular ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional