0

I have this code that is giving me a suppress warning

public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    }

The error mentions something about accepting an unchecked conversion so I tried adding Class<?> but it also recommended to put in Class<? extends Object>. I have read about this before but I can't remember where. I think <?> means it accepts any object, and ? extends Object must mean that the class is a subclass of Object, but isn't that the same thing? Both examples give me no error.

gallly
  • 1,201
  • 4
  • 27
  • 45

2 Answers2

1

There is no difference, they are synonymous. I found a good answer for the same question:

By @ruakh:

"

<?> and <? extends Object> are synonymous, as you'd expect, but there are a few cases with generics where extends Object is not actually redundant. For example, <T extends Object & Foo> will cause T to become Object under erasure, whereas with <T extends Foo> it will become Foo under erasure. (This can matter if you're trying to retain compatibility with a pre-generics API that used Object.)

"

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241
1

They both are synonymous. So there is no difference

Also check this out:- Converting Legacy Code to Use Generics

Also as notnoop answered in this question there is a very minor difference between the two:

  1. The JVMS (Java Virtual Machine Specification) has a special specification for the unbounded wildcards, as ClassFileFormat-Java5 specifies that unbounded wildcard gets encoded as *, while encodes a Object-bounded wildcard as +Ljava/lang/Object;. Such change would leak through any library that analyzes the bytecode. Compiler writers would need to deal with this issue too. From revisions to "The class File Format"

  2. From reifiablity standpoint, those are different. JLS 4.6 and 4.7 codify List<?> as a reifiable type, but List<? extends Object> as a erasured type. Any library writer adding .isReifiable() (e.g. mjc lib) needs to account for that, to adhere to the JLS terminology. From JLS 4.6 and 4.7.

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331