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 / June 2006

Tip: Looking for answers? Try searching our database.

generics Object.getClass clarification. "<?>" vs "<? extends Object>"

Thread view: 
odwl - 15 Jun 2006 22:01 GMT
Hi,

First, it seems to me that <T> is completely redundant with <T extends
Object>.
Indeed I can cast without warning List<T> and List<T extends Object> in
both way
Could someone confirm? Is that specifically said in the spec? Is that
really really equivalent?

Second, for the same reason, I also think that <?> is equivalent to <?
extends Object>.
Could someone confirm?

Third, the signature of the method Object.getClass() is:
Class<? extends Object> getClass().
Why they didn't write the following simple simple instead:
Class<?> getClass().

I know that there is a "trick" here in the sense that that methods does
not really return <? extends Object> but  <? extends X> where X is the
erasure of the static type (as mentioned in the javadoc) so perhaps
they just choose <? extends X> because it is more "similar"!!

Could have they choose  Class<?>. in place of Class<? extends Object>?
Was the choice made by chance?

Regards,

Olivier
Thomas Hawtin - 15 Jun 2006 22:17 GMT
> First, it seems to me that <T> is completely redundant with <T extends
> Object>.

The extends Object is redundant, I think.

> Indeed I can cast without warning List<T> and List<T extends Object> in
> both way
> Could someone confirm? Is that specifically said in the spec? Is that
> really really equivalent?

You can't cast to a generic type without warnings. And I don't think
extends/super makes sense in a cast, but you are using rather than
declaring the generic parameter.

> Second, for the same reason, I also think that <?> is equivalent to <?
> extends Object>.
> Could someone confirm?

Yup.

> Third, the signature of the method Object.getClass() is:
> Class<? extends Object> getClass().
> Why they didn't write the following simple simple instead:
>  Class<?> getClass().

It is in 1.6. It's fudged by the compiler/language spec. If you call
getClass on List<String> you get a result of type Class<? extends List>.
The 1.6 docs have more details (and there is a bug in the Bug Parade
covering the change).

"public final Class<?> getClass()
    Returns the runtime class of this Object. The returned Class object
is the object that is locked by static synchronized methods of the
represented class.

    The actual result type is Class<? extends |X|> where |X| is the
erasure of the static type of the expression on which getClass is
called. For example, no cast is required in this code fragment:

    Number n = 0;
    Class<? extends Number> c = n.getClass();"

http://download.java.net/jdk6/docs/api/java/lang/Object.html#getClass()

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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



©2008 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.