473,486 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Collections.class methods anachronisms?

The Collections class is supposed to, among other things, return
type safe collections from existing collections with static methods
such as .checkedList().

My question is: What is so special about these static methods since
we can do the same thing by declaring the original list generically
with a type like <String>? I mean, once you create the list with
<String the compiler will no longer allow you to add, say,
Integer to the list. So what purpose does .checkedList() provide
in that context?

Maybe it's all about backward compatibility?
May 28 '07 #1
3 2545
jupiter wrote:
The Collections class is supposed to, among other things, return
type safe collections from existing collections with static methods
such as .checkedList().

My question is: What is so special about these static methods since
we can do the same thing by declaring the original list generically
with a type like <String>? I mean, once you create the list with
<String the compiler will no longer allow you to add, say,
Integer to the list. So what purpose does .checkedList() provide
in that context?

Maybe it's all about backward compatibility?

For the most part (where "most part" includes all of the collections
interface), all of the type constraints of generics are only checked at
compile time. The type-safe methods, e.g., checkedList, are guarantees
at /runtime/, something which generics can't do. Observe:

ArrayList<Stringfoo = new ArrayList();
List bar = foo;
bar.add(5);
String s = foo.get(0).substring(4);

This passes the compiler (although the compiler does give a warning), so
the code will be compiled to bytecode where it promptly emits a
ClassCastException. Passing the list through to checkedList still gives
an error, but it is emitted at the point of modification as opposed to
the point of access (which, in some cases, might not even give an error!).

In short, it is mostly a backwards-compatible feature, but it is
desirable in circumstances, so it is in no way an anachronism.
May 29 '07 #2

"Joshua Cranmer" <Pi*******@epenguin.zzn.comwrote in message
news:F517i.6680$3B1.1920@trnddc08...
jupiter wrote:
>The Collections class is supposed to, among other things,
return type safe collections from existing collections with
static methods such as .checkedList().

My question is: What is so special about these static methods
since we can do the same thing by declaring the original list
generically with a type like <String>? I mean, once you create
the list with <String the compiler will no longer allow you to
add, say, Integer to the list. So what purpose does
.checkedList() provide in that context?

Maybe it's all about backward compatibility?

For the most part (where "most part" includes all of the
collections interface), all of the type constraints of generics
are only checked at compile time. The type-safe methods, e.g.,
checkedList, are guarantees at /runtime/, something which
generics can't do. Observe:

ArrayList<Stringfoo = new ArrayList();
List bar = foo;
bar.add(5);
String s = foo.get(0).substring(4);

This passes the compiler (although the compiler does give a
warning), so the code will be compiled to bytecode where it
promptly emits a ClassCastException. Passing the list through to
checkedList still gives an error, but it is emitted at the point
of modification as opposed to the point of access (which, in some
cases, might not even give an error!).

In short, it is mostly a backwards-compatible feature, but it is
desirable in circumstances, so it is in no way an anachronism.
Great example, Joshua. Thanks.

I guess I was stuck thinking "compiler means everything now" but
when using references to do implicit casting, anything can happen
at runtime as you've clearly shown.

May 30 '07 #3
jupiter wrote:
>This passes the compiler (although the compiler does give a
warning), so the code will be compiled to bytecode where it
promptly emits a ClassCastException. Passing the list through to
checkedList still gives an error, but it is emitted at the point
of modification as opposed to the point of access (which, in some
cases, might not even give an error!).
I guess I was stuck thinking "compiler means everything now" but
when using references to do implicit casting, anything can happen
at runtime as you've clearly shown.
This is true, but don't underestimate compiler warnings.

im
Jul 1 '07 #4

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

Similar topics

10
17181
by: Mart | last post by:
What class does everyone out there use if they want to store a set of values efficiently? In java I use a HashSet, but there is no equivalent in C#. Even worse, the lowest level interface to...
2
2874
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states:...
2
3700
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
3
1184
by: Paul | last post by:
Hello, I'm upgrading a small single user app to VB.NET and have a few questions about loading classes & collections classes with the data from a one to many dataset structure. I'm serializing...
2
3279
by: ljlevend | last post by:
I've noticed that in VS.NET 2.0 Beta 1 that none of the methods in System.Collections.Generic.List are overridable. In my app I currently have over 50 strongly typed ArrayLists that inherit from...
6
7188
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
25
2993
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
6
1328
by: =?Utf-8?B?QW5kcmV3IEhheWVz?= | last post by:
Maybe I'm missing a code snippet or something, but there doesn't seem to be a way in VS2005 to create a class that is a collection of another class. Something that was very easy to do in the old...
3
1926
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.