473,379 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Generics references

I've noticed that the type in generic declarations is not
"inherited" by new references to the object. This seems
inconsistent to me but maybe there's a good reason for it. To wit:

ArrayList<Stringlist1 = 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?


May 28 '07 #1
2 2232
jupiter <ju*****************@msn.comwrote:
>I've noticed that the type in generic declarations is not
"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<Stringlist1 = new ArrayList<String>();
ArrayList list2 = list;
List2 is explicitly NOT declared as a parameterized type. It happens to have
an ArrayList<Stringin 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 is NOT on the List interface
--
Mark Rafn da***@dagon.net <http://www.dagon.net/>

May 29 '07 #2

"Mark Rafn" <da***@dagon.netwrote in message
news:cs************@hydra.dagon.net...
jupiter <ju*****************@msn.comwrote:
>>I've noticed that the type in generic declarations is not
"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<Stringlist1 = new ArrayList<String>();
ArrayList list2 = list;

List2 is explicitly NOT declared as a parameterized type. It
happens to have
an ArrayList<Stringin 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 is NOT on the
List interface
Nice example indeed. This is yet another case of how implicit
casting can cause headaches for we normally-brained people.

May 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kamen Yotov | last post by:
Hello, I got my hands on the PDC preview version of whidbey and I think I managed to break the compiler/runtime with my first Generics/Iterators attempt. The reason I am posting it here is that I...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
4
by: INeedADip | last post by:
Can I put a generic List<MyObject> in cache? I read that generics can't be serialized.. Can I cache them? Can I return them from Web Services?
4
by: Gazarsgo | last post by:
This seems to be a bit of a contradiction, but how can I construct a Generic class using a System.Type variable that is assigned at run time? Is there some parallel to the Generics concept that...
1
by: Peter Nofelt | last post by:
Hey All, I'm wondering if I'm able to have create generic user controls in .net 2.0 much like one can create generic classes. I would like to do this so that I can handle a set a types derived...
1
by: Ole Nielsby | last post by:
I'm puzzled at how static fields intersect with generics, as the following snippet illustrates. namespace MonkeyParty { abstract class Fruit { } class Apple : Fruit { } class Pear : Fruit {...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
7
by: JCauble | last post by:
I have a question about using Generics with Interfaces and some of there inheritance issues / problems. If this is not possible what I describe below I will have to go a different route and would...
10
by: Frank Rizzo | last post by:
Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I am converting them to List<and Dictionary<respectively. It's a pretty massive system, so there are a lot of casts. For...
2
by: NamelessNumberheadMan | last post by:
So here is my problem. I have two beans (one extends the other), and I have one DAO class for each to access their tables (using Hibernate). In most cases the queries will be identical so I also have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.