[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Java 5/6 Runtime Generics

Posted on 2013-02-01
10
Medium Priority
?
1,029 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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