Solved

Java 5/6 Runtime Generics

Posted on 2013-02-01
10
987 Views
Last Modified: 2013-05-08
Java 5/6 Runtime Generics

Please explain why the following snippet does not actually do what is intended.

if (appleList instanceof List<Apple>) { System.out.println("true"); }
0
Comment
Question by:javaagile
  • 5
  • 5
10 Comments
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38843178
How did you create appleList?
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38843189
Actually , it shld not compile , since you cannot use instanceof on a strict type . You can use it on a generic type though like List<?>
0
 

Author Comment

by:javaagile
ID: 38843197
I got this question on one written test. The question does not specify how appleList was created. I think you don't need to know that information to answer this question. This question is testing runtime generics.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:javaagile
ID: 38843207
The answer has to be one of the following options

Runtime Generics

Explain why the following snippet does not actually do what is intended.

if (appleList instanceof List<Apple>) { System.out.println("true"); }

a) Because you do not know that appleList is a List type.
b) Because generic types are erased before runtime.
c) Because the instanceof keyword only works on primitive types.
d) Because the statement could cause appleList to be set to an instance of a List.
e) Because you can only check the type of collections using reflection.
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38843209
Is is an academic test?
0
 

Author Comment

by:javaagile
ID: 38843213
I need to understand why this will not work as intended
0
 
LVL 12

Accepted Solution

by:
Sharon Seth earned 500 total points
ID: 38843239
Generics is to ensure strict type checking at compile time , to prevent a later on runtime type cast error . But this concept of Generics  is not implemented in the JVM (most probably for backward compatibility) . So , during compilation , the compiler removes the generic type (called as type erasure)  . Since instanceof is a runtime directive , and generic type info is not available at runtime , the compiler does not allow you to do it.
0
 

Author Comment

by:javaagile
ID: 38843266
Thanks for responding,. so what will be the answer from below options?

a) Because you do not know that appleList is a List type.
b) Because generic types are erased before runtime.
c) Because the instanceof keyword only works on primitive types.
d) Because the statement could cause appleList to be set to an instance of a List.
e) Because you can only check the type of collections using reflection.
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 38843294
I think I have given enough explanation  for you to find the answer yourself .
 As such , experts-exchange prohibits answering test questions directly .
0
 

Author Comment

by:javaagile
ID: 38843300
Thanks that helps
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

825 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