Solved

Is instanceof illegal in this case?

Posted on 2011-09-19
6
282 Views
Last Modified: 2012-05-12
My textbook says that one of the restrictions on generic types is that instanceof tests and typecasts will not work, and will only work with the raw type. I know that instanceof tests to see if an object is of a specified type, but why doesn't that an typecasting work with generic types?
0
Comment
Question by:shampouya
  • 5
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36564094

check here:
http://stackoverflow.com/questions/4704902/java-instanceof-generic


Generics are a compile time feature. Generics add checks at compile time which may not have any meaning at runtime. This is one example. You can only check the type of the object referenced which could be a super type in code. If you want to pass the type T you have do this explicitly.

void someMethod(Class<T> tClass) {
    if(String.class.isAssignableFrom(tClass))

or

void someMethod(Class<T> tClass, T tArg) {

Note: the type might not be the same,

someMethod(Number.class, 1);

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36564098
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36564112
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Expert Comment

by:for_yan
ID: 36564127


With unboundedd wildcard "?" you can use generics in instanceof, but
these generics with unbounded wildcard are very close to non-gereic (raw) tyoes
reda here:
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html

The raw type and the unbounded wildcard parameterized type have a lot in common.  Both act as kind of a supertype of all instantiations of the corresponding generic type.  Both are so-called reifiable types. Reifiable types can be used in instanceof expressions and as the component type of arrays, where non-reifiable types (such as concrete and bounded wildcard parameterized type) are not permitted.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36564227

read this from
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ005
really clears many questions similar to yours above:

The compiler translates generic and parameterized types by a technique called type erasure .  Basically, it elides all information related to type parameters and type arguments. For instance, the parameterized type List<String> is translated to type List , which is the so-called raw type .  The same happens for the parameterized type List<Long> ; it also appears as List in the bytecode.

After translation by type erasure, all information regarding type parameters and type arguments has disappeared. As a result, all instantiations of the same generic type share the same runtime type, namely the raw type.

Example (printing the runtime type of two parameterized types):

    System.out.println("runtime type of ArrayList<String>: "+ne w ArrayList<String>().getClass());
    System.out.println("runtime type of ArrayList<Long>  : "+new ArrayList<Long>().g etClass());

    prints:   runtime type of ArrayList<String> : class java.util. ArrayList
          runtime type of ArrayList<Long>   : class java.util. ArrayList

The example illustrates that ArrayList<String> and ArrayList<Long> share the runtime type ArrayList .
0
 

Author Closing Comment

by:shampouya
ID: 36565145
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
wordcount challenge 11 102
Java DateChooser? 3 29
custom annotations 9 27
difference of if loops 23 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now