Solved

Java 5/6 Runtime Generics

Posted on 2013-02-01
10
974 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

8 Experts available now in Live!

Get 1:1 Help Now