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

Discussion in 'Java' started by odwl, Jun 15, 2006.

  1. odwl

    odwl Guest

    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
    odwl, Jun 15, 2006
    #1
    1. Advertising

  2. Re: generics Object.getClass clarification. "<?>" vs "<? extendsObject>"

    odwl wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jun 15, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    9,339
    Viator
    Nov 24, 2005
  2. JavaEnquirer

    Generics and getClass

    JavaEnquirer, Feb 10, 2006, in forum: Java
    Replies:
    4
    Views:
    24,521
    darkaico
    May 4, 2011
  3. Philipp
    Replies:
    2
    Views:
    398
    Rolf Magnus
    Jun 29, 2006
  4. MattLFerraro

    clarification on generics

    MattLFerraro, Feb 11, 2008, in forum: VHDL
    Replies:
    12
    Views:
    813
  5. Paka Small
    Replies:
    19
    Views:
    1,154
Loading...

Share This Page