Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Generics: SupressWarnings("unchecked"). Why, and why not injava.util.ArrayList

Reply
Thread Tools

Re: Generics: SupressWarnings("unchecked"). Why, and why not injava.util.ArrayList

 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      10-21-2009
phi <> wrote:
> public class ListImplementation<T> implements generics.List<T> {
> Object [] data;


Just guessing... If you made this T[] typed, does the warning then
disappear? The array itself would not "know" the difference, but
the Compiler does.

> public T getFirst() {
> if (isEmpty()) {
> return null; }
> return (T) data[0]; } // <<---- WARNING


In that case, you then won't need the cast, either, but probably
it won't hurt.
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      10-21-2009
Andreas Leitgeb wrote:
> phi <> wrote:
>> public class ListImplementation<T> implements generics.List<T> {
>> Object [] data;

>
> Just guessing... If you made this T[] typed, does the warning then
> disappear? The array itself would not "know" the difference, but
> the Compiler does.
>
>> public T getFirst() {
>> if (isEmpty()) {
>> return null; }
>> return (T) data[0]; } // <<---- WARNING

>
> In that case, you then won't need the cast, either, but probably
> it won't hurt.


I'd guess that just moves the warning, say to

T[] data = (T[])new Object[20];

Because

T[]data = new T[20];

is illegal.


 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      10-21-2009
Mike Schilling <> wrote:
>>> Object [] data;

>> Just guessing... If you made this T[] typed, ...

> I'd guess that just moves the warning, say to
> T[] data = (T[])new Object[20];
> Because
> T[]data = new T[20];
> is illegal.


Yes, I noticed after reading the other followup :-}

PS: I haven't tried "new T[...]", myself. I believe, that it
*could* theoretically work consistently, but thats moot, if
it isn't allowed in reality.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-21-2009
Andreas Leitgeb wrote:
> PS: I haven't tried "new T[...]", myself. *I believe, that it
> * *could* theoretically work consistently, but thats moot, if
> * it isn't allowed in reality.
>


Five minutes in the JLS uncovers:
"10.3 Array Creation
"... An array creation expression specifies the element type, ...
"It is a compile-time error if the element type is not a reifiable
type (§4.7)"

So, no, that expression is not allowed in reality.

--
Lew
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-22-2009
Andreas Leitgeb wrote:
> Mike Schilling <> wrote:
>>>> Object [] data;
>>> Just guessing... If you made this T[] typed, ...

>> I'd guess that just moves the warning, say to
>> T[] data = (T[])new Object[20];
>> Because
>> T[]data = new T[20];
>> is illegal.

>
> Yes, I noticed after reading the other followup :-}
>
> PS: I haven't tried "new T[...]", myself. I believe, that it
> *could* theoretically work consistently, but thats moot, if
> it isn't allowed in reality.


It would cause problems, because the following would be legal code:

T[] arr = new T[20]; // Not legal now, but we're assuming
Object[] oarr = arr; // Legal in general, since we know that T
extends Object
arr[0] = new Object();// compile-time legal, and since the "real"
row type
// of the Array is Object, legal
at run time too
T t = arr[0]; // Uh-oh.


 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      10-22-2009
Mike Schilling <> wrote:
> Andreas Leitgeb wrote:
>> PS: I haven't tried "new T[...]", myself. I believe, that it
>> *could* theoretically work consistently, but thats moot, if
>> it isn't allowed in reality.


> It would cause problems, because the following would be legal code:
> T[] arr = new T[20]; // Not legal now, but we're assuming
> Object[] oarr = arr; // Legal in general, since we know that
> // T extends Object
> arr[0] = new Object(); // compile-time legal, and since the "real"
> // row type of the Array is Object, legal


You need of course "oarr[0]" for the assignment, but the argument
is then sound. I admit defeat of that former belief

> at run time too
> T t = arr[0]; // Uh-oh.


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-22-2009
Andreas Leitgeb wrote:
> Mike Schilling <> wrote:
>> Andreas Leitgeb wrote:
>>> PS: I haven't tried "new T[...]", myself. I believe, that it
>>> *could* theoretically work consistently, but thats moot, if
>>> it isn't allowed in reality.

>
>> It would cause problems, because the following would be legal code:
>> T[] arr = new T[20]; // Not legal now, but we're assuming
>> Object[] oarr = arr; // Legal in general, since we know that
>> // T extends Object
>> arr[0] = new Object(); // compile-time legal, and since the
>> "real" // row type of the Array is
>> Object, legal

>
> You need of course "oarr[0]" for the assignment,


Right. It's difficult to test code that's based on a counterfactual
assumption

> but the argument
> is then sound. I admit defeat of that former belief
>
>> at run time too
>> T t = arr[0]; // Uh-oh.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57