java - Why does this cast to a generic type parameter work? -


having idea of type erasure, think this cast not compile or work @ runtime:

public class genericdatumreader<d> implements datumreader<d> {     ...     @suppresswarnings("unchecked")     public d read(d reuse, decoder in) throws ioexception {       return (d) read(reuse, actual, expected != null ? expected : actual, in);     } 

however, works!

how program know @ runtime d is?

edit

i wrote simple test:

  1 public class main {                                                                                                                                                              2                                                                                                                                                                                  3     public static class something<d> {                                                                                                                                           4                                                                                                                                                                                  5         private object getobj() { return new object(); }                                                                                                                         6         private string getstr() { return new string("hello"); }                                                                                                                  7                                                                                                                                                                                  8         public <d> d get(boolean str) {                                                                                                                                          9            return (d) (str ? getstr() : getobj());                                                                                                                              10         }                                                                                                                                                                       11     }                                                                                                                                                                           12                                                                                                                                                                                 13     public static void main(string[] args) {                                                                                                                                    14         something<string> = new something<>();                                                                                                                        15         string str = something.get(true);                                                                                                                                       16         string notstr = something.get(false);                                                                                                                                   17     }                                                                                                                                                                           18 }  

without (d) cast, doesn't compile:

main.java:9: error: incompatible types: bad type in conditional expression            return (str ? getstr() : getobj());                                ^     string cannot converted d   d type-variable:     d extends object declared in method <d>get(boolean) main.java:9: error: incompatible types: bad type in conditional expression            return (str ? getstr() : getobj());                                           ^     object cannot converted d   d type-variable:     d extends object declared in method <d>get(boolean) 2 errors 

with (d) cast unchecked warning compiles. however, @ runtime:

$ java main exception in thread "main" java.lang.classcastexception: java.lang.object cannot cast java.lang.string         @ main.main(main.java:16) 

there 2 aspects here:

  1. compile-time: works because thing know d @ compile-time it's object, it's possible returned value going d--the compiler can't determine otherwise.
  2. run-time: because java implements generics via erasure, generic types entirely compile-time concept. method has no idea d @ run-time, cast doesn't @ all. if non-generic method assigned result type known @ runtime (i.e. non-generic), create cast exception if object not of right type.

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -