Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Strange Generics Behavior

Thread view: 
Benjamin Lerman - 21 Apr 2006 08:28 GMT
Hi all,

I encounter a strange behavior with generics that I do not understand.

I have the following interfaces:

public interface Int1<E> {   
}

and

public interface Int2<E> extends Int1<Integer> {
}

and I have somewhere the following lines:

void foo(Int2 o) {
 Int1<Integer> p = o;
}

The java compiler gives the following warning:

Type safety: The expression of type Int2 needs unchecked conversion to
conform to Int1<Integer>

I do not understand why this warning exists. Moreover it prevents the
compiler to understand that an Int2<E> is also an Int1<Integer>, so it
prevents a lot of type checking.

Is there any solution ?

Thanks.

    Benjamin Lerman
hiwa - 21 Apr 2006 10:11 GMT
> public interface Int2<E> extends Int1<Integer> {
>
> }
There can't be such.
It should be:

interface Int2 extends Int1<Integer> {

}
Benjamin Lerman - 21 Apr 2006 10:31 GMT
>> public interface Int2<E> extends Int1<Integer> {
>>
>> }

> There can't be such.
> It should be:
>
> interface Int2 extends Int1<Integer> {
>
> }

Why?

Note that E in Int2 is not Integer. I want to have something like:

public class O1 implements Int2<Float> {
}

    Benjamin
Chris Uppal - 21 Apr 2006 10:53 GMT
> Type safety: The expression of type Int2 needs unchecked conversion to
> conform to Int1<Integer>
>
>  I do not understand why this warning exists. Moreover it prevents the
> compiler to understand that an Int2<E> is also an Int1<Integer>, so it
> prevents a lot of type checking.

I think the warning is spurious.  I suspect that there's a bug somewhere.

The compiler (as far as I know) emits this warning when it is unable to prove
to itself that an operation is safe /and/ cannot emit a runtime checkcast
operation (because the types are identical after erasure).  In this case there
is no checkcast (correctly), but I can't see why it shouldn't realise that
anything which conforms to Int2<anything> necessarily also conforms to
Int1<Integer>.

>  Is there any solution ?

Just ignore it.

   -- chris
Simon - 21 Apr 2006 10:55 GMT
Hi,

> public interface Int1<E> {   
> }
[quoted text clipped - 14 lines]
> Type safety: The expression of type Int2 needs unchecked conversion to
> conform to Int1<Integer>

I'm not sure why you get that warning, but you can avoid it if you declare foo
like this:

 void foo(Int2<Object> o)

You can reproduce the same warning with the following simpler code that doesn't
use inheritance:

 Int1 o = null;
 Int1<Object> p = o;

I wonder what the difference is between Int1 and Int1<Object>. Is there any
example where this is significant or rather where using Int1 instead of
Int1<Object> causes any problems/exceptions?
Hendrik Maryns - 21 Apr 2006 13:48 GMT
Simon schreef:
> Hi,
>
[quoted text clipped - 16 lines]
>> Type safety: The expression of type Int2 needs unchecked conversion to
>> conform to Int1<Integer>

Indeed, for the same reason why you get this warning when you use List
foo = new ArrayList<Date>(); : you use an ungeneric instance of a
generic class.

> I'm not sure why you get that warning, but you can avoid it if you declare foo
> like this:
>
>   void foo(Int2<Object> o)

Well, I would declare foo like something more meaningful than
Int2<Object>, but that entirely depends on what you meant the second
generic parameter of Int2 to mean.

> You can reproduce the same warning with the following simpler code that doesn't
> use inheritance:
[quoted text clipped - 5 lines]
> example where this is significant or rather where using Int1 instead of
> Int1<Object> causes any problems/exceptions?

There is a huge difference between Collection<Object> and just
Collection.  For example Collection<Integer> is an heir of Collection,
but not of Collection<Object>.  (Interestingly, in Eiffel this is not
the case.  I wonder how they handle the problems that arise out of this.
They had generics from the start, so the whole type erasure stuff does
not exist.)

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Piotr Kobzda - 21 Apr 2006 11:35 GMT
>  Is there any solution ?

This:

  void foo(Int2 o) {
    Int1<Integer> p = (Int2<?>) o;
  }

or better:

  void foo(Int2<?> o) {
    Int1<Integer> p = o;
  }

Java compilers treat raw types specially. :)

Regards,
piotr


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.